I am working on referring system, but I am stuck how to do this. When generate a referrer link for the product I want to redirect the user to that product when any one click on that link example: http://127.0.0.1:8000/affiliate/referrer_id/product_id this is the example referrer link.
I generated this link
http://127.0.0.1:8000/affiliate/1/9
As we see at the end of the url the product id is present. The product detail page of this id link is this
http://127.0.0.1:8000/product/9
My question is when some one click on the referrer link how to redirect user to this product detail link?
I try this but i am not sure about this is it right or not. At the web.php I declare a route like this
// Affiliate
Route::get('http://127.0.0.1:8000/affiliate/{referrer_id}/{product_id}', 'UsersController@referrerRedirect');
At the UsersController referrerRedirect function
public function referrerRedirect($product_id, $referrer_id)
{
return view();
}
Thanks!
CodePudding user response:
Assuming you have a product route and controller: There are many ways to setup the route (eg resource controllers and such) this just an example
Route::get('products/{product_id}', 'ProductController@get')->name('product');
public function get($product_id)
{
$product = Product::findOrFail($product_id);
return view('your_product_view', ['product' => $product]);
}
The idea is that you return a redirect to the product route ->name() and pass the param.
public function referrerRedirect($product_id, $referrer_id)
{
//do referrel things with the $referrer_id
return redirect()->route('product', [$product_id]);
//or it may be
return redirect()->route('product', ['product' => $product_id]);
}
CodePudding user response:
web.php
// Affiliate
Route::get('affiliate/{product_id}/{referrer_id}', 'UsersController@referrerRedirect');
UsersController referrerRedirect function
public function referrerRedirect($product_id, $referrer_id)
{
// right a query to fetch data from database product table match with $product_id
$product = Product::where('id', $product_id)->get();
return view('products.details')->with(compact('product'));
}