简体   繁体   中英

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)}}"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM