首页 > 解决方案 > 如何在 Laravel 8 中按用户 ID 显示用户的产品?

问题描述

我只想显示用户帖子,即帖子的所有者,而不是数据库中的所有帖子数据。怎么做?

我已经尝试过了:

这是我的产品控制器:

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

这是我的 Model/User.php 关系:

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

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

}

型号/Product.php:

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

这是我的观点:

<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

我是 Laravel 的新手,在此先感谢。

标签: phplaraveleloquentlaravel-8

解决方案


尝试使用这样的关系(告诉我是否有任何错误):

public function index()
{
    $listproducts['listproducts'] = Auth::user()->products()->get(); // This will get you all products related to that user
    return view('pages.products.index')->with($listproducts);
}

推荐阅读