Sunday, March 19, 2017

How to improve Laravel code using with operator?

In my request to DB I use Laravel's models and function with like:

$user = User::with("city")->get();

Problem is that if to do the following in template:



It will work only then if user specified city, so then value exists in database table. Otherwise it returns an error:

How to get rid of superfluous checks like:

@if(isset($user->city()->name))
   
@endif

This is awfully!



via Darama

Advertisement