我正在建立一个新视图,以显示所有用户名及其在管理员页面上的标题。我有4个用户,其中2个(管理员和david)已提交帖子,其中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'));
}
上面的代码可以正常工作,但是正如我所说的,以显示提交带有标题的帖子的用户名。我希望输出显示给我所有具有标题帖子的用户,包括有提交帖子的用户和没有帖子的用户。对于没有提交任何帖子的用户,我想在标题栏中显示“无帖子”。
答案 0 :(得分: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
答案 1 :(得分:0)
您可以尝试使用data(starwars)
df <- starwars
df$species <- NA
library(DataExplorer)
plot_missing_2(df)
。喜欢 -
ternary operator