Laravel Blade template how to return null instead of ErrorException when trying to get property of non-object

一曲冷凌霜 提交于 2019-12-10 22:54:34

问题


I'm writing some Laravel Blade templates, and I have models that may have null objects. I would very much like to just try and get the object property and if there is an error, just return null.

So instead of having to write this:

@if ($model->child_object_that_may_be_null)
    {{ $model->child_object_that_may_be_null->interesting_property }}
@endif

I could just write this:

{{ $model->child_object_that_may_be_null->interesting_property }}

and if the child object is null, then the result evaluates to null.

I want to only do this with my blade templates, and not everywhere in the application (I'd still like to get the error in the controllers for example).

Is there an easy way to accomplish this? I also really don't want to have to try/catch the exception each time in the blade templates either.

Basically, I'm trying to make the blade templates easier/shorter to write when I don't care that I child is null, if it is null then its non-existent property should just be null also for the rendering portion.


回答1:


You can use array_get as:

{{ array_get($model, 'child_object_that_may_be_null.interesting_property') }}

The array_get function retrieves a value from a deeply nested array using "dot" notation.

Note - It may only work with Laravel's model objects




回答2:


You might check for nullable values in your object and initialize them as stdClass.

$properties = get_object_vars($model);
foreach ($properties as $k => $v) {
    if ($v === null) $model->$k = new stdClass;
}



回答3:


I get bitten by this all the time. I started adding displayAttribute accessors on my objects when I know they'll have this kind of data display. Especially useful when it comes to dates or multiple levels of relationship.

So like if I have a Service model that's related to a Contract, and that contract has a start_date I want to display, I'll add a getDisplayContractStartDateAttribute to my Service model and do the check there. That way I can also choose to have a message displayed if there is no contract. Like so:

public function getDisplayContractStartDateAttribute(){
    if ($this->contract && $this->contract->starts_at){
        return $this->contract->starts_at->format('m/d/Y @ g:i a');
    } else {
        return 'Start date or contract missing';
    }
}

So now anywhere in my Blade templates, I can access $service->displayContractStartDate without fear of throwing that horrible error.

If you're worried about extending your models too much with these non-database-related methods, consider using a Trait to contain all of our displayAttribute methods for a given model.

Or, if you're worried about separation of concerns, you could use a decorator and access it like $service->decorator->contractStartDate(), but I personally think that's too convoluted.



来源:https://stackoverflow.com/questions/41168170/laravel-blade-template-how-to-return-null-instead-of-errorexception-when-trying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!