筛选自己的话题为什么不用模型关联,使用 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);
}
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!