Answer the question
In order to leave comments, you need to log in
How to get a model property through another model's connection and not get an exception, in the absence of this?
Hi all.
I know that the question is posed very strangely, because it is difficult to describe the problem / scenario in one line: I
want:
$value = $modelA->modelB()->where("foo", "bar")->first()->value;
$modelB = $modelA->modelB()->where("foo", "bar")->first();
if($modelB) {
$value = $modelB->value;
}
Answer the question
In order to leave comments, you need to log in
One way is described at
laraveldaily.com/belongsto-default-models-no-need-...
The second is to use ?? operator
$value = $modelA->modelB()->where("foo", "bar")->first()->value ?? null;
Use try/catch. If you often write like this, then write a function that handles it through try / catch
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question