How to set where in relation Laravel?

前端 未结 2 944
死守一世寂寞
死守一世寂寞 2021-01-15 16:21

I use the following construction for joining tables: with(\"attachments\", \"offers.publisher\").

public function publisher()
    {
        retu         


        
2条回答
  •  温柔的废话
    2021-01-15 17:09

    The best way would to just have two definitions, publishers for all publishers, and active_publishers for those with status = 1:

    public function publisher()
    {
        return $this->belongsTo("App\User", "user_id", "id");
    }
    
    public function active_publisher()
    {
        return $this->publisher()->where('status', 1);
    }
    

    Use with $object->active_publisher()->get();

提交回复
热议问题