php - 如何在 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> 100
<i class="fas fa-grin-hearts"></i> 15
<i class="fas fa-paper-plane"></i> 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 的新手,在此先感谢。
解决方案
尝试使用这样的关系(告诉我是否有任何错误):
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);
}
推荐阅读
- java - Java TypedQuery 比直接查询数据库慢得多
- python - 如何禁用python控制台
- linked-list - 将一个节点插入到单个链表的第 n 个位置
- php - 动态解析变量到array_map
- javascript - instanceof 接受一个字符串作为我的 Type 类 Javascript 的实例
- python - 数据时间第一天PANDAS的动作
- sql - 具有行和列级别的 XML 到 SQL Server 表
- symfony - 如何使用 Doctrine 和 createQueryBuilder 从视图中获取数据?
- python - Python - collections.OrderedDict() 没有正确排序字典
- c++ - Javascript 找不到用 emscripten 编译的导出 WASM 函数