Hamed Adil
Hamed Adil

Reputation: 483

Sending POST request with GET parameter in Laravel

For example, I have this route in my web.php :-

Route::get('products/{product}/owners', 'ProductController@showOwners');

When I try to add new 'Owner' to the product, I have to do it in the parent URL, like this :-

Route::post('products/storeOwner', 'ProductController@storeOwner');

And then I pass the product ID in a hidden field in the form, because the post request doesn't accept URL parameters. So is there anyway to do it like below ?

Route::post('products/{product}/storeOwner', 'ProductController@storeOwner');

So the POST request will be sent inside the particular 'product' URL?
UPDATE

/* ProductController Class */
public function storeOwner (AddProductOwner $request)
    {

        $product= Product::find($request->product);
        $user = Auth::user();

        if ( $user->ownerOf($product)) {
            // Check if the current user is already one of the owners).
            // If the current user is the owner then return to the product
            // This line is not executed because in (products/show.blade.php) we have set a condition.

            return redirect('products/' . $request->product);
        }

        $join = new Join;
        $join->role = $request->join_role;
        $join->product()->associate($request->product);
        $join->user()->associate(Auth::user());

        $join->message = $request->message;


        $join->save();

        // TODO: we have to make this with ajax instead of normal form
        return redirect('products/'. $request->product);
}



I hope my question is clear enough..

Upvotes: 1

Views: 10724

Answers (2)

Himanshu Upadhyay
Himanshu Upadhyay

Reputation: 6565

Yes you can do as you mentioned in your last route

Route::post('products/{product}/storeOwner', 'ProductController@storeOwner');

And then get the product Id in your functions argument

public function storeOwner (AddProductOwner $request, $productId)
{
    dd($productId);    // TRY THIS OUT. CHECK THE 2nd ARGUMENT I SET.
    $product= Product::find($productId);    // PASS THE VERIABLE HERE.
    $user = Auth::user();

    if ( $user->ownerOf($product)) {
        // Check if the current user is already one of the owners).
        // If the current user is the owner then return to the product
        // This line is not executed because in (products/show.blade.php) we have set a condition.

        return redirect('products/' . $request->product);
    }

    $join = new Join;
    $join->role = $request->join_role;
    $join->product()->associate($request->product);
    $join->user()->associate(Auth::user());

    $join->message = $request->message;


    $join->save();

    // TODO: we have to make this with ajax instead of normal form
    return redirect('products/'. $request->product);

}

Upvotes: 4

Dhiraj
Dhiraj

Reputation: 2767

You can send URL parameters to a POST request. Just make sure in your form you are sending the wildcard.

 <form action="/products/{{ $productid }}/storeOwner" method="POST">

In your routes

Route::post('products/{productid}/storeOwner', 'ProductController@storeOwner');

In your controller, use it

public function storeOwner($productid)
{
    dd($productid);
}

Upvotes: 3

Related Questions