Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$user_id

Anonymous Chatbox

I'm just a beginner at laravel, Sorry if it's a stupid question

Here's my controller :-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Profile;
use Auth;



class ProfilesController extends Controller
{

  public function _construct()
  {
    $this->middleware('auth');
  }

  public function create()
  {
    return view('bio');
  }

  public function store(Request $request)
  {
  auth()->user()->profile()->user_id;
    // create Bio
    $profile = new Profile;

    $profile->bio = $request->input('bio');
    $profile->save();
    return redirect('/home');

  }


}

Here's My Model:-

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
  protected $gaurded = [];
protected $fillable = ['user_id', 'bio'];

    public function user()
    {
      return $this->belongsTo(User::class);
    }
}

Here Are My Tables

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
  protected $gaurded = [];
protected $fillable = ['user_id', 'bio'];

    public function user()
    {
      return $this->belongsTo(User::class);
    }
}

Here's My User Model:-

  public function profile()
    {
      return $this->hasOne(Profile::class);
    }

    public function posts()
    {
      return $this->hasMany(Posts::class);
    }

I'm getting this error "Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$user_id" I don't know where i went wrong please help me and guide me if u can Thanks

TsaiKoga

You cannot call the attribute user_id on Illuminate\Database\Eloquent\Relations\HasOne profile(),

you can call it by auth()->user()->profile->user_id.

However, the user has no profile yet. Need to create it and use create method on Illuminate\Database\Eloquent\Relations\HasOne, Laravel will automatically build the foreign_key user_id to profile.

  public function store(Request $request)
  {
      auth()->user()->profile()->create([
         'bio' => $request->input('bio')
      ]);
      return redirect('/home');
  }

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

Undefined property: user::$load

Valor inteiro incorreto: 'undefined' para a coluna 'user_id'

Laravel 8 : Attempt to read property "user_id" on int

Gatsby TypeError - Cannot read property 'id' of undefined

react TypeError: Cannot read property 'id' of undefined

Angular Material TypeError: Cannot read property 'id' of undefined

"Cannot read property 'id' of undefined" when parsing an array declared in a factory

Cannot read property 'id' of undefined when posting to local API

Array findIndex() function - Cannot read property 'id' of undefined

array of objects: Cannot read property 'id' of undefined inside onchange event

AngularJS + Fullcalendar send error TypeError: Cannot read property '__id' of undefined

MongoError: TypeError: Cannot read property 'id' of undefined, while using $or

Logback property value is undefined

Unable to get property then of undefined?

Cannot read property 'then' of undefined

Undefined property: stdClass::$Blanch

Angular httpClient cannot read property undefined of undefined

TypeError: Cannot destructure property 'position' of 'undefined' as it is undefined

Cannot read property 'handleChange' of undefined

Cannot read property 'units' of undefined

Cannot read property substring of undefined

Vuex Cannot read property of undefined

Cannot read property 'filter' of undefined

Nodemailer: Cannot read property 'then' of undefined

Cannot read property 'userNameInput' of undefined

Cannot read property 'dasherize' of undefined

Undefined property: App\Cart::$totalPrice

Cannot read property 'module' of undefined

Angular cannot set property of undefined