I have a div that displays items , quantities and total prices for each item using a foreach loop. I now want to get the total amount and dont seem to get it.
This is my blade file
<div class="order-summary">
<div class="order-col">
<div><strong>PRODUCT</strong></div>
<div><strong>TOTAL</strong></div>
</div>
<div class="order-products">
#if(session('cart'))
#foreach(session('cart') as $id => $details)
<?php
$total = 0;
$total += $details['price'] * $details['quantity'] ;
$total_amount += $details['price'] * $details['quantity'];
?>
<div class="order-col">
<div>
<h4 class="nomargin">{{$details['quantity'] }}x {{ $details['name'] }}</h4>
</div>
<div>KSh {{ $total }}</div>
</div>
#endforeach
#endif
</div>
<hr>
<div class="order-col">
<div>Shiping</div>
<div><strong>KSh 300</strong></div>
</div>
<hr>
<div class="order-col">
<div><strong>TOTAL</strong></div>
<?php $total_amount += $total?>
<div><strong class="order-total">KSh {{ $total_amount }} </strong></div>
</div>
</div>
I have also tried <div><strong class="order-total">KSh <?php echo $total_amount?> </strong></div> but still does not work.
I get an error of undefined variable total_amount. How do I go about it?
The variables $total and $total_amount must be outside of the foreach
here I have rewritten your code
<div class="order-summary">
<div class="order-col">
<div><strong>PRODUCT</strong></div>
<div><strong>TOTAL</strong></div>
</div>
#php $total = 0; $total_amount = 0; #endphp
<div class="order-products">
#if(session('cart'))
#foreach(session('cart') as $id => $details)
#php
$total += $details['price'] * $details['quantity'] ;
$total_amount += $details['price'] * $details['quantity'];
#endphp
<div class="order-col">
<div>
<h4 class="nomargin">{{$details['quantity'] }}x {{ $details['name'] }}</h4>
</div>
<div>KSh {{ $total }}</div>
</div>
#endforeach
#endif
</div>
<hr>
<div class="order-col">
<div>Shiping</div>
<div><strong>KSh 300</strong></div>
</div>
<hr>
<div class="order-col">
<div><strong>TOTAL</strong></div>
#php $total_amount += $total; #endphp
<div><strong class="order-total">KSh {{ $total_amount }} </strong></div>
</div>
</div>
Related
I have a table with multiple fields called "skills" . how do I show only one(first) data associated with "skills" .
#foreach ($others as $other )
#if ($other->type == 'skills')
<div class="col-md-6 p-0">
<img src="{{$other->photoOrVideo}}" class="imgbg-col" alt="imghome">
</div>
<div class="col-md-6 centered">
<div class="detailcontent">
<div class="subheading">{{$other->type}}</div>
<div class="heading">{{$other->titleOrName}}</div>
<div class="textdetail">{{$other->description}}</div>
</div>
</div>
#endif
#endforeach
in my controller-
public function index()
{
$sliders = Slider::latest()->with('service')->get();
$clients = OurClient::all();
$galleries = Gallery::all();
$services = Service::all();
$others = Others::all();
return view('frontend.pages.home',compact('services','galleries','clients','others','sliders'));
}
this is showing the same data twice. I want only the first data
I solved it. changed the blade file
<div class="row">
<?php $count = 0; ?>
#foreach ($others as $other )
#if ($other->type == 'skills')
<?php if($count == 1) break; ?>
<div class="col-md-6 p-0">
<img src="{{$other->photoOrVideo}}" class="imgbg-col" alt="imghome">
</div>
<div class="col-md-6 centered">
<div class="detailcontent">
<div class="subheading">{{$other->type}}</div>
<div class="heading">{{$other->titleOrName}}</div>
<div class="textdetail">{{$other->description}}</div>
</div>
</div>
<?php $count++; ?>
#endif
#endforeach
</div>
<div class="row">
#foreach($product as $data)
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img src="{{ asset('image/product_image/'.$data->product_image) }}" alt="photo">
<div class="card-body">
<h4 class="card-title">
{{ $data->product_name }}
</h4>
<h5>{{ $data->product_price }}</h5>
<p class="card-text">{{ $data->product_description }}</p>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
</div>
</div>
#endforeach
<!-- /.row -->
</div>
First collect what you want in the controller. It could be something like this:
$product = Product::all();
And then you must send the product variable to the view. Something like this:
return view('path.to.view', compact('product'));
By the way. it's better to use plural form products.
You can add the code in the blade to retrieve all the products from the product model
<div class="row">
#php
$product = App\Product::all();
#endphp
#foreach($product as $data)
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img src="{{ asset('image/product_image/'.$data->product_image) }}" alt="photo">
<div class="card-body">
<h4 class="card-title">
{{ $data->product_name }}
</h4>
<h5>{{ $data->product_price }}</h5>
<p class="card-text">{{ $data->product_description }}</p>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
</div>
</div>
#endforeach
<!-- /.row -->
</div>
Your code should be like this
Controller's index method,
public function index()
{
$product = Product::all();
return view('path', compact('product'));
}
I am using laravel-snappy & wkhtmltopdf 0.12.2.4 (unpatched qt) which doesn't support the --header-html switch so I have to manually create a header but the problem is the header and footer only appear on the first page. Is it possible to add a header and footer to every page without upgrading or downgrading wkhtmltopdf?
html
<div class="container">
<div class="header">
<img src="{{URL ('/images/logo.png')}}" alt="logo" width="125" height="50"/>
<h2>Parts Orientation</h2>
</div>
<div class="row marginBody">
#foreach ($parts as $part)
<div class="col-xs-2 marginThumbnail">
<div class="border text-center">
<img class="img" src="data:image/jpeg;base64, {{$part->image}}"
alt="Error" width="120" height="120"/>
<p class="displayText {{$part->class}}">
{{$part->name}}
</p>
</div>
</div>
#endforeach
<div class="footer">
<p> {{$date}} </p>
</div>
</div>
</div>
</body>
</html>
controller
public function printPDF(Request $request)
{
$parts = Parts::all();
$date = $this->getTime();
$pdf = SnappyPdf::loadView('parts.print.landscape', ["parts" => $parts, "date" => $date->toFormattedDateString()])
->setOrientation('landscape')
->setPaper('a4');
}
}
I was able to figure out a solution that works for me. Its very messy but it works
controller
public function printPDF(Request $request)
{
$parts = Parts::all();
$date = $this->getTime();
// divide by 36 because I can fit 36 items on each page
$totalPages = ceil(count($parts)/36);
$pdf = SnappyPdf::loadView('parts.print.landscape', ["parts" => $parts, "date" => $date->toFormattedDateString(), "totalPages" => $totalPages])
->setOrientation('landscape')
->setPaper('a4');
}
}
html
<body>
#php
$count = 0;
$page = 0;
#endphp
<div class="container">
<div class="header">
<img src="{{URL ('/img/logo.png')}}" alt="logo" width="125" height="50"/>
<h2>PARTS ORIENTATION</h2>
</div>
<div class="row marginBody">
#foreach ($parts as $part)
#php
// 36 is the total number of items I can fit on a page
// so after 36 I add a header and footer
if ($count === 36) {
echo '<div class = "newPageHeader">';
echo '<h2>PARTS ORIENTATION</h2>';
echo '</div>';
$count = 0;
$page++;
}
#endphp
<div class="col-xs-2 outline">
<div class="border text-center">
<img class="img" src="data:image/jpeg;base64, {{$part->image}}"
alt="Error" width="127" height="127"/>
<p class="displayText {{$part->c,ass}}">
{{$part->text}}
</p>
</div>
</div>
#php
$count++;
if ($count == 36) {
// 36 is the total number of items I can fit on a page
// so after 36 I add a header and footer
$page = ($page == 0) ? 1 : $page;
echo '<footer class="footer">';
echo "<span class='dateFooter'> $date </span>";
echo "<span class='pageFooter'> $page of $totalPages </span>";
echo '</footer>';
}
elseif ($totalPages == $page) {
echo '<footer class="lastFooter">';
echo "<span class='dateFooter'> $date </span>";
echo "<span class='lastPageFooter'> $page of $totalPages </span>";
echo '</footer>';
}
#endphp
#endforeach
</div>
</div>
</body>
So I have 2 tables.
DirtyEvent model and Event model.
I am retrieving DirtyEvent with Event which works fine
In blade I have:
#if (\Request::is('profile/event'))
#foreach ($events as $event)
#if (empty( $event->image ))
<div class="card-header">
<span class="card-title"> {{($event->title) }}</span>
</div>
#else
<div class="card-image">
<img class="img-responsive" src="{{ $event->image }}"></img>
<span class="card-title"> {{($event->title) }}</span>
</div>
#endif
<div class="card-content">
<p><strong>Starts: </strong>#php echo ($startdate) #endphp - {{$event->startime }}</p>
<br>
#if ($startdate != $enddate)
<p><strong>Ends: </strong>#php echo ($enddate) #endphp - {{$event->endtime }}</p>
<br>
#endif
<p><strong>Description:</strong></p>
<br>
<p>{{$event->description }}</p>
</div>
<div class="card-action">
<i class="fa fa-pencil-square-o fa-2x" aria-hidden="true"></i>
<form method="POST" action={{ url('events/delete/' . $event->id) }}>
{{ method_field('PATCH') }}
{{ csrf_field() }}
<button type="submit" class="delete" style="border:none;"><i class="fa fa-trash-o fa-2x" aria-hidden="true"></i></button>
</form>
</div>
#foreach ($events->publicevents as $eventss)
{{dd($events)}}
#endforeach
#endforeach
However dd($events) gives me:
DirtyEvent Collection -> relations -> publicevents -> Event collection
But, It is saying that publicevents do not exist in current collection.
Controller
public function index()
{
$id = Auth::id();
$events = DirtyEvent::where('user_id', $id)
->with('publicevents')
->get();
return view('events.viewEvent', compact('events'));
}
Model
public function publicevents()
{
return $this->hasMany('App\Event', 'event_id');
}
I guess not all dirty event objects have events. So check this first with empty() or isEmpty() or count():
#if (!empty($event->publicevents))
#foreach ($event->publicevents as $eventss)
{{ $eventss->id }}
#endforeach
#endif
Update
It should be $event->publicevents, not $events->publicevetns.
I'm getting Invalid argument supplied for foreach() error in my view after publishing a post and the code i use for looping in my controller is this:
public function edit($id)
{
$post = Post::find($id);
$categories = Category::all();
$cats = array();
foreach ($categories as $category) {
$cats[$category->id] = $category->name;
}
$tags = Tag::all();
$tags2 = array();
foreach ($tags as $tag) {
$tags2[$tag->id] = $tag->name;
}
return view('admin.posts.edit')->withPost($post)->withCategories($cats)->withTags($tags2);
}
this is the only part i handle loops in my postcontroller edit section. And I know the issue is from Tags loop because when I remove the tags code in my view other part will show up correctly.
Oh and this is the loop i use in my view:
#extends('layouts.app')
#section('content')
<div class="col-md-8">
<div class="panel panel-default">
<div class="panel-heading">{{ $post->title }}</div>
<div class="panel-body">
<p><img src="{{ asset('uploads/' . $post->image) }}" alt="{{ $post->title }}" class="img-responsive" /></p>
<p>{!! $post->body !!}</p>
</div>
<div class="tags">
#foreach ($tags as $tag)
<span class="label label-default">{{ $tag }}</span>
#endforeach
</div>
</div>
</div>
#endsection
#section('sidebar')
<div class="col-md-4">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-info"></i> Post Info</div>
<div class="panel-body">
<dl class="dl-horizontal">
<label>URL:</label>
<p>{{ url('blog/'.$post->slug) }}</p>
</dl>
<dl class="dl-horizontal">
<label>Created On:</label>
<p>{{ date('M j, Y h:ia', strtotime($post->created_at)) }}</p>
</dl>
<dl class="dl-horizontal">
<label>Last Update:</label>
<p>{{ date('M j, Y h:ia', strtotime($post->updated_at)) }}</p>
</dl>
<dl class="dl-horizontal">
<label>Posted In:</label>
<p>{{ $post->category->name }}</p>
</dl>
<hr/>
<div class="row">
<div class="col-md-6">
{!! Html::linkRoute('posts.edit', 'Edit', array($post->id), array('class' => 'btn btn-warning btn-block')) !!}
</div>
<div class="col-md-6">
{!! Form::open(['route' =>['posts.destroy', $post->id], 'method' => 'DELETE']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger btn-block']) !!}
{!! Form::close() !!}
</div>
</div>
<hr/>
{!! Html::linkRoute('posts.index', '<< Back to Posts', [], array('class' => 'btn btn-primary btn-block')) !!}
</div>
</div>
</div>
#endsection
PS: I'm using Laravel 5.4
Post updated view!
You can't get tags via post so Try this...
<div class="tags">
#foreach ($tags as $tag)
<span class="label label-default">{{ $tag }}</span>
#endforeach
</div>
Try:
return view('admin.posts.edit')->with("post",$post)->with("categories",$cats)->with("tags",$tags2);
and at view
<div class="tags">
#foreach ($tags as $tag)
<span class="label label-default">{{ $tag->name }}</span>
#endforeach
</div>
in your table if your primary key name if not id you have to mention in your relationship Link
2.for your post has one category so you can show this into your view like this
{{ $post->category->name }}
if you get result you can use it. but seems you need tag key and value pair so use this methods
$tags = Tag::where('post_id',$post->id)->groupBy('id')->get();
or try your way
$tags = Tag::all();
$tags2 = array();
foreach ($tags as $tag) {
$tags2[$tag->id] = $tag->name;
}
return view('admin.posts.edit',compact($post,$tags2,$cats));
in your view try this
#foreach ($tags as $key => $tag)
<span class="label label-default">{{ $tag }}</span>
#endforeach