【问题标题】:Can't Retrieve One to Many Relationship Data In Laravel无法在 Laravel 中检索一对多关系数据
【发布时间】:2021-04-06 22:43:47
【问题描述】:

我对在 Laravel 中检索一对多关系数据有点奇怪。

型号

// JobTypes model
public function jobs()
{
    // one type of job has many jobs
    return $this->hasMany('App\Jobs', 'id'); // id refer to jobs.id
}

// Jobs model
public function job_types()
{
    // one job only belongs to one type of job
    return $this->belongsTo('App\jobTypes');
}

数据透视表

 Schema::create('jobs_job_types', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('jobs_id')->unsigned()->nullable();
        $table->integer('job_types_id')->unsigned()->nullable();
        $table->timestamps();

        $table->foreign('jobs_id')->references('id')->on('jobs');
        $table->foreign('job_types_id')->references('id')->on('job_types');
    });

控制器

$data = \App\JobTypes::paginate($items);

    return view('jobs.index', compact('data'))->with(array('showData' => $showData, 'count' => $count))->withItems($items);

查看

@foreach($data as $jobType)
        <td>
          @foreach($jobType->jobs as $category)
            {{ $category->name }}
          @endforeach
        </td>
    @endforeach 

我错过了什么吗?

【问题讨论】:

  • 建议尽可能遵循通用的 Laravel 命名约定,尤其是在你的模型中,因为这会为你以后省去很多麻烦,并使你的代码更容易被其他 Laravel 开发人员阅读。即App\jobTypes应为单数大写App\JobType,其对应的数据库表为snake_case和小写job_types

标签: laravel laravel-5 relationship


【解决方案1】:

在 Laravel 中,one-to-many relationship 不需要数据透视表。子关系(“多”端)可以简单地存储它所属的父模型的 id。

(另请参阅我上面关于以下一般Laravel naming conventions的评论。)

型号:

// JobType.php
class JobType extends Model
{
    public function jobs()
    {
        return $this->hasMany('App\Job');
    }
}

// Job.php
class Job extends Model
{
    public function job_type()
    {
        return $this->belongsTo('App\JobType');
    }
}

迁移:

// create_job_types_table.php
    Schema::create('job_types', function (Blueprint $table) {
        $table->increments('id');
        ...
    });

// create_jobs_table.php
    Schema::create('jobs', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('job_type_id')->unsigned()->index();
        ...

        $table->foreign('job_type_id')->references('id')->on('job_types');
    });

【讨论】:

  • 我不知道,现在已经用数据透视表解决了一对多关系。这很糟糕吗?
  • 一对多的关系是你能得到的最基本的。数据透视表通常仅用于/需要用于多对多关系。至少可以说,在这里使用它们是非标准。如果您真的坚持使用数据透视表,则需要将关系的双方声明为belongsToMany(参见laravel.com/docs/5.8/eloquent-relationships#many-to-many)。
  • Laravel 中的一对一关系怎么样?我们需要数据透视表吗?
  • @SeadLab 不,一对一的关系也不需要数据透视表。这是我过去在决定使用什么关系以及如何实现它时使用的有用资源:medium.com/hackernoon/…
【解决方案2】:

试试这样:

// JobTypes model
public function jobs()
{
    // one type of job has many jobs
    return $this->hasMany(\App\Job_Type::class, 'job_id');
}

// Jobs model
public function job_types()
{
    // one job only belongs to one type of job
    return $this->belongsTo('App\jobTypes','job_type_id','id');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-14
    • 2017-01-17
    • 2020-03-26
    • 2014-04-18
    • 2023-02-07
    • 2018-01-06
    • 1970-01-01
    • 2014-08-11
    相关资源
    最近更新 更多