【问题标题】:Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$user_id未定义的属性:Illuminate\Database\Eloquent\Relations\HasOne::$user_id
【发布时间】:2020-07-08 15:57:34
【问题描述】:

我只是laravel的初学者,如果这是一个愚蠢的问题,对不起

这是我的控制器:-

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

  }


}

这是我的模型:-

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

这是我的桌子

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

这是我的用户模型:-

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

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

我收到此错误 "Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$user_id" 我不知道我哪里出错了请帮助我并指导我你可以 谢谢

【问题讨论】:

  • 仅供参考,$gaurded 中的错字。 auth()-&gt;user()-&gt;profile()-&gt;user_id; 应该做什么?
  • auth()-&gt;user()-&gt;profile-&gt;user_id; 而不是 auth()-&gt;user()-&gt;profile()-&gt;user_id;
  • 正如你所说@TsaiKoga 我试过 "auth()->user()->profile->user_id;" 但我得到的错误是这个 "试图获取非对象“的属性'user_id'
  • @AnonymousChatbox 因为配置文件是无,这意味着用户没有配置文件。你为什么不直接使用$id = auth()-&gt;id();
  • 这是我得到的 "SQLSTATE[HY000]: 一般错误:1364 字段 'user_id' 没有默认值" 我也是编码新手所以如果你对我的愚蠢感到生气,我很抱歉

标签: php laravel relationship


【解决方案1】:

你不能在Illuminate\Database\Eloquent\Relations\HasOneprofile()上调用属性user_id

您可以通过auth()-&gt;user()-&gt;profile-&gt;user_id 调用它。

但是,该用户还没有个人资料。需要创建它并在Illuminate\Database\Eloquent\Relations\HasOne上使用create方法,Laravel会自动构建foreign_keyuser_idprofile

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

【讨论】:

  • THAAAAANNKKSS 兄弟......
【解决方案2】:

在您的个人资料模型中

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

在你的控制器中

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

【讨论】:

  • 它显示 "未定义的属性:Illuminate\Auth\AuthManager::$user " 错误
  • 检查更新的答案$input['user_id'] = auth()-&gt;id();
  • 感谢您的回复...我找到了“我没有在其中创建配置文件”的解决方案
  • 请选择这个答案:)
  • 如果你愿意我会的
【解决方案3】:

您需要在用户模型中定义相关的外键和本地键:

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

【讨论】:

  • 它显示 "未定义的属性:Illuminate\Auth\AuthManager::$user" 错误
  • 让我看看你在用户模型和控制器中的代码,我试过了,和我一起工作
  • 我已经在我的问题中添加了 User 模型,并且我已经收到了 create 命令丢失的答案 感谢您的回复
【解决方案4】:

我猜你的目标是定义一对一的关系。

为此,您需要在用户模型中定义 hasOne 关系

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

以及profile模型中的belongsTo

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

并且不要忘记将 user_id 放入您的个人资料表中。

查看此link 了解更多信息

如果您有一个已登录(经过身份验证)的用户,则在这些步骤之后,您可以使用以下内容获取配置文件表的 user_id 字段:

use Illuminate\Support\Facades\Auth;


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

【讨论】:

  • 是的,我已经完成了那种关系,正如你所说,我已将其更改为 "auth()->user->profile->user_id; " 但它仍然显示此错误 "Undefined property: Illuminate\Auth\AuthManager::$user "
  • 好的,我找到了解决方案,您获取有关经过身份验证的用户信息的语法应该是这样的:Auth::user()->Profile->user_id; Auth 是 Illuminate\Support\Facades\Auth
  • 感谢您的回复但我找到了解决方案,我没有创建配置文件,这就是我无法传递数据的原因再次感谢
猜你喜欢
  • 2021-11-25
  • 2018-11-22
  • 2018-11-19
  • 2020-05-04
  • 2021-09-23
  • 1970-01-01
  • 2020-11-23
  • 2019-08-12
  • 2020-01-26
相关资源
最近更新 更多