试图获取非对象的属性“标题”

时间:2019-07-10 07:07:39

标签: php laravel

  

ErrorException(E_ERROR)试图获取非对象的属性“ title”   (视图:   D:\ xampp \ htdocs \ xampp \ practise \ freecode \ resources \ views \ profiles \ edit.blade.php)

迁移后,我遇到了这个问题。

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

2 个答案:

答案 0 :(得分:0)

您可以用双??来击败它

<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>

我也检查了??中的$user->profile->title

答案 1 :(得分:0)

我做到了,而且有效。

user.php文件.....

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

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