Passing variables from one controller to another with laravel - php

Is it possible when redirect from one controller to another also to pass variables? What I mean is that I have submit function and after submit I make redirect to url. Then in other controller trying to display some info based on variables. This is the redirect from first controller
return Redirect::to('/cart/payment/order/' . $order->order_id . '+' . $order->user_id);
then in second controller this
public function paymentView() {
$order = Order::where('order_id', $orderId)->first();
if (!$order) {
App::abort(404);
}
$userID = $order['user_id'];
$orderID = $order['order_id'];
}
Question is how to get $user_id and $order_id now?

First your route declaration should accept them, something like this, depending on your route:
Route::get('cart/payment/order/{orderID}/{userID}'...
Then Laravel will automatically inject them in your controller method:
public function paymentView( $orderID, $userID, ) {
And I recommend your URL to be with slash not with plus sign:
return Redirect::to('/cart/payment/order/' . $order->order_id . '/' . $order->user_id);

Related

How to prevent get/post clash in Laravel 6?

Currently I'm working on a project where I made it so that when a user types a correct password in form field, it will give them the items from the given section.
The main problem i'm having is that to do this I need to capture the request and therefore the route has to be a post method instead of a get as such:
public function index(Request $request)
{
$id = $request->input('id');
$password = $request->input('password');
$result = DB::table('scrumboards')->find($id);
if ($result->key == $password) {
$scrumboard = $result;
$items = DB::table('backlogs')->get();
return view('scrumboard', ['items' => $items, 'scrumboard' => $scrumboard]);
} else {
$scrumboard = $result;
return redirect('home');
}
}
and the route as such:
Route::post('/scrumboard', 'ScrumboardController#index');
By doing this, request errors wont work since It wants to redirect back but can't since this is a post method.
Any way I can avoid this clash?
Routes can have multiple HTTP verbs. Define your route as
Route::match(['get', 'post'], '/scrumboard', 'ScrumboardController#index');
to make it available as GET and POST route.

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

Passing parameter while redirecting to another route

I am trying to 'redirect to' from one controller method to another through the route. However, I want to pass some data as well. I tried Session::get('name') but doesn't seem to work. This is what I tried:
public function before() {
return Redirect::to('later')->with('x', 'y');
}
public function later() {
dd( Session::get('x') ); // null
dd( $x ) // not working
}
My route is like classic:
Route::get('/later', 'TheController#later')->middleware('auth');
What am I missing?
Instead of Session::get('x') try with session('x') as below.You can check for it using if (session()->has('x'))
public function later() {
dd( session('x'));
}

How to use variables in routes in laravel?

I'm trying to build a application in laravel 5.3 in which I get the variable from request method and then trying to pass that variable in a redirect to the routes. I want to use this variable in my view so that I can be able to display the value of variable. I'm currently doing this:
In my controller I'm getting the request like this:
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member' => $member);
}
Now I've following in my routes:
Route::get('/member/memberinfo', 'MemberController#memberinfo')->with('member', $member);
Now in MemberController I want to use $member variable and display this into my view:
public function memberinfo()
{
return view('member.memberinfo', ['member' => $member]);
}
But I'm getting an error in the routes files
Call to undefined method Illuminate\Routing\Route::with()
Help me out, how can I achieve this.
When you're using redirect()->with(), you're saving data to the session. So to get data from the session in controller or even view you can use session() helper:
$member = session('member'); // In controller.
{{ session('member')['xyz'] }} // In view.
Alternatively, you could pass variables as string parameters.
Redirect:
return redirect('member/memberinfo/xyz/abc')
Route:
Route::get('/member/memberinfo/{xyz}/{abc}', 'MemberController#memberinfo');
Controller:
public function memberinfo($xyz, $abc)
{
return view('member.memberinfo', compact('xyz', 'abc'));
}
You can use like this:
route:
Route::get('/member/memberinfo', 'MemberController#memberinfo')
and the redirect:
return redirect('member/memberinfo')->with('member', $member);
You need to replace => with ,
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member', $member); // => needs to be replaced with ,
}
Hope this works!
Replace line
return redirect('member/memberinfo')->with('member' => $member);
to
return redirect('member/memberinfo')->with('member', $member);
......

pass multiple parameters to controller from route in laravel5

I want to pass multiple parameters from route to controller in laravel5.
ie,My route is ,
Route::get('quotations/pdf/{id}/{is_print}', 'QuotationController#generatePDF');
and My controller is,
public function generatePDF($id, $is_print = false) {
$data = array(
'invoice' => Invoice::findOrFail($id),
'company' => Company::firstOrFail()
);
$html = view('pdf_view.invoice', $data)->render();
if ($is_print) {
return $this->pdf->load($html)->show();
}
$this->pdf->filename($data['invoice']->invoice_number . ".pdf");
return $this->pdf->load($html)->download();
}
If user want to download PDF, the URL will be like this,
/invoices/pdf/26
If user want to print the PDF,the URL will be like this,
/invoices/pdf/26/print or /invoices/print/26
How it is possibly in laravel5?
First, the url in your route or in your example is invalid, in one place you use quotations and in the other invoices
Usually you don't want to duplicate urls to the same action but if you really need it, you need to create extra route:
Route::get('invoices/print/{id}', 'QuotationController#generatePDF2');
and add new method in your controller
public function generatePDF2($id) {
return $this->generatePDF($id, true);
}

Categories