第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我怎樣才能在 Laravel 中找到關(guān)系的支點?

我怎樣才能在 Laravel 中找到關(guān)系的支點?

PHP
白衣非少年 2024-01-19 10:21:46
我的應(yīng)用程序中有一個關(guān)系。候選人可以有多個“candidate_trainings”,并且每個“candidate_training”與一次訓(xùn)練相關(guān)聯(lián)。我想避免將“candidate_trainings”設(shè)為樞軸,因為在分離等時很難刪除正確的值。那么,在我的 hasMany 關(guān)系中,我如何才能使用訓(xùn)練模型中的數(shù)據(jù)獲取 CandidateTraining 模型。以下是我的人際關(guān)系:<?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貢獻(xiàn)1963條經(jīng)驗 獲得超6個贊

為了能夠直接更新模型上的數(shù)據(jù)CandidateTraining,您需要向$fillable其中添加字段。


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

你的代碼應(yīng)該可以工作!但如果你不介意的話,我做了一些重構(gòu)。您可以通過另一種方式完成此操作:


<?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內(nèi)容不可讀,$training->id如果可以的話將其更改為類似的內(nèi)容。


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

}


查看完整回答
反對 回復(fù) 2024-01-19
  • 1 回答
  • 0 關(guān)注
  • 142 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號