I'm trying to pass two variables/arguments from my view through a link
<a href="{{ route('shop.order.test', $id,$form['grouping']) }}"
and call the route
Route::get('ordering/test', 'Shop\OrderingController#testing')
->name('shop.order.test');
And call this function with those two arguments
public function testing($id,$grouping){
}
It doesn't seem to be working though. Is my error in my route or my link call?
If you want to have parameters to be passed into controller's method, you need to define route parameters like this
Route::get('ordering/test/{id}/{grouping}', 'Shop\OrderingController#testing');
then you can have it in controller method:
public function testing($id, $grouping)
To generate route for above definition, the second parameter is the array of params to pass. So it will become
{{ route('shop.order.test', ['id' => $id, 'grouping' => $form['grouping']) }}
To pass parameters in a route use an array with the paramter names as keys:
{{ route('shop.order.test', ['id' => $id, 'grouping' => $form['grouping']]) }}
Laravel Doc
Related
My problem:
I am currently trying to refactor some of my controllers. Doing so I found these two routes:
Route::get('/events', [EventsController::class, 'eventsList'])->name('event-list');
Route::get('/courses', [EventsController::class, 'allCoursesList'])->name('all-events');
they show different filter options in the frontend.
What I want to do:
Example Code:
Route::get('/courses', [
'all' => 1,
EventsController::class, 'courseList'
])->name('all-events');
so having the ability to pass a variable, in this case all to my controller. EventsController So I can check with if in my controller and handle these routes differently with only one function instead of two.
With the current solutions on StackOverflow, users are using:
'uses'=>'myController#index'
now if I try it like this:
Route::get('/courses', [
'all' => 1,
'uses' => 'EventsController#CourseList'
])->name('all-events');
I get the following error:
Target class [EventsController] does not exist.
Question:
What is the current, correct way, to pass a variable to a controller, from a route. In Laravel 9 and 10.
You can pass arbitrary data to the route as a parameter using the defaults method of Route:
Route::get('courses', [EventsController::class, 'list'])
->name('all-events')
->defaults('all', 1);
Route::get('events', [EventsController::class, 'list'])
->name('event-list');
public function list(Request $request, $all = 0)
{
...
}
There are also other ways of using the Route to pass data.
Laravel versions 8 and above do not automatically apply namespace prefixes. This means that when passing the class name as a string, you need to use the fully qualified class name (FQCN).
For example:
Route::get('/courses', [
'all' => 1,
'uses' => '\App\Http\Controllers\EventsController#CourseList'
])->name('all-events');
If it makes sense for your use case, you could also use URL parameters. For example, if your Course models belong to a Category model, you might do something like this:
Route::get('/courses/{category}', [EventsController::class, 'allCourseList');
Then in your countroller, you define the allCoursesList function like so:
public function allCoursesList(Category $category)
{
// ... do something with $category which is an instance of the Category model.
}
You can use Route Parameters to pass variable from route to controller.
// web.php
Route::get('/events/{all}', [EventsController::class, 'eventsList'])->name('event-list');
then in your controller you can access the variables
public function eventsList(Request $request,$all){
if($all==1){
//your logic for handling the condition when $all =1
}
// the remaining condition
}
if you have multiple parameters you can pass them like so, you can use ? for optional parameter.
// web.php
Route::get('/courses/{param_one}/{param_two?}', [EventsController::class, 'allCoursesList'])->name('all-events');
then in your controller you can access the variables
public function allCoursesList(Request $request,$paramOne,$paramTwo){
return $paramOne.' '.$paramTwo;
}
to access the query parameter
// web.php
Route::get('/evets', [EventsController::class, 'allCoursesList'])->name('all-events');
if the route were /events?timeframe=0&category=1 you can access the query parameter like so
public function allCoursesList(Request $request,$paramOne,$paramTwo){
$timeframe= $request->query('timeframe');
// do this for all the query parameters
}
I need to pass array to route and controller from view.
I get the error:
Missing required parameters for [Route: actBook] [URI: bookfromindex/actBook/{id}/{array}].
I have my route defined as:
Route::get('/bookfromindex/actBook/{id}/{array}', 'BookController#actBook')->name('actBook');
My controller function is defined as:
public function actBook(Request $request, $id, $array){
And I call this route in my view using:
დაჯავშნა
How do I prevent this error?
Just change -
დაჯავშნა
to -
დაჯავშნა
First, you need to serialize your array then you can pass into the parameter
Example :
{{ $serializeArray = serialize($array) }}
<a href="{{ route('actBook', $room->id, $serializeArray) }}" class="btn btn-default">
Controller :
public function actBook(Request $request, $id, $array){
Route :
Route::get('/bookfromindex/actBook/{id}/{array}', 'BookController#actBook')->name('actBook');
Hope this will help you.
Just use serialize($array);
Then pass this array to the route.
I have a simple app, I need to pass two different ID's id and code_id in a route, here is my solution I have tried so far
view
{{ __('Code') }}
Here is route config
Route::get('settings/code/{id}/{code_id}', ['as' => 'settings.code', 'uses' => 'SettingController#code']);
Here is my function in a controller
public function code($code_id, $id)
{
$settings = Setting::find($code_id, $id);
dd($settings);
return view('pages.settings.code', compact('settings'));
}
Here is the error I get
Missing required parameters for [Route: settings.code] [URI: settings/code/{id}/{code_id}]. (0)
What is wrong with my code?
First you should pass an array as 2nd argument to route() method:
{{ route('settings.code', ['id' => $settings->id, 'code_id' => $settings->code_id]) }}
And note that:
Route parameters are injected into route callbacks / controllers based on their order - the names of the callback / controller arguments do not matter.
So you should swap the arguments of your controller's method:
public function code($id, $code_id)
{
//...
}
I got my solution like this:
Route::match(["get", "post"], "/patient-member-orders/**{patient_id}/{member_id}**", [PathologyController::class, "patientMemberOrders"])->name("pathology-patient-member-orders");
After that we need to pass that name in our route method's second argument as an array in the Key Value pair.
**route("pathology-patient-member-orders", ["patient_id" => $member->patient_id, "member_id" => $member->id])**
Please correct me if I am wrong.
Chinmay Mishra
I have a route like this:
Route::post('{object}', 'CommentController#store')->name('store');
And I want to pass an object as argument to my form action like this:
<form action="{{ route('comment.store', $object) }}" method="post" id="comment--form"></form>
Here is the problem
if I pass my query object as you know I will get the id of query object
if I pass something like this: {{ route('comment.store', (new app\anyObject())) }} I will get exception and for some polymorphism reason I can't use model binding because the object could be any object so I'm wondering if there is any solution for my question
PS
I have a polymorphic comment system and I dont know if it's decoration design pattern or not but I want to save comments dynamically via my interface so I want to save comments dynamically like this:
public function store(Request $request, $object)
{
// comments() is an implementation of an interface
$object->comments()->create([
'name' => auth()->check() ? $request->user()->fullName() : $request->name,
'email' => $request->user()->email ?? $request->email,
'comment' => $request->content,
]);
}
But I can't pass an object as argument is there anyway to pass it through route or any other idea or solution?
Thanks.
You can't pass any object array or form-data in the url . you might want to consider doing base64_encode then decode after in your method. This can solve your issue.
I've constructed a select list in a dashboard view, which lists id and name of various components. The data is passed to a controller that makes a view using the passed id to pull up the correct component data for that id. Problem is that the controller is constructed to expect an object from which to get the id, so that when I submit the id from the list, I get a "Trying to get property of non-object" error. (It doesn't matter whether I submit to the route or directly to the controller; I get the same error.) Here's the code:
PagesController (that creates list array for the dashboard):
public function showDashboard()
{
$components = Component::lists('name','id');
return View::make('dashboard', array(
'components'=>$components, ...
));
}
Snippet of source code for the select list:
<form method="GET" action="https://..." accept-charset="UTF-8">
<select id="id" name="id"><option value="2">Component Name</option>...
Components Model:
class Component extends Eloquent {
protected $table = 'components'; ... }
ComponentsController:
public function show($id)
{
$component = $this->component->find($id);
return View::make('components.show', array(
'component'=>$component, ...
));
}
dashboard.blade.php:
{{ Form::open(array(
'action' => 'ComponentsController#show',
'method'=>'get'
)) }}
{{ Form::Label('id','Component:') }}
{{ Form::select('id', $components) }}
{{ Form::close() }}
The same controller code is used for other purposes and works fine, for example, when I pass a specific id from a URL, it accepts that id without an error. So, I know this should be something simple involving the form opening, but I can't figure it out. How can I fix this? Thanks!
It will not work because using get method the url will be like this.
http://laravel/puvblic/show?id=2
and laravel will not accept it , rather it accepts a parameter for function in this way
http:/laravel/puvblic/show/2
A better way to do this would be to make your form method as 'POST' . It would be much safer and better this way. and modify your function as.
public function show()
Then , You can get the id in your controller as
Input::get('id')
EDIT:
To make it simple , try this one:
Route::get('{show?}', function()
{
$id = Input::get('id') ;
echo $id; //This will give you the id send via GET
die();
});
Simply follow the GET method , your form will send a GET request and it will come to this route here you can perform your desired function.
I finally figured out the problem, and the solution:
The problem is that the form should (optimally) be sent as POST (not GET), and therefore not changed from the default value provided by Blade. BUT then the route has to be registered correctly as POST, and that's what I wasn't doing before. So,
dashboard.blade.php:
{{ Form::model(null, array('route' => array('lookupsociety'))) }}
{{ Form::select('value', $societies) }} ...
Route:
Route::post('lookupsociety', array('as'=>'lookup society', 'uses'=>'SocietiesController#lookupsociety'));
SocietiesController#lookupsociety:
public function lookupsociety()
{
$id = Input::get('value'); ... // proceed to do whatever is needed with $id value passed from the select list
}
It works perfectly! The key was changing the method in the route to Route::post() instead of Route::get().
I knew it had to be simple -- I simply hadn't stumbled on the solution before :)