简体   繁体   English

我怎样才能在 Laravel 中获得我的关系的支点?

[英]How can I get a pivot on my relationship in Laravel?

I have a relationship in my app.我在我的应用程序中有一个关系。 A candidate can have several "candidate_trainings" and each "candidate_training" is associated with a training.一个候选人可以有多个“candidate_trainings”,每个“candidate_training”都与一个训练相关联。 I wanted to avoid making "candidate_trainings" the piviot since it's hard to delete the right values when detaching, etc. So, how can I, on my hasMany relationship get the CandidateTraining model with the data from the Training model.我想避免将“candidate_trainings”作为中心点,因为在分离时很难删除正确的值,等等。所以,我如何才能在我的 hasMany 关系中使用来自 Training 模型的数据获取 CandidateTraining 模型。

Here are my relationships:以下是我的关系:

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

Thank you!谢谢!

For you to be able to update the data directly on the CandidateTraining model, you need to add the $fillable fields to it.为了能够直接在CandidateTraining模型上更新数据,您需要向其中添加$fillable字段。

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

Your code should work!您的代码应该可以工作! But if you don't mind, I did a little refactoring.但如果你不介意,我做了一点重构。 You can accomplish this in another way:您可以通过另一种方式完成此操作:

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

That $training->training stuff is not readable, change it to something like $training->id if you are able to.那个$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);
    }
}

If you want to access the pivot object from a controller:如果要从控制器访问枢轴对象:

$candidates = Candidate::with(['trainings'])->get();
foreach ($candidates as $candidate)
{
    dd($candidate->pivot);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM