4
votes

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

4
fyi, typo in $gaurded. What is auth()->user()->profile()->user_id; supposed to do?brombeer
auth()->user()->profile->user_id; instead of auth()->user()->profile()->user_id;TsaiKoga
As you said @TsaiKoga i tried "auth()->user()->profile->user_id;" but the error i got is this "Trying to get property 'user_id' of non-object "Anonymous Chatbox
@AnonymousChatbox because the profile is none, it means the user has no profile. Why don't u just use $id = auth()->id();TsaiKoga
This is what i got "SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value" i'm new to coding as well so i'm sorry if you are pissed by my stupidityAnonymous Chatbox

4 Answers

2
votes

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

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

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");
}
0
votes

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;