how can I pass an extra parameter to a controller in laravel - php

I have this:
Route::get('/product/{id}', 'PagesController#display');
And in my PagesController I have this:
public function display($id) {
return View::make("details", ["id" => $id, "premium" => $premium]);
}
How can I pass the variable $premium to the controller method without inserting it in the url? In simple words I don't want this: www.mysite.com/product/false/125 (false is the value of $premium) but this: www.mysite.com/product/125. That's why I have just $id as parameter in the controller method and no also $premium. I want to pass that variable $premium in other way.
I've try some approaches like this one:
Route::get('/product/{id}', 'PagesController#display')->where("premium" => "false");
which didn't work.

You can use a closure route and call the action "manually":
Route::get('/product/{id}', function($id){
return app('PagesController')->callAction('display', [$id, false]);
});
And
public function display($id, $premium) {
return View::make("details", ["id" => $id, "premium" => $premium]);
}

Related

How to bypass multiple parameter from url to controller

I have 2 parameters that I put in my url. What I want is how to get these 2 parameters, so it can be read from my controller.
I've try this so the controller can read the parameter:
$id = Input::get('id');
but when I check with dd, the parameter is null
Here is my code:
The link a href on view:
{{ URL('/lap_spd/tambahspd/'.$items->nosurat.'/'.$items->id )}}
web php :
Route::get('lap_spd/tambahspd/{id}/{nosurat}', function($id, $nosurat){
return redirect()->action(
'lapspdController#tambahuwong', ['id' => $id], ['nosurat' => $nosurat]);
});
lapspdController php:
public function tambahuwong($id, $nosurat) {
$id = Input::get('id');
$nosurat = Input::get('nosurat');
$data3 = DB::table('list_nama')
->where('id_nosurat', 'nosurat')
->toSql();
dd($data3);
so the output that I want - is the parameter can pass to variable on controller.
Thanks for respone before and sorry for my bad english.
Pass the parameters in a single array in your web.php file
return redirect()->action(
'lapspdController#tambahuwong', ['id' => $id, 'nosurat' => $nosurat]);
In lapspdController php:
public function tambahuwong($id, $nosurat) {
//remove these lines and use the variables from the parameters directly
//$id = Input::get('id');
//$nosurat = Input::get('nosurat');
$data3 = DB::table('list_nama')
->where('id_nosurat',$nosurat)
->toSql();
dd($data3);
in view:
in route:
Route::get('lap_spd/tambahspd/{id}/{nosurat}','HomeController#tambahuwong')
->name('check');
in controller:
public function tambahuwong($id,$nosurat) {
$check_id=$id;
$nosurat_name=$nosurat;
dd($check_id);
dd($nosurat_name);
}
Does it work if you change
return redirect()->action(
'lapspdController#tambahuwong', ['id' => $id], ['nosurat' => $nosurat]);
});
to
return redirect()->action(
'lapspdController#tambahuwong', ['id' => $id, 'nosurat' => $nosurat]);
});
?

i Can't pass three arrays to laravel view

I tried to pass three arrays to a view in laravel
but I got this problem
Undefined variable: demmande (View: C:\wamp\www\project\resources\views\demmande\demmandes.blade.php)
i change the order of the arrays i can't pass the third array
here is my function in the controller
public function ViewDemmandes(){
$listdemmande=Demmande::all();
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$villes = array('villes' => $listvillee, );
$demmande = array('demmande' => $listdemmande, );
$categorie = array('categorie' => $listcategorie, );
return view("demmande.demmandes",$villes,$categorie,$demmande);
}
You can use compact() method.
Try this line to return your data.
return view("demmande.demmandes",compact('villes','categorie','demmande'));
Just replace your code with this,
public function ViewDemmandes(){
$listdemmande=Demmande::all();
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$villes = $listvillee;
$demmande = $listdemmande;
$categorie = $listcategorie;
return view("demmande.demmandes",compact('villes','categorie','demmande'));
}
And you can retrieve those variables by
#foreach ($demmande as $data)
{{$data->property}} //your property to define
#endforeach
Hope this will work.
From reading the Laravel Views documentation I think that the view() method expects you to specify the template parameters using one array. You can combine your three arrays into one:
public function ViewDemmandes(){
$listdemmande=Demmande::all();
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$data = array(
'villes' => $listvillee,
'demmande' => $listdemmande,
'categorie' => $listcategorie,
);
return view("demmande.demmandes", $data);
}
According to the official documentation you can either pass an array as second parameter, as opposed to the list of all the parameters.
return view("demmande.demmandes", [
'villes' => $villes,
'categorie' => $categorie,
'demande' => $demande
]);
or chain the with method to add more parameters (see the Github page).
return view("demmande.demmandes")
->with('villes', $villes)
->with('categorie', $categorie)
->with('demande', $demande);
The view function will only accept a single array, however, you can nest your arrays within it like this -- and still access them by key from within the view.
return view("demmande.demmandes",['villes'=>$villes, 'categorie'=>$categorie, 'demmande'=>$demmande]);
public function ViewDemmandes(){
$listdemmande=Demmande::all();
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$villes = array('villes' => $listvillee, );
$demmande = array('demmande' => $listdemmande, );
$categorie = array('categorie' => $listcategorie, );
return view("demmande.demmandes",compact('villes','categorie','demmande');
A simple example
View the controller in app/Http/Controllers/SampleController.php
/**
* #return View
*/
public function index()
{
$movieList = [
'Shawshank redemption',
'Forrest Gump',
'The Matrix',
'Pirates of the Carribean',
'Back to the future',
];
return view('welcome', compact('movieList'));
}
and for example, See latest movie views in resources/views/welcome.blade.php
#section('content')
<h1>Latest Movies</h1>
<ul>
#foreach($movieList as $movie)
<li class="list-group-item"><h5>{{ $movie }}</h5></li>
#endforeach
</ul>
#endsection

Laravel Route Redirect with URL Parameter

I am trying to generate a unique id and then redirect to a different route with id in the url parameter. But I am getting error as :
"Route [sequences] not defined."
Here is my route defined:
Route::get('/sequences_create','SequencesController#create');
Route::get('/sequences/{id}', 'SequencesController#show');
Here is the create function on the sequences controller:
public function create()
{
$uniq = 'seq'. uniqid();
$seq = new Sequences;
$seq->id = $uniq;
$seq->user_id = auth()->user()->id;
$seq->name = 'New Sequence'; //temp name
$seq->save();
return redirect()->route('sequences', ['id' => $uniq]);
}
you have not set a name for your route by name() method.
try it:
Route::get('/sequences_create','SequencesController#create')->name('sequances.create');
Route::get('/sequences/{id}', 'SequencesController#show')->name('sequances.show');
then:
public function create()
{
//...
return redirect()->route('sequences.show', ['id' => $uniq]);
}
you need to name your route.
Route::get('/sequences_create','SequencesController#create')->name('sequences.create');
Route::get('/sequences/{id}', 'SequencesController#show')->name('sequences.show');
Then change your redirect to:
return redirect()->route('sequences.show', ['id' => $uniq]);

how to pass two variables to view in controller (laravel 5.3)

I have two different queries which are saved in two variables.I want to pass the variables to view page from controller.
public function getApprovalList(){
// $users = select query..
// $request = select query..
return view('travelerHome',['users'=>$users,'request'=>$request]);
}
solution:
controller
return view('travelerHome',['users'=>$users,'requestList'=>$request]);
view
#foreach ($requestList as $req)
{{$req->traveler_name }}
#endforeach
return view('travelerHome')->with(array('users'=>$users,'request'=>$request));
You can use return view('travelerHome', compact('users', 'request')); too
Do like this
return view('travelerHome')->with('users'=>$users)->with('request'=>$request);
Code with you have written that is correct, you need to remove the comment only, I am also using the same method for pass the variable to View. Here is the example.
return view('admin.product.product.edit', ['product' => $product,
'attribute_set' => $productAttributes,
'category' => $cates,
'images' => $images,
'status' => $status,
'countries' => $countries,
'crane_manufacture' => $crane_manufacture,
'product_category' => $productCategory]);
}
And it's also working
So, in your code you need to
public function getApprovalList(){
$users = 'select query..';
$request = 'select query..';
return view('travelerHome',['users'=>$users,'request'=>$request]);
}

Laravel Passing Additional Parameter To Controller

I need to pass an additional parameter($uid) from my index.blade.php to my edit.blade.php by clicking on a button.
My index.blade.php:
Edit
My FlyersController:
public function edit($id, $uid)
{
return view('backend.flyers.edit')->withUid($uid);
}
With the code above I get an error: "Missing argument 2 for App\Http\Controllers\FlyersController::edit()"
What am I doing wrong here?
The error is not throwing from the action method. It is coming from route for that URL.
Check the URL for passing argument to the the controller.
If this is the your desire URL localhost:8000/backend/flyers/10/edit?%24uid=1 then the second argument is in $request variable not in controller function argument.
You should pass an array into action() helper:
action('FlyersController#edit', ['id' => Auth::user()->id, 'uid' => 1])
Ok,
the only way I can solve this is by using the following in My FlyersController:
public function edit(Request $request, $id)
{
return view('backend.flyers.edit')->withRequest($request);
}
and access then the uid with {{request->uid}} in my view.
If anybody has a better solution for this, let me know.
Use this code
return view('backend.flyers.edit', ['var1' => $var1, 'var2' => $var2]);
That will pass two or more variables to your view

Categories