dongya767979565 2014-02-19 18:00
浏览 38
已采纳

Laravel Parent被软删除,孩子仍然试图显示导致错误?

In Laravel 4 I am having trouble calling a Related model when the Parent model is Soft Deleted...

In my Controller I have this which gets a Campaign record....

$campaign = Campaign::find($id);

In my View for this controller method, I call a related model like this...

echo $campaign->user->profile->full_name;

User is related to Camapign and Profile is related to User

My problem is, if a user is Soft Deleted, when I call echo $campaign->user->profile->full_name;

I get...

ErrorException  
Trying to get property of non-object (View: app/views/campaign/mapView.blade.php)

What I would like to do is instead show an error message that this users campaign is disabled/deleted since the User is soft deleted.

  • 写回答

1条回答 默认 最新

  • douchixu3686 2014-02-19 18:52
    关注

    You can detect if a user has been soft deleted and do something like showing a message or redirect the application

    if ($campaign->user->trashed())
    {
        //show error message or redirect the app with something like Session::flash('error', 'There was an error campaign is disabled ');
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?