【问题标题】:Laravel cannot get relationship data of a selected profileLaravel 无法获取所选配置文件的关系数据
【发布时间】:2021-02-03 23:29:13
【问题描述】:

我刚刚为用户创建了个人资料,并希望显示与登录用户或其他选定用户相关的教育详细信息。

为此,我为用户创建了一个教育模型,并赋予了双方适当的关系。我无法从登录用户或其他用户的教育表中获取任何数据。我在刀片文件中使用了 foreach 标签。请修改我的代码。谢谢。

教育模式

class Education extends Model
{
    use HasFactory;

    protected $primaryKey = 'education_id';

    public function myusers()
    {
        return $this->belongsTo('App\Models\User','user_id','education_id');
    }

}

用户模型

  public function myeducation()
    {
        return $this->hasMany('App\Models\Education','education_id','user_id');
    }

配置文件控制器

   public function index()
    {

         $user = Auth::user();


          return view('candidate.profile',['user'=>$user,]);
    }

刀片文件

 @foreach ($user->myeducation as $education)
                    <div>
                       {{  $education->school }}
                    </div>

                @endforeach

教育和用户表结构

**Education Table**

{
        Schema::create('education', function (Blueprint $table) {
            $table->bigIncrements('education_id');
            $table->bigInteger('user_id');
            $table->string('school');
            $table->string('degree');
            $table->string('fieldOfStudy');
            $table->date('startDate');
            $table->date('endDate');
            $table->string('grade');
            $table->string('activities');
            $table->string('description');
            $table->timestamps();
        });
    }

用户表。

        $table->increments('user_id');
        $table->bigInteger('role_id');
        $table->bigInteger('membership_id')->nullable();
        $table->string('firstname');
        $table->string('lastname');

没有错误信息,只是空白

Table entries

 DB::table('education')
            'user_id' => '2',
            'school' => 'University of Bedfordshire',
            'degree' => 'MBA',
            
        ]);



 DB::table('users')->insert([
            'user_id' => '1',
            'role_id' => '1',
            'firstname' => 'Mohammed',
            'lastname' => 'Sabeel',
            .......
        ]);

        DB::table('users')
            ' user_id' => '2'
            'role_id' => '2',
            'firstname' => 'zahida',
            'lastname' => 'sabeel',
            .......
        ]);

【问题讨论】:

  • 你的桌子是什么样子的?对于关系而言,这些键似乎有点奇怪。你有user_ideducation_id 听起来像是两个不同的值。键应代表两个表中的相同值(例如模型变量$user-&gt;id === $education-&gt;user_id)。如果该陈述已经成立,那么您无需手动定义键,因为 Laravel 会为您完成。
  • 你好@matticustard。 user>user_id 和 Education>user_id 具有相同的值。并且密钥已经手动输入。请检查我的问题所附的表格结构。谢谢

标签: laravel eloquent relationship


【解决方案1】:

问题在于你的关系第二个和第三个参数。您以错误的方式传递密钥。

Education模型中使用类似代码

public function myUser()
{
    return $this->belongsTo('App\Models\User', 'user_id');
}

如果你使用关系的主键,你不需要传递第三个参数。虽然您可以传递第三个参数来定义用于连接表的列

public function myUser()
{
    return $this->belongsTo('App\Models\User', 'user_id', 'user_id');
    // second argument user_id is from your education model while the third argument that is user_id is the primary key of your user model
    // i have used singular name for the relationship name with camel case
}

现在在User 模型中

public function myEducations()
{
    return $this->hasMany('App\Models\Education', 'user_id');
    // user_id is the user_id of education model
    // and this is a has many relation i used plural form
}

laravel doc了解更多关于关系的信息

【讨论】:

  • 非常感谢。这对我有帮助。现在我可以看到结果了。
【解决方案2】:

在我们开始之前,请确保您拥有与该登录用户相关联的教育。

尝试急切地加载您的关系。有时这对我有用。
在您的个人资料控制器中,

public function index()
{

     $user = Auth::user()->load('myeducation');


      return view('candidate.profile',['user'=>$user,]);
}

即使它不起作用,请分享您的表格结构和表格条目。以便我们可以清楚地检查。

【讨论】:

  • 它不起作用。请检查表结构
猜你喜欢
  • 2020-06-03
  • 2021-12-10
  • 2021-02-09
  • 2017-07-07
  • 2021-12-07
  • 2013-10-04
  • 1970-01-01
  • 2014-03-26
  • 2019-07-12
相关资源
最近更新 更多