Laravel无法获取所选配置文件的关系数据

mmmsabeel

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

为此,我为用户创建了一个教育模型,并在双方之间建立了适当的关系。我无法从已登录用户或其他用户的教育表中获取任何数据。我在刀片文件中使用了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',
            .......
        ]);
扎希德·哈桑emon

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

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文档中阅读有关关系的更多信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Laravel:无法读取配置文件

来自分类Dev

vera ++抛出错误:无法打开配置文件描述以获取默认配置文件

来自分类Dev

如何在RPM SPEC文件中设置可选配置文件?

来自分类Dev

Laravel关系数据库连接

来自分类Dev

如何在其他配置文件中获取配置文件值-Laravel 5

来自分类Dev

无法获取GCM配置文件

来自分类Dev

Laravel 5.1将关系数据加载到所选行中

来自分类Dev

在Angular组件中获取数据(“配置文件”页面)

来自分类Dev

在Laravel 5.7中获取与各自的关系到关系数据

来自分类Dev

查询以获取Laravel中的关系数据

来自分类Dev

Laravel关系数据返回

来自分类Dev

从雄辩的构建者laravel 7获取关系数据

来自分类Dev

“获取”的DataPower SSL配置导致“无法获取SSL配置文件”

来自分类Dev

无法从AWS Lambda函数获取AppConfig的配置配置文件

来自分类Dev

无法在Laravel中检索一对多关系数据

来自分类Dev

如何获取可可数据集的Xception配置文件?

来自分类Dev

laravel嵌套关系数据透视表

来自分类Dev

Laravel:无法读取配置文件

来自分类Dev

传输的Thunderbird配置文件中无法识别数据

来自分类Dev

@ManagedBean问题获取关系数据

来自分类Dev

从hasOne关系Laravel 4.1获取hasMany关系数据

来自分类Dev

从Codeigniter中的关系数据库中获取数据

来自分类Dev

如何在RPM SPEC文件中设置可选配置文件?

来自分类Dev

关系数据库结构逻辑配置

来自分类Dev

无法获取配置文件(google-services.json)

来自分类Dev

Laravel-如何从配置文件中获取命名路由?

来自分类Dev

从配置文件获取数据源

来自分类Dev

无法显示来自 eloquent关系的关系数据

来自分类Dev

Laravel 获取关系数据

Related 相关文章

热门标签

归档