猿问

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

我的应用程序中有一个关系。候选人可以有多个“candidate_trainings”,并且每个“candidate_training”与一次训练相关联。我想避免将“candidate_trainings”设为枢轴,因为在分离等时很难删除正确的值。那么,在我的 hasMany 关系中,我如何才能使用训练模型中的数据获取 CandidateTraining 模型。

以下是我的人际关系:

<?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) {

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

    }


}


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

    }


    public function candidateTrainings() {

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

    }

}


<?php


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

    }

}


谢谢你!


白衣非少年
浏览 67回答 1
1回答

神不在的星期二

为了能够直接更新模型上的数据CandidateTraining,您需要向$fillable其中添加字段。protected $fillable = ['training_id', 'description'];你的代码应该可以工作!但如果你不介意的话,我做了一些重构。您可以通过另一种方式完成此操作:<?phpnamespace App;use App\Traits\SanitizeIds;use App\Salary;use Illuminate\Database\Eloquent\Model;use Illuminate\Support\Facades\Storage;class Candidate extends Model{&nbsp; &nbsp; public function saveTraining($data)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // remove all relationships&nbsp; &nbsp; &nbsp; &nbsp; $this->trainings()->detach();&nbsp; &nbsp; &nbsp; &nbsp; // add new ones&nbsp; &nbsp; &nbsp; &nbsp; foreach(json_decode($data['training']) as $training)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(Training::find($training->training)->first())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->trainings()->attach($training->training, [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'description' => $training->value,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public function candidateTrainings()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->hasMany(App\CandidateTraining::class);&nbsp; &nbsp; }&nbsp; &nbsp; public function trainings()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsToMany(App\Training::class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withTimestamps()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->using(App\CandidateTraining::class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withPivot([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'training_id',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'description',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; }}这些$training->training内容不可读,$training->id如果可以的话将其更改为类似的内容。<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Training extends Model{&nbsp; &nbsp; protected $fillable = ['name_french', 'name_english'];&nbsp; &nbsp; public function candidates()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsToMany(App\Candidate::class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withTimestamps()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->using(App\CandidateTraining::class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withPivot([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'training_id',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'description',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]);;&nbsp; &nbsp; }&nbsp; &nbsp; public function candidateTrainings()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->hasMany(App\CandidateTraining::class);&nbsp; &nbsp; }}<?phpnamespace App;use Illuminate\Database\Eloquent\Relations\Pivot;class CandidateTraining extends Pivot{&nbsp; &nbsp; protected $fillable = ['training_id', 'description'];&nbsp; &nbsp; public function candidate()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsTo(App\Candidate::class);&nbsp; &nbsp; }&nbsp; &nbsp; public function training()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsTo(App\Training::class);&nbsp; &nbsp; }}如果您想从控制器访问枢轴对象:$candidates = Candidate::with(['trainings'])->get();foreach ($candidates as $candidate){&nbsp; &nbsp; dd($candidate->pivot);}
随时随地看视频慕课网APP
我要回答