M
M
mrFlyer2016-07-22 19:15:36
Laravel
mrFlyer, 2016-07-22 19:15:36

How to set default link in Laravel 5 if not found?

Greetings.
There is a model, the model has a connection.
In the template, I pull the method from the associated model:
$model->relation->method()
If there is no connection, which is possible, a logical error is thrown out that `method()` does not exist.
How to resolve this optimally, so as not to check for the existence of a connection everywhere in the code? How to slip him a default model?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Semyon Pyatnitsky, 2016-07-23
@snowsem

And how do you assign a default to different links? you will have different models and for each you still need to write conditionally try. They still have to be checked.
If you need to check in View, then like this:

@if (isset($t->callStatusColor)) {{$t->callStatusColor}} @endif

M
mrFlyer, 2016-07-25
@mrFlyer

Did so. I don't know how correct...

public function getSomeAttribute(){
        if (!$some =   $this->getRelation('some')) {
            $some =   new Some();
            $some->setDefaults();
        }
        return $some;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question