【问题标题】:How to retrieve all users with and without posts?如何检索所有有和没有帖子的用户?
【发布时间】:2019-05-01 18:34:05
【问题描述】:

我正在设置一个新视图,以显示所有用户名及其在管理页面的标题帖子。我有 4 个用户,其中 2 个(管理员和大卫)提交了帖子,其中 2 个没有帖子,所以我可以在输出中制作下面的图像,你可以看到我没有 2 个没有帖子的用户,所以我怎么能有他们在下图中和标题栏中对于没有帖子的用户有“没有帖子”

电流输出

视图/刀片

@if ( $user->role_id === '1')
    @foreach ($allposts as $post)
        <tr>
            <th class="font-parsibit">{{ $post->title }}</th>
            <th class="font-parsibit">{{ $post->user->name }}</th>
            <th class="text-center">
                <a href="/posts/{{ $post->id }}" class="btn btn-success text-light">Display</a>
            </th>
            <th class="text-center">
                {!! Form::open(['action' => ['PostsController@destroy', $post->id], 'method' => 'POST', 'class' => 'mx-auto']) !!}
                {!! Form::hidden('_method','DELETE') !!}
                {!! Form::submit('Delete', ['class'=>'btn text-light btn-danger']) !!}
                {!! Form::close() !!}
            </th>
            <th class="text-center">
                <a href="/posts/{{ $post->id }}/edit/{{ $post->noo }}" class="btn btn-primary text-light">Edit</a>
            </th>
        </tr>
    @endforeach
@endif

控制器

public function index()
{
    $allposts = Post::with('user')->get();
    $bgvar = 'dashboard';

    return view('dashboard', compact('bgvar'), compact('allposts'));
}

上面的代码可以正常工作,但正如我所说,显示提交帖子的用户名及其标题。我希望输出向我显示所有用户及其标题帖子,包括提交帖子和未提交帖子的用户。对于没有提交任何帖子的用户,我想在标题栏中显示“没有帖子”。

【问题讨论】:

  • 如果你想显示所有用户,即使他们没有帖子,你应该拉用户,而不是帖子。然后你可以用 $user->post 填充你的视图
  • 如果您看到我附上的图片:例如 david 是一个有 3 个帖子的用户,所以如果我让所有用户查看我无法制作类似图片的内容,请完整解释我回答谢谢

标签: laravel view model controller


【解决方案1】:

进行正确的加入将获得所有用户,包括那些没有任何帖子的用户,因此帖子可能具有 NULL id:

控制器

public function index()
{
    $allPosts = Post::selectRaw("posts.id, users.name as user_name, CASE WHEN posts.id IS NULL THEN 'No Post' ELSE posts.title END AS title")
    ->rightJoin('users', 'posts.user_id', '=', 'users.id')
    ->where('users.role_id', 1)
    ->get();

    return view('dashboard', compact('bgvar'), compact('allPost'));
}

查看

@foreach ($allPosts as $post)
    <tr>
        <th class="font-parsibit">{{ $post->title }}</th>
        <th class="font-parsibit">{{ $post->user_name }}</th>
        <th class="text-center">
        @if($post->id)
            <a href="/posts/{{ $post->id }}" class="btn btn-success text-light">Display</a>
        @endif       
        </th>
        <th class="text-center">
        @if($post->id)
            {!! Form::open(['action' => ['PostsController@destroy', $post->id], 'method' => 'POST', 'class' => 'mx-auto']) !!}
            {!! Form::hidden('_method','DELETE') !!}
            {!! Form::submit('Delete', ['class'=>'btn text-light btn-danger']) !!}
            {!! Form::close() !!}
        @endif
        </th>
        <th class="text-center">
        @if($post->id)
            <a href="/posts/{{ $post->id }}/edit/{{ $post->noo }}" class="btn btn-primary text-light">Edit</a>
        @endif  
        </th>
        </tr>
    @endforeach

【讨论】:

  • 我们不可能有post但我们没有id!什么是用户名?我的数据库发布表中没有用户名
  • 我已经更新了我的答案,包括视图和控制器。希望这能回答你的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-03
  • 2023-04-06
相关资源
最近更新 更多