筛选自己的话题为什么不用模型关联,使用 user_id 筛选呢?
1、筛选自己的话题应该是模型关联查询,
2、根据测试方法名称,这里要做的应该是通过任意用户名筛选文章,并非筛选自己的文章。
public function a_user_can_filter_threads_by_any_username()
{
$this->signIn(create('App\User',['name' => 'NoNo1']));
$threadByNoNo1 = create('App\Thread',['user_id' => auth()->id()]);
$threadNotByNoNo1 = create('App\Thread');
$this->get('threads?by=NoNo1')
->assertSee($threadByNoNo1->title)
->assertDontSee($threadNotByNoNo1->title);
}
控制器方法也是根据任意用户名查询用户文章(非授权下)
public function index(Channel $channel)
{
if($channel->exists){
$threads = $channel->threads()->latest();
}else{
$threads = Thread::latest();
}
if($username = request('by')){
$user = \App\User::where('name',$username)->firstOrFail();
$threads->where('user_id',$user->id);
}
$threads = $threads->get();
return view('threads.index',compact('threads'));
}
所以这里的测试方法是不是应该改成:
public function a_user_can_filter_threads_by_any_username()
{
$user = factory('App\User')->create(['name' => 'NoNo1']);
$threadByNoNo1 = create('App\Thread',['user_id' => $user->id]);
$threadNotByNoNo1 = create('App\Thread');
$this->get('threads?by=NoNo1')
->assertSee($threadByNoNo1->title)
->assertDontSee($threadNotByNoNo1->title);
}