Ad

Laravel 5 Check If Object Exists And Use If True

- 1 answer

Instead of having to write:

{{$x = $data->where('colour_id', 1)->where('size_id', 1)->where('product_id', 1)->first() ? $data->where('colour_id', 1)->where('size_id', 1)->where('product_id', 1)->first()->quantity : 0}}

Is there a much cleaner way?

Ad

Answer

You could use Blade's or syntax:

{{ $data->where('colour_id', 1)->where('size_id', 1)->where('product_id', 1)->first->quantity or 0 }}

Not sure why you're assigning it to $x so I removed it, just readd if you do need it.

Personally, though, I'd make it a method on whatever model the $data object is an instance of. :)

Ad
source: stackoverflow.com
Ad