为了账号安全,请及时绑定邮箱和手机立即绑定

我怎样才能在 Laravel 中找到关系的支点?

我怎样才能在 Laravel 中找到关系的支点?

PHP
白衣非少年 2024-01-19 10:21:46
我的应用程序中有一个关系。候选人可以有多个“candidate_trainings”,并且每个“candidate_training”与一次训练相关联。我想避免将“candidate_trainings”设为枢轴,因为在分离等时很难删除正确的值。那么,在我的 hasMany 关系中,我如何才能使用训练模型中的数据获取 CandidateTraining 模型。以下是我的人际关系:<?phpnamespace App;use App\Traits\SanitizeIds;use App\Salary;use Illuminate\Database\Eloquent\Model;use Illuminate\Support\Facades\Storage;class Candidate extends Model{    public function saveTraining($data) {        $this->candidateTrainings()->delete();        foreach(json_decode($data['training']) as $training) {            if(Training::find($training->training)->first()) {                $candidateTraining = new CandidateTraining;                $candidateTraining->description = $training->value;                $candidateTraining->training_id = $training->training;                $this->candidateTrainings()->save($candidateTraining);            }        }    }    public function candidateTrainings() {        return $this->hasMany('\App\CandidateTraining');    }    public function trainings() {        return $this->belongsToMany('\App\Training');    }}<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Training extends Model{    protected $fillable = ['name_french', 'name_english'];    public function candidates() {        return $this->belongsToMany('\App\Candidate');    }    public function candidateTrainings() {        return $this->hasMany('\App\CandidateTraining');    }}<?phpnamespace App;use Illuminate\Database\Eloquent\Relations\Pivot;class CandidateTraining extends Pivot{    public function candidate() {        return $this->belongsTo('\App\Candidate');    }    public function training() {        return $this->belongsTo('\App\Training');    }}谢谢你!
查看完整描述

1 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

为了能够直接更新模型上的数据CandidateTraining,您需要向$fillable其中添加字段。


protected $fillable = ['training_id', 'description'];

你的代码应该可以工作!但如果你不介意的话,我做了一些重构。您可以通过另一种方式完成此操作:


<?php


namespace App;


use App\Traits\SanitizeIds;

use App\Salary;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Facades\Storage;


class Candidate extends Model

{

    public function saveTraining($data)

    {

        // remove all relationships

        $this->trainings()->detach();


        // add new ones

        foreach(json_decode($data['training']) as $training)

        {

            if(Training::find($training->training)->first())

            {

                $this->trainings()->attach($training->training, [

                    'description' => $training->value,

                ]);

            }

        }

    }


    public function candidateTrainings()

    {

        return $this->hasMany(App\CandidateTraining::class);

    }


    public function trainings()

    {

        return $this->belongsToMany(App\Training::class)

            ->withTimestamps()

            ->using(App\CandidateTraining::class)

            ->withPivot([

                'id',

                'training_id',

                'description',

            ]);

    }

}


这些$training->training内容不可读,$training->id如果可以的话将其更改为类似的内容。


<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class Training extends Model

{

    protected $fillable = ['name_french', 'name_english'];


    public function candidates()

    {

        return $this->belongsToMany(App\Candidate::class)

            ->withTimestamps()

            ->using(App\CandidateTraining::class)

            ->withPivot([

                'id',

                'training_id',

                'description',

            ]);;

    }


    public function candidateTrainings()

    {

        return $this->hasMany(App\CandidateTraining::class);

    }

}


<?php


namespace App;


use Illuminate\Database\Eloquent\Relations\Pivot;


class CandidateTraining extends Pivot

{

    protected $fillable = ['training_id', 'description'];


    public function candidate()

    {

        return $this->belongsTo(App\Candidate::class);

    }


    public function training()

    {

        return $this->belongsTo(App\Training::class);

    }

}


如果您想从控制器访问枢轴对象:


$candidates = Candidate::with(['trainings'])->get();

foreach ($candidates as $candidate)

{

    dd($candidate->pivot);

}


查看完整回答
反对 回复 2024-01-19
  • 1 回答
  • 0 关注
  • 66 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信