Jess McKenzie
Jess McKenzie

Reputation: 8385

Laravel isset with array value

I am having an issue - well because the value I am trying to pull from the DB does not exist yet.

Is there away that I check if its isset? Is there any better way that I can get my value from the db to save on double code?

Controller:

    $siteSocialFacebook      = socialSettings::where('socialName','=','facebook')->get();
    $siteFacebook            = $siteSocialFacebook[0]['socialLink'];

Blade:

value="{{ old('facebook', @$siteFacebook)}}"

Upvotes: 0

Views: 1897

Answers (1)

Josh
Josh

Reputation: 3288

If you will only ever expect one result, use first() instead of get() and skip the array. You can pass it into the Blade template like this:

return view('blade', [
    'siteFacebook' => $siteSocialFacebook['socialLink'] ?: null,
]);

This will prevent any issues with undefined parameters.

Edit: I just realized you're treating models as arrays. You can do this too:

return view('blade', [
    'siteFacebook' => $siteSocialFacebook->socialLink,
]);

That handles it for you.

Upvotes: 1

Related Questions