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

withTrashed() can be applied to the query that retrieves the users like so:

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

You can also apply withTrashed() to both the appointment and their users:

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

Alternatively, you can add withTrashed() to the association method to apply it whenever the association is loaded:

public function user() {
  return $this->hasOne('User')->withTrashed();
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)