404 NOT FOUND LARAVEL ELOQUENT

32 Views Asked by At

I have three models

Produit Model :

public function propositions ()
    {
        return $this->belongsToMany(PropoCom::class, 'propo_com_produits',
        'propo_com_id','produit_id',)->withPivot('tva', 'prix_unitaire_ht', 'quantite', 'reduction', 'prix_revient');
    }

PropoCom Model :

public function produits ()
    {
        return $this->belongsToMany(Produit::class, 'propo_com_produits',
        'propo_com_id','produit_id',)->withPivot('tva', 'prix_unitaire_ht', 'quantite', 'reduction', 'prix_revient');
    }

public function propoCom_produit()
    {
        return $this->hasMany(PropoCom_produit::class);
    }

PropoCom_produit Model :

public function produits(){
        return $this->belongsTo(Produit::class, 'produit_id');
      }

public function proposition (){
        return $this->belongsTo(PropoCom::class,"propo_com_id");
    }

PropositionController which allows me to modify Produit associated with PropoCom :

public function edit($id)
    {
        $commerce = PropoCom_produit::findOrfail($id);
        $proposition = $commerce->proposition;
        return view('layouts.commerce.edit-propo', compact('commerce', 'proposition'));
    }

Route to edit :

Route::get('/commerce/new/proposition/details/{propo_com_id}/edit'[App\Http\Controllers\PropositionController::class, 'edit'])->name('proposition.produit.edit');

My Blade view with link allowing me to modify Produit associated with PropoCom:

<a href="{{ route('proposition.produit.edit', $proposition->id) }}" target="_blank" class="btn btn-primary btn-sm"><i class="fa fa-pen"></i></a>

But when I hover over the icon to see the link it retrieves the PropoCom ID from the PropoCom_Product ID link and when I click on it I get a 404 error NOT FOUND

Any ideas ?

1

There are 1 best solutions below

0
Karl Hill On

It seems like you are trying to pass the PropoCom_produit ID to the edit method of PropositionController, but in your route definition, you are using propo_com_id as a parameter. This might be causing the issue.

In your route definition, you should use the PropoCom_produit ID as a parameter. Here's how you can do it:

Route::get('/commerce/new/proposition/details/{id}/edit', [App\Http\Controllers\PropositionController::class, 'edit'])->name('proposition.produit.edit');

And in your Blade view, you should pass the PropoCom_produit ID to the route:

<a href="{{ route('proposition.produit.edit', $commerce->id) }}" target="_blank" class="btn btn-primary btn-sm"><i class="fa fa-pen"></i></a>

This way, when you click the link, it will pass the correct PropoCom_produit ID to the edit method of PropositionController.