简体   繁体   English

在 pivot 表中插入数据 Controller Laravel 8

[英]Insert data in pivot table Controller Laravel 8

I have two tables with many to many relationship:我有两个具有多对多关系的表:

  • courriers
  • structures
  • courrier_structure

The pivot table contains: pivot 表包含:

  • id
  • courrier_id
  • structure_id
  • valide

valide is a boolean attribute. valide是 boolean 属性。

In first case, I inserted the courrier_id and the structure_id via a form.在第一种情况下,我通过表单插入了courrier_idstructure_id Next, I wish to put 1 into the valide value when the user click on a button, and I wanna do this via the controller.接下来,当用户单击按钮时,我希望将 1 放入验证值中,并且我想通过valide执行此操作。

My models look like this:我的模型如下所示:

class Courrier extends Model {
    public function structures()
    {
        return $this->belongsToMany(Structure::class)->withTimestamps()
            ->withPivot('valide');
    }
}
class Structure extends Model
{
    use HasFactory;

    protected $fillable = [
        'nom_structure',
    ];
    
    public function courriers(){

        return $this->belongsToMany(Courrier::class)->withTimestamps()
            ->withPivot('valide');
    }
}

My web route我的 web 路线

Route::get('{id}/valider', [App\Http\Controllers\CourrierController::class, 'valider'])
    ->name('valider');

My view on blade:我对刀片的看法:

@foreach($courriers as $key => $courrier)
<td>
    <a class='' onclick='return confirm("Êtes-vous sûr de vouloir valider la réception de ce courrier?")' href="{{ route('valider',$courrier->id) }}"> 
        Valider 
    </a>
</td>
@endforeach

My controller where I get this error:我的 controller 出现此错误:

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$id未定义属性:Illuminate\Database\Eloquent\Relations\BelongsToMany::$id

public function valider( Request $rquest, Courrier $courrier) {
    $courrier->structures()->updateExistingPivot($courrier->structures()->id,
        [$request->valide = 1]
    );
    return redirect()->route('nvCourriersDMO');
}

And I'm working on fields where structure_id=1,我正在研究结构id = 1的领域,

public function nvCourriersDMO(Request $request, Courrier $courrier)
    {
                     
        $courriers = Courrier::join('courrier_structure', 'courriers.id',         
    '=', 'courrier_structure.courrier_id')- 
   >where('courrier_structure.structure_id','=','1')- 
   >where('courrier_structure.valide','=',NULL)->get();
         
                
        return view("nvCourriersDMO", compact('courriers'));
    }

Laravel relationship methods are a little tricky sometimes. Laravel 关系方法有时有点棘手。 If you call the method using parentheses (as you are here), it returns a "relationship class" instance.如果您使用括号调用该方法(就像您在这里一样),它会返回一个“关系类”实例。 In this case an instance of the BelongsToMany class.在这种情况下, BelongsToMany class 的实例。

If you call the method as a property (without parentheses), Laravel returns the relationship target .如果将该方法作为属性调用(不带括号),则 Laravel 将返回关系目标 In this case a collection of Structure model instances.在这种情况下, Structure model 实例的集合。

This should do the trick:这应该可以解决问题:

public function valider(Request $request, Courrier $courrier)
{
    $pivotIds = $courrier->structures()->allRelatedIds();

    $courrier->structures()->updateExistingPivot(
        $pivotIds,
        ['valide' => 1]
    );

    return redirect()->route('nvCourriersDMO');
}

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

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