hasManyThrough

时间:2019-03-10 01:34:40

标签: php laravel

我想创建一个编辑/更新系统,但是有一个hasManyThrough。 我的表格中有3个字段,可用于3个表格。

台式车的入学 表系列的fk_serie fk_mark为表格标记

enter image description here

我可以更改矩阵和fk_serie的值,但字段fk_mark不会发生任何变化。

我的问题可能出在我的CarController中吗?

public function edit($id)
    {
        $cars = Car::find($id);
        $series = Serie::all();
        $marks = Mark::all();
        return view('admin.cars.edit', compact('cars', 'series', 'marks'));
    }
    public function update(Request $request, $id)
    {
        $request->validate([
        'matriculation' => 'required|string',
        'fk_serie' => 'required'
        ]);

        $cars = Car::find($id);

        $cars->matriculation = $request->get('matriculation');
        $cars->fk_serie = $request->get('fk_serie');
        $cars->save();
        return redirect()->route('cars.index')
                   ->with('success', 'updated successfully');
    }

这是我的文件 cars.edit.blade

<fieldset class="form-group">
                <label for="form-group-input-1">Matriculation</label>
                <input type="text" name="matriculation" class="form-control" id="form-group-input-1" value="{{$cars->matriculation}}">
                </fieldset>

                <div class="form-group">
                <label for="company-content">Select Model</label>
                <select name="fk_serie" id="" class="form-control">
                    @foreach($series as $serie)
                    <option value="{{$serie->id}}">
                    {{$serie->name}}
                    </option>
                    @endforeach
                </select>
                </div>

                <div class="form-group">
                <label for="company-content">Select Mark</label>
                <select name="fk_mark" id="" class="form-control">
                    @foreach($marks as $mark)
                    <option value="{{$mark->id}}">
                    {{$mark->name_mark}}
                    </option>
                    @endforeach
                </select>
                </div>

我的模型是:

汽车

class Car extends Model
{

    protected $fillable = ['matriculation', 'fk_serie'];

    public function series(){

        return $this->belongsTo('App\Serie', 'fk_serie');
    }

    //

    public function cars()
    {
        return $this->hasManyThrough(
            'App\Car',
            'App\Serie',
            'fk_mark', 
            'fk_serie',
            'id', 
            'id' 
        );
    }

}

意甲

class Serie extends Model
{

    protected $fillable = ['name', 'fk_mark'];

    public function cars(){

        return $this->hasMany('App\Car','fk_serie');
    }

    public function marks(){

        return $this->belongsTo('App\Mark', 'fk_mark');

    }

}

标记

class Mark extends Model
{

    protected $fillable = ['name_mark'];

    public function series(){
        return $this->hasMany('App\Serie', 'fk_mark');
    }



    public function cars(){
     return $this->hasMany('App\Car','fk_serie');
   }


}

谢谢。

0 个答案:

没有答案