简体   繁体   中英

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

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

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');
  }

In your Profile model

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

In your Controller

  public function store(Request $request)
  {
       echo $id = auth()->user->profile->user_id; 
       $input = $request->all();
       $input['user_id'] = auth()->id(); // OR auth()->user->profile->user_id;
       Profile::create($input);
       return redirect('/home');
  }

you need to define foreign key and local key in relation in user model as this:

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

I'm guessing your goal is to define a one to one relationship.

For that matter you need to define a hasOne relationship in user model

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

and a belongsTo in profile model

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

and don't forget to put the user_id in your profile table.

check this link for more info

After these steps if you have a logged in (authenticated) user, you could get the user_id field of profile table with this:

use Illuminate\Support\Facades\Auth;


Auth::user()->profile->user_id; 

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