简体   繁体   English

未定义的属性:Illuminate\\Database\\Eloquent\\Relations\\HasOne::$user_id

[英]Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$user_id

I'm just a beginner at laravel, Sorry if it's a stupid question我只是laravel的初学者,对不起,如果这是一个愚蠢的问题

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我收到此错误“未定义的属性:Illuminate\\Database\\Eloquent\\Relations\\HasOne::$user_id”我不知道我哪里出错了,如果可以,请帮助我并指导我谢谢

You cannot call the attribute user_id on Illuminate\\Database\\Eloquent\\Relations\\HasOne profile() ,您不能在Illuminate\\Database\\Eloquent\\Relations\\HasOne profile()上调用属性user_id

you can call it by auth()->user()->profile->user_id .您可以通过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 .需要创建它并在Illuminate\\Database\\Eloquent\\Relations\\HasOne上使用create方法,Laravel 会自动构建 foreign_key user_idprofile

  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为此,您需要在用户模型中定义一个 hasOne 关系

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.并且不要忘记将 user_id 放在您的配置文件表中。

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:在这些步骤之后,如果您有登录(经过身份验证的)用户,您可以使用以下命令获取配置文件表的 user_id 字段:

use Illuminate\Support\Facades\Auth;


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义的属性:Illuminate\Database\Eloquent\Relations\BelongsTo - Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo Laravel HasMany 关系未定义属性:Illuminate\\Database\\Eloquent\\Relations\\HasMany::$id - Laravel HasMany relationship Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$id Laravel 5.8:未定义的属性:Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany::$usr_id - Laravel 5.8: Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$usr_id Laravel:未定义属性:Illuminate\\Database\\Eloquent\\Relations\\HasMany::$game - Laravel: Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$game 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Relations \\ MorphMany :: $ title - Undefined property: Illuminate\Database\Eloquent\Relations\MorphMany::$title 未定义的属性:Illuminate\\Database\\Eloquent\\Relations\\BelongsTo::$center_name - Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$center_name 未定义的属性:Illuminate\\Database\\Eloquent\\Relations\\BelongsTo::$name laravel 5.4 - Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name laravel 5.4 未定义的属性:Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany::$balance - Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$balance Laravel Eloquent HasOne :: $ id未定义属性 - Laravel Eloquent HasOne::$id Undefined Property Laravel:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ id - Laravel:Undefined property: Illuminate\Database\Eloquent\Collection::$id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM