簡體   English   中英

我怎樣才能在 Laravel 中獲得我的關系的支點?

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

我在我的應用程序中有一個關系。 一個候選人可以有多個“candidate_trainings”,每個“candidate_training”都與一個訓練相關聯。 我想避免將“candidate_trainings”作為中心點,因為在分離時很難刪除正確的值,等等。所以,我如何才能在我的 hasMany 關系中使用來自 Training 模型的數據獲取 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');
    }
}

謝謝!

為了能夠直接在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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM