Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
the product data already store in database but the data cannot show in the wishlist page.How do i solve it??
public function wishList(Request $request)
{
$products = wishlist::where('wishlist.pro_id','=','products.id')->get();
return view('wishlist',compact('products'));
}
public function addWishList(Request $request){
$wishList = new wishList;
$wishList->user_id = Auth::user()->id;
$wishList->pro_id = $request->pro_id;
$wishList->save();
}
this is the wishlist page that i want to show the product..
is it the data cannot get from the database?
<tbody>
#forelse($products as $product)
<tr>
<td>
<img src="{{url('/assets/images/products')}}/{{\App\Product::findOrFail($product->product)->feature_image}}" alt="">
</td>
<td>
/*product name*/
<a href="{{url('/product',$product->id)}}">
{{$product->title}}
</a>
</td>
</tr>
#empty
<tr>
<td colspan="6">
<h3>Your WishList Is Empty</h3>
</td>
</tr>
#endforelse
<tr>
<td colspan="4">
{{$language->continue_shopping}}
</td>
</tr>
</tbody>
I think the problem is in wishlist query:
$products = wishlist::where('wishlist.pro_id','=','products.id')->get();
This will produce sql query
SELECT * FROM wishlist WHERE wishlist.pro_id = 'products.id'
It will not throw mysql exception as 'products.id' is string and I don't think it is the query which you want.
I think you have to join your wishlist on products table and then
use whereRaw or where(DB::raw('...') Query Builder syntax
whereRaw:
Wishlist::whereRaw('wishlist.pro_id = products.id')->get()
where:
Wishlist::where(DB::raw('wishlist.pro_id = products.id'))->get()
Try using #foreach instead of #forelse
#foreach($products as $product)
<tr>
<td>
<img src="{{url('/assets/images/products')}}/{{\App\Product::findOrFail($product->product)->feature_image}}" alt="">
</td>
<td>
/*product name*/
<a href="{{url('/product',$product->id)}}">
{{$product->title}}
</a>
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="6">
<h3>Your WishList Is Empty</h3>
</td>
</tr>
#endif
First of all it's a bad practice to call a property in the same line you perform a query. Also, in the controller you referred to product id in the Wishlist table as prod_id while you called it as product in the template.
<tbody>
#forelse($products as $product)
<tr>
<td>
#php
$prod = \App\Product::findOrFail($product->prod_id)) // prod_id not product
#endphp
#if($prod)
<img src="{{url('/assets/images/products')}}/{{ $prod->feature_image }}" alt="">
#endif
</td>
<td>
/*product name*/
<a href="{{url('/product',$product->id)}}">
{{$product->title}}
</a>
</td>
</tr>
#empty
<tr>
<td colspan="6">
<h3>Your WishList Is Empty</h3>
</td>
</tr>
#endforelse
<tr>
<td colspan="4">
{{$language->continue_shopping}}
</td>
</tr>
</tbody>
I am not sure if you meant to use $product->title as a part of the wishlist object or the product query you have created on the template. If you meant the product query then you will include the rest of the code in the if condition and use $prod->title
Related
I am new to using Laravel and I am currently trying to pass a variable from the index blade file to the controller in order to bring up all apparatus type fields associated with a specific apparatus code id.
I am able to access the apparatus type fields associated with the apparatus code id when I include a specific number which correlates to that apparatus code id (eg. inputting 2 brings up the apparatus type details for the apparatus code id 2, inputting 3 brings up the apparatus type details for the apparatus code id 3 etc).
However when I have tried to pass a variable relating to each individual apparatus code id using a for loop in the controller, the apparatus type loop appears to not be accessed and does not return any information. I will include snippets of my current code for clarity.
Any help or guidance with this issue would be greatly appreciated!
Controller file
public function index()
{
$apparatusCodes = ApparatusCodes::get();
foreach ($apparatusCodes as $apparatusCode){
$apparatusTypes = ApparatusTypes::where('apparatus_code_id', $apparatusCode->id)->get();
}
return view('apparatus_codes.index', compact('apparatusCodes', 'apparatusTypes'));
}
Index.blade file
<table id="datatable" class="stripe hover dt-responsive display nowrap" style="width:100%; padding-top: 1em; padding-bottom: 1em;">
<thead>
<tr>
<th>ID</th>
<th >Apparatus Code</th>
<th>Description</th>
<th>Rent</th>
<th></th>
</tr>
</thead>
<!--start of for loop-->
#foreach ($apparatusCodes as $apparatusCode)
<tbody>
<tr data-toggle="collapse" id="table{{ $apparatusCode->id}}" data-target=".table{{ $apparatusCode->id}}">
<td> {{ $apparatusCode->id}} </td>
<td>{{ $apparatusCode->apparatus_code}} </td>
<td> {{ $apparatusCode->description}}</td>
<td> {{ $apparatusCode->rent}}</td>
<td #click="isOpen = !isOpen" class="main-bg"><img class="mb-1 duration-300 h-6 w-6" :class="{'transform rotate-180' : isOpen}"
src="../img/Icon-arrow-dropdown-white.png" alt="arrow down">
</td>
<td><img class="mb-1 duration-300 h-6 w-6" :class="{'transform rotate-180' : isOpen}"
src="../img/edit-icon.svg" alt="Edit Icon">
</td>
</tr>
<tr x-show.transition.duration.300ms.origin.bottom="isOpen" x-cloak #click.away="isOpen = false" class="collapse table{{ $apparatusCode->id}}">
<td colspan="999">
<div>
<table id="datatable" class="table table-striped">
<thead>
<tr>
<th>Apparatus Code </th>
<th>Apparatus Type</th>
<th>Compensation </th>
<th>Created On </th>
<th>Created By </th>
<th></th>
#foreach ($apparatusTypes as $apparatusType)
</thead>
<tbody>
<tr>
<td>{{ $apparatusCode->apparatus_code}}</td>
<td>{{ $apparatusType->apparatus_type }}</td>
<td>{{ $apparatusType->compensation }}</td>
<td>{{ $apparatusType->created_at }}</td>
<td>{{ $apparatusType->users->name}}</td>
<td> <button type="button" class="btn btn-default btn-edit js-edit"><img class="mb-1 duration-300 ml-4 inset-0 h-6 w-6" src="/../../img/Icon-edit-gray.png" alt="edit"></button></td>
</tr>
#endforeach
</table>
</tr>
</tr>
</td>
</tbody>
#endforeach
</table>
If you have the relationships setup you could eager load the ApparatusTypes for each ApparatusCode:
$apparatusCodes = ApparatusCodes::with('appartusTypes')->get();
Then in the view you could iterate the appartusTypes of each ApparatusCode:
#foreach ($apparatusCodes as $apparatusCode)
...
#foreach ($apparatusCode->apparatusTypes as $type)
...
#endforeach
...
#endforeach
Without using the relationships you could get all the ApparatusTypes for the ApparatusCodes and then group them by apparatus_code_id:
$codes = ApparatusCodes::get();
$types = ApparatusTypes::whereIn('apparatus_code_id', $codes->pluck('id'))
->get()
->groupBy('apparatus_code_id');
Then in the view you could iterate the group that corresponds to the current Code's apparatus_code_id:
#foreach ($codes as $code)
...
#foreach ($types->get($code->id, []) as $type)
...
#endforeach
...
#endforeach
Laravel 8.x Docs - Eloquent - Relationships - Eeager Loading with
Laravel 8.x Docs - Collections - Available Methods - pluck
Laravel 8.x Docs - Collections - Available Methods - groupBy
I have a problem with #foreach in my blade.php. When I insert movie with multiple categories my other moves to the right. Here is how it looks in browser
and here is code in blade
#if($movies)
<div class="row">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Categories</th>
<th>Actors</th>
</tr>
</thead>
<tbody>
#foreach($movies as $movie)
<tr>
<td>{{$movie->name}}</td>
#foreach($movie->categories as $category)
<td>{{$category->category_name}}</td>
#endforeach
#foreach($movie->actors as $actor)
<td>{{$actor->actor_name}}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
#endif
Your issue lies here:
#foreach($movie->categories as $category)
<td>{{$category->category_name}}</td>
#endforeach
You are saying that for each category, it should insert a new <td> into you table. This will of course skew the content since your table expects a set number of columns.
Instead, you should move your foreach statement inside the <td></td>
<td>
#foreach($movie->categories as $category)
{{$category->category_name}}
#endforeach
</td>
This will print the links for each category in the same column.
Help me again.
I have this in my model
public function quantity()
{
return $this->hasMany('App\Productquantity', 'prod_id', 'id');
}
and I want to get the branch name of each product quantity so the productquantity table has field of branch_id.
How can I display the branch name in branch table?
#forelse($Productquantity->quantity as $dataQuantity)
<tr class="item{{$dataQuantity->id}}">
<td align="center" style="text-align:center" width="100px"> <img src="{{ asset('productimg') }}/{{$dataQuantity->pic}}" width="50px"/> </td>
<td style="width:100px;"> {{$Productquantity->product_name}}</td>
<td>{{$dataQuantity->quantity}}</td>
<td>{{$dataQuantity->price}}</td>
<td>{{$dataQuantity->branch_id}}</td>
<td style="width:100px;" class="td-actions"> </i> Add Stocks </td>
</tr>
#empty
<tr>
<td colspan="5"> <em>No Data</em></td>
</tr>
#endforelse
this {{$dataQuantity->branch_id}} should be displayed the name instead of the branch Id.. Please help.
I would say you need a relationship on your Productquantity model to your branch.
Something like
public function branch()
{
$this->hasOne('App\Branch', 'branch_id');
}
and then you could do this:
$dataQuantity->branch->branch_name
I am having the problem in embeding images, but for the icon that is working fine and display the icon in mail, but for inside the foreach I am having some problem in displaying the images.
This is the shopping cart details I am trying to send for the customer email address! please help me to fix this !
#foreach($products as $item)
<?php $i++; ?>
<tr>
<td align="center">
**<img src="{{ $message->embed('img/tempEng/{!!$item->imgPath]!!}') }}" style="padding:0px; margin:0px" />**
</td>
<td align="center">
{{$item['name']}}
</td>
<td>
<td align="center">{{ $item['quantity'] }}</td>
<?php $quantity = $quantity + $item['quantity'] ?>
</td>
<td align="center">LKR {{$item['price']}}</td>
<td align="center">{{$item['discount']}}%</td>
<td align="center"><?php $item['price']=$item['price']*$item['quantity'] - ($item['price']*$item['quantity']*$item['discount'])/100.0 ?>LKR {{$item['price']}}</td>
</tr>
<?php $total=$total+$item['price']; ?>
{{--$i++;--}}
#endforeach
Just started to working with laravel and found it really cool framework to work with , I'm working with basic Shopping cart and after i do Add to Cart Option , product values goto another table called "customer_items" along with current user ID,
now when I want to call current product which user already added to cart and show it in a table ,
here is the code
#foreach($shoppingCarts as $items)
<tr>
<td class="name">{{ $items->product_name }}</td>
<td class="price">{{ $items->product_price }}</td>
<td class="total"> <img class="tooltip-test" data-original-title="Remove" src="img/remove.png" alt=""></td>
</tr>
#endforeach
note : {{ $items->product_price }} , usually there is 4, 6 items added by user ,
I want to get total value of product price from each row ,
Thanks in Advance for the help :) !
Used this and worked fine ,
<?php $total = 0; ?>
#foreach($shoppingCarts as $items)
<?php $total += $items->product_price; ?>
<tr>
<td class="name">{{ $items->product_name }}</td>
<td class="price">{{ $items->product_price }}</td>
<td class="total">{{ $total }}<img class="tooltip-test" data-original-title="Remove" src="img/remove.png" alt=""></td>
</tr>
#endforeach
or
#foreach($shoppingCarts as $items)
<tr>
<td class="name">{{ $items->product_name }}</td>
<td class="price">{{ $items->product_price }}</td>
<td class="total">{{ $items->sum('product_price') }}<img class="tooltip-test" data-original-title="Remove" src="img/remove.png" alt=""></td>
</tr>
#endforeach
Thanks everyone for the support :) !
Source : https://laracasts.com/discuss/channels/general-discussion/get-total-value-from-table-rows