Kenneth
Kenneth

Reputation: 2993

Laravel display a relationship data with soft delete

I want to display the name of the user in the blade using Eloquent: Relationships. My code's can display data using relationship but if the data is soft deleted it gives me a error.

Here's my code.

// History Model//

public function user()
{
    return $this->belongsTo(User::class);
}

// User model //

public function history()
{
    return $this->hasMany(History::class);
}

// Controller //

public function index()
{
    $histories = History::find(3);
    return view('booking.backend.content.history.index', compact('histories'));
}

// index.blade //

{{$history->user->name}}

Upvotes: 3

Views: 4598

Answers (1)

Kenneth
Kenneth

Reputation: 2993

I already fixed it. In my model history I add withtrashed.

public function history()
{
    return $this->hasMany(History::class)->withTrashed();
}

Upvotes: 6

Related Questions