Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 如何从'TABLE'中创建SELECT*,其中user_id=id;在拉威尔8_Laravel_Laravel 8 - Fatal编程技术网

Laravel 如何从'TABLE'中创建SELECT*,其中user_id=id;在拉威尔8

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

我想创建一个页面,可以显示用户id=用户id登录的数据 这是我从表中选择所有数据的控制器,我想用相应的登录用户id对其进行过滤

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()函数。。。