How to use withTrashed when I'm querying using eager loading?

前端 未结 1 998
你的背包
你的背包 2020-12-29 22:06

I have some tables, one of which is a table named Users where there are soft deleted rows.

I have code like:

$appointments = Appointment         


        
相关标签:
1条回答
  • 2020-12-29 23:03

    I'm not 100% sure this works, as this method is meant for adding constraints to eager-loading, but you may want to try it:

    $appointments = Appointment::with(array('user' => function($query) {
        $query->withTrashed();
    }))->get();
    

    This should apply withTrashed() to the query that grabs the users as opposed to the query which grabs the appointments.

    Oh and just to clarify, you can add withTrashed() to both queries:

    $appointments = Appointment::with(array('user' => function($query) {
        $query->withTrashed();
    }))->withTrashed()->get();
    

    Edit: just thought of an easier method:

    Add withTrashed() to the user() relationship (only do this if you want this to apply EVERY time you call this relationship):

    public function user() {
      return $this->hasOne('User')->withTrashed();
    }
    
    0 讨论(0)
提交回复
热议问题