简体   繁体   中英

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:

  • id
  • courrier_id
  • structure_id
  • valide

valide is a boolean attribute.

In first case, I inserted the courrier_id and the structure_id via a form. 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.

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

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:

Undefined property: 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,

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. 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.

If you call the method as a property (without parentheses), Laravel returns the relationship target . In this case a collection of Structure model instances.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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