John Davidson

php - How to display user's products by user id in Laravel 8?

0 comments
Message:


I want to display only user posts, which is the owner of the post, not all post data from the database. How to do it?


I have already tried this :


This is my Product Controller :


public function index()
{
$listproducts['listproducts'] = Product::where('user_id','=',Auth::user()->id)->get();
return view('pages.products.index')->with($listproducts);
}

This is my Model/User.php relationship :


public function products(): \Illuminate\Database\Eloquent\Relations\HasMany
{

return $this->hasMany(Product::class,'user_id','id');

}

Models/Product.php :


public function users(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class,'user_id', 'id');
}

this is my view :


<tbody>
@if ($listproducts->count())
@foreach($listproducts as $listproduct)
<tr id="pid{{$listproduct->id}}">
<td style="width: 30%" class="td p-0 pl-4 text-center">
<div class="flex-column-product custom-control-child">
<label>
<input name="ids"
id="checkSingle"
type="checkbox"
class="checkSingle rounded border-gray-300 text-indigo-600 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"
value="{{ $listproduct -> id }}">
</label>
<img style="display: block; margin-left: auto; margin-right: auto;" class="img c-thumb" alt="image"
data-toggle="tooltip" title="" src="{{ asset("storage/gambarproduct")."/".$listproduct->gambar }}">
</div>
<div>
<p style="margin-bottom: 0; margin-top: 1px; margin-left: 30px;" class="mr-2">
<i class="fas fa-eye"></i>&nbsp;&nbsp;100&nbsp;&nbsp;
<i class="fas fa-grin-hearts"></i>&nbsp;&nbsp;15&nbsp;&nbsp;
<i class="fas fa-paper-plane"></i>&nbsp;&nbsp;8
</p>
</div>
<div style="margin-bottom: 0; margin-top: 1px; margin-left: 30px;">
<p class="text-judul-product">{{ $listproduct->nama }}</p>
</div>
</td>
<td class="text-center">
<div class="badge badge-success">Bekas</div>
</td>
<td class="text-center">
<div class="badge badge-success">Elektronika</div>
</td>
<td class="text-center">
{{ "Rp.".number_format($listproduct->harga) }}
</td>
<td class="text-center">
<div class="badge badge-success">100</div>
</td>
<td class="text-center">
<div class="badge badge-success">{{ "#".number_format($listproduct->id) }}</div>
</td>
<td class="text-center">
<a href="#" class="btn btn-icon icon-left btn-primary"><i class="far fa-edit"></i> Edit</a>
<a href="#" class="btn btn-danger delete"
data-toggle="modal"
data-target="#deleteModal"
data-productid="{{$listproduct->id}}"><i class="fas fa-times"></i> Delete
</a>
</td>
</tr>
@endforeach
@else
<tr>
<td colspan="7" class="text-center text-white py-32 bg-red-400">Anda belum mempunyai iklan.</td>
</tr>
@endif

I am new to Laravel , thanks in advance.



My Answer:
In your Product Controller, you are already fetching the products of the logged-in user correctly. However, in your view, you need to access the products using the key 'listproducts' instead of 'listproduct' in your foreach loop. Also, make sure to use the correct syntax for accessing the properties of the product object.

Here is the updated code for your view:

html

@if ($listproducts->count())
@foreach($listproducts['listproducts'] as $listproduct)




image data-toggle="tooltip" title="" src="{{ asset("storage/gambarproduct")."/".$listproduct->gambar }}">



  100  
  15  
  8




{{ $listproduct->nama }}





Bekas



Elektronika



{{ "Rp.".number_format($listproduct->harga) }}


100



{{ "#".number_format($listproduct->id) }}



Edit
data-toggle="modal"
data-target="#deleteModal"
data-productid="{{$listproduct->id}}"> Delete



@endforeach
@else

Anda belum mempunyai iklan.

@endif



Make sure to use the correct syntax for accessing the properties of the product object in your view. This should display only the products of the logged-in user on the page.

Rate this post

4 of 5 based on 9605 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us