Laravel - Trying to get property of non-object on ::first()
Asked Answered
W

4

6

Ok so I'm getting Trying to get property of non-object when I try and get the data from the DB using $settings = AdminSettings::first();

here is the controller code

    <?php

namespace App\Http\Controllers\AdminSettings;

use App\AdminSettings\AdminSettings;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AdminSettingsController extends Controller
{
    public function index()
    {
        $settings = AdminSettings::first();

        return view('admins.settings.settings', compact('settings'));
    }
}

here is the modal code

    <?php

namespace App\AdminSettings;

use Illuminate\Database\Eloquent\Model;

class AdminSettings extends Model
{
    protected $table = 'site_settings';
    protected $fillable = [
        'site_title', 'site_url', 'email_from', 'email_to', 'timezone', 'date_format', 'time_format',
    ];
}

here I'm trying to put the site_title into the input but I get Trying to get property of non-object

<div class="form-group">
                        <label for="site_title" class="form-label">Site Title</label>
                        <input type="text" class="form-control" name="site_title" id="site_title" value="{{ $settings->site_title }}"/>
                    </div>

when I try to dd($settings); i get null

Winnipegosis answered 5/1, 2018 at 17:18 Comment(6)
Are you sure, there is a record in the table?Mignonmignonette
@commonsense no I made sure the table is empty because the user could add the settings once they visit the page and click save changes and then add the settings to the DB I'm just trying to display the `{{ $settings->tsite_title }} once the user adds the dataWinnipegosis
If the table is empty, first() will be null, so attempting to access data on it won't work. Simple as that. Check for the existence of a setting record before attempting to use it.Softboiled
@Softboiled oh ok because i remember doing something like this beforeWinnipegosis
I would use a seeder which creates the first record with basic data.Mignonmignonette
I think your table does not contain dataParvati
N
10

You've said the table is empty, so make settings object optional:

{{ optional($settings)->site_title }}
Nimocks answered 5/1, 2018 at 17:26 Comment(6)
I didn't know optional() helper existed. Sweet!Softboiled
I'm getting Call to undefined function optional() Winnipegosis
@$settings->site_title use it that way insteadParvati
@Winnipegosis it's available in 5.5. If you're on 5.4 and below, you can do this {{ $settings ? $settings->site_title : '' }}Nimocks
Ok thanks, I just saw that it was only for 5.5 and i'm still on 5.4Winnipegosis
Thanks a lot @AlexeyMezenin this 'optional()' really saved me a lot of time.Bromo
N
1

You can use or operator too:

{{ $settings->site_title or ''  }}
Northernmost answered 5/1, 2018 at 17:23 Comment(0)
C
0

use find() to get a single collection

$settings = AdminSettings::find($id);

You can use first too.

 $settings = AdminSettings::where('id',$id)->first();

And if you need entire collection then use all()

$settings = AdminSettings::all();

In your view, make a simple check with-

<input type="text" class="form-control" name="site_title" id="site_title" value="<?php echo ($settings)?$settings->site_title:'' >"/>
Candleberry answered 5/1, 2018 at 17:22 Comment(1)
First is going to take one collection whether you set a where or notParvati
E
0

use empty to check object:

@if(!empty($settings->site_title))
  {{ $settings->site_title }}
@else
  "Nothing"
@endif
Elodea answered 5/1, 2018 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.