Laravel 如何从'TABLE'中创建SELECT*,其中user_id=id;在拉威尔8
我想创建一个页面,可以显示用户id=用户id登录的数据 这是我从表中选择所有数据的控制器,我想用相应的登录用户id对其进行过滤Laravel 如何从'TABLE'中创建SELECT*,其中user_id=id;在拉威尔8,laravel,laravel-8,Laravel,Laravel 8,我想创建一个页面,可以显示用户id=用户id登录的数据 这是我从表中选择所有数据的控制器,我想用相应的登录用户id对其进行过滤 public function staffHome() { $posts = Post::latest()->paginate(5); return view('staffHome', compact('posts')) ->with('i', (request()->input('pag
public function staffHome()
{
$posts = Post::latest()->paginate(5);
return view('staffHome', compact('posts'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
这是我的观点
@foreach ($posts as $post)
<tr>
<td class="text-center">{{ ++$i }}</td>
<td>{{ $post->title }}</td>
<td class="text-center">
<form action="{{ route('posts.destroy',$post->id) }}" method="POST">
<a class="btn btn-info btn-sm" href="{{ route('posts.show',$post->id) }}">Show</a>
<a class="btn btn-primary btn-sm" href="{{ route('posts.edit',$post->id) }}">Edit</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Apakah Anda yakin ingin menghapus data ini?')">Delete</button>
</form>
</td>
</tr>
@endforeach
@foreach($posts as$post)
{{++$i}
{{$post->title}
@csrf
@方法('DELETE')
删除
@endforeach
提前谢谢
$posts = Post::where('user_id', $user_id)->latest()->paginate(5);
请看:你可以这样做
public function staffHome()
{
$posts = Post::where('user_id', Auth::id())->latest()->paginate(5);
return view('staffHome', compact('posts'))->with('i', (request()->input('page', 1) - 1) * 5);
}
$posts=Post::where('user_id',Auth::id())->latest()->paginate(5)代码>
其中,Auth::id()
当前已登录到用户id中
还是有关系
Auth::user()->posts()->->latest()->paginate(5)代码>您的代码应该是这样的
public function staffHome()
{
$posts = Post::where('user_id', Auth::id())->latest()->paginate(5);
return view('staffHome', compact('posts'))->with('i', (request()->input('page', 1) - 1) * 5);
}
通过阅读laravel的文档,您可以在其中找到where()函数。。。