-1

ErrorException (E_ERROR) Trying to get property 'title' of non-object (View: D:\xampp\htdocs\xampp\practise\freecode\resources\views\profiles\edit.blade.php)

After migration, I face that problem.

edit.blade.file......

<input id="title"
  type="text"
  class="form-control @error('title') is-invalid @enderror" 
  name="title" 
  value="{{ old('title') ?? $user->profile->title }}" required 
  autocomplete="title" autofocus>

 @error('title')
    <span class="invalid-feedback" role="alert">
        <strong>{{ $message }}</strong>
    </span>
@enderror
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

2 Answers2

0

You can beat it clean with double ??

<input id="title"
  type="text"
  class="form-control @error('title') is-invalid @enderror" 
  name="title" 
  value="{{ old('title') ?? ($user->profile->title ?? '') }}" required 
  autocomplete="title" autofocus>

I have checked ?? for $user->profile->title too.

Rahul
  • 18,271
  • 7
  • 41
  • 60
0

I do this and it's work.

user.php file.....

protected static function boot()
{
    parent:: boot(); 

        static::created(function($user){
            $user->profile()->create([
                'title'=>$user->username,
            ]);
        });
}