Missing argument 1 in laravel controller - laravel

In routes.php I have following route
Route:: get('/crm/hotel/occupant/{id}', array('uses'=>'OccupantController#occupant','as'=>'crm.hotel.occupant'));
for the above route ... if i put the controller like this it's work...but if i remove the $room_id in model calling like
$hotel = new Occupant();
.. i got an error missing argument 1 ....
public function occupant($room_id)
{
$hotel = new Occupant($room_id);
// manage page
return $hotel->occupant($room_id);
}
how to solve it ...

You can make {id} optional.
It is achieved by this:
Route:: get('/crm/hotel/occupant/{id?}', array('uses'=>'OccupantController#occupant', 'as'=>'crm.hotel.occupant'));

as explained by #vinweb you have to add a question mark ? to your id parameter,
Route:: get('/crm/hotel/occupant/{id?}', array('uses'=>'OccupantController#occupant','as'=>'crm.hotel.occupant'));
but you also have to set your variable to a default value (example taken from the official doc,):
Route::get('user/{id}', function ($id) {
return 'User '.$id;
});
So, in your case it would be probably be something like this :
public function occupant($room_id = null)
{
$hotel = new Occupant($room_id);
// manage page
return $hotel->occupant($room_id);
}

Related

Route not defined when passing parameter to a route

I have a named route with a parameter which looks like this...
Route::get('/profile/{user_id}', [ProfileController::class, 'profile'])->name('profile');
Now in one of my controller,
I have a function that calls this route like this
public function _myFunction($some_data) {
return redirect()->route('profile', [$some_data->user_id]);
}
and in my ProfileController's profile() function, I have this.
public function profile() {
return view('modules.profile.profile');
}
I've followed the documentation and some guides I found in SO, but I got the same error,
"Route [profile] not defined."
can somebody enlighten me on where I went wrong?
Here's what my routes/web.php looks like...
Route::middleware(['auth:web'])->group(function ($router) {
Route::get('/profile/{user_id}', [ProfileController::class, 'profile'])->name('profile');
});
When calling the route, you should pass the name of the attribute along with the value (as key vaue pairs). In this case, your route is expecting user_id so your route generation should look like this:
return redirect()->route('profile', ['user_id' => $some_data->user_id]);
Read more on Generating URLs To Named Routes in Laravel.
I solved the issue, and its really my bad for not providing a more specific case information and made you guys confused.
I was using socialite and called _myFunction() inside the third party's callback..
After all, the problem was the socialite's google callback, instead of placing the return redirect()->route('profile', [$user->id]) inside _myFunction(), what I did was transfer it to the callback function.
So it looked like this now...
private $to_pass_user_id;
public function handleGoogleCallback()
{
try {
$user = Socialite::driver('google')->user();
$this->_myFunction($user);
return redirect()->route('profile', [$this->to_pass_user_id]);
} catch (Exception $e) {
dd($e->getMessage());
}
}
public function _myFunction($some_data) {
... my logic here
$this->to_pass_user_id = $some_value_from_the_logic
}

Not able to get Route parameter - Laravel 6

I have tried multiple solution but nothing worked yet, i am trying to get route Parameter in controller that was passed from a view.
Here is how i have created the route:
Route::get('addOptions/{questionId}', 'QuestionController#addOptions')->name('addOptions');
Here is how i am passing parameter to route from view:
Add Options
And here is what i am trying to get in controller but it's returning empty array:
public function addOptions(Request $request)
{
$allParameters = $request->input(); //not working
//$allParameters = $request->all(); //not working
//$allParameters = Input::all(); //not working
return $allParameters;
}
It returns empty array [] like this.
EDIT: But url at route addOptions look like this http://127.0.0.1:8000/admin/addOptions/4 in which 4 is questionId which means parameter is being passed but not retrieved.
What am I doing wrong here? Please guide, Thanks.
You should be passing the route like this:
Add Options
as for Laravel docs. the route params are passed an array with the key referencing the param
$url = route('profile', ['id' => 1]);
To retrieve the data in your controller, you should use:
$request->route()->paremeters()
or
$request->route('parameter_name')
If you want to pass the parameter
Add Options
I think your function parameters are wrong
You are passing question id from Route So your function should be like
public function addOptions($questionId)
{
$allParameters = $questionId; // you question ID passed throught Route
return $allParameters;
}

Missing argument 1 for App\Http\Controllers\ProductsController::edit()

Im working on editing of an already saved product and i am getting the following error message on my browser
ErrorException in ProductsController.php line 88:
Missing argument 1 for App\Http\Controllers\ProductsController::edit()
in ProductsController.php line 88
My route controller is as below:
Route::get('productsedit', array('as'=> '/productsedit','uses'=>'ProductsController#edit'));
The function is as below
public function edit($id)
{
//find the post in the db and sav it as a variable
$product = Products:: findOrFail($id);
//return view and pass in the var previously created
return view('/productsedit')->withProducts($product);
}
Where iam i going wrong
In your edit method on your ProductsController you require a parameter ($id), but you don't have that value in your route . Which is what this error is saying.
[Missing argument 1 for
App\Http\Controllers\ProductsController::edit()]
Your route:
Route::get('productsedit', array('as'=> '/productsedit','uses'=>'ProductsController#edit'));
Will have to change to something like this:
Route::get('products/{$id}/edit', 'ProductsController#edit');
When calling the route in your view it will have to look something like this:
'products/{{$product->id}}/edit'
Extra:
You might want to take a look at Resource controllers since you are not really following restfull practices when it comes to your routes
https://laravel.com/docs/5.4/controllers#resource-controllers
your should give route id
Route::get('productsedit/{id}', array('as'=> '/productsedit','uses'=>'ProductsController#edit'));
and in your function should like this
`public function edit($id = null)
{
//find the post in the db and sav it as a variable
$product = Products:: findOrFail($id);
//return view and pass in the var previously created
return view('/productsedit')->withProducts($product);
}`
hope this work !!!

conditional nested routes in laravel

I want to check segment of particular URL on route and based on value of segment decide it to handle to another route.Somewhat like below:
Route::get('{module}/{seg}', function(){
if (is_numeric((Request::segment(3)) {
return Route::get('{module}/{seg}',Request::segment(2) . 'Controller#index');
}else{
return Route::get('{module}/{seg}',Request::segment(2).'Controller#index' . Request::segment(3));
}
});
I don't think above code works but can anyone suggest a working code for implementing above logic in laravel?
I'd suggest adding it as an optional parameter, and handle differences in the controller. Given your code, it might look like this, for instance:
// route
Route::get('{module}/{seg}/{param?}', 'Controller#index');
// controller
public function index($module, $seg, $param = null)
{
// for dynamic index methods
if (is_numeric($param)) {
$method = 'index' . $param;
return $this->{$method}();
}
// for non-numeric third-segment params, continue here as usual
}

how to pass arguments to filter in laravel4.2

in my project , i want to pass 'user_id' to filter to check.
this is isAdmin function:
public function isAdmin($id){
$user=User::find($id);
if($user->role==10){
return true;
}
return false;
}
and this is my route file:
Route::group(array('before'=>'admin'),function(){
Route::get('dashboard','UserController#Dashboard');
});
this is my filte that iwant give user_id ($id), and idont know how.
Route::filter('admin',function($id)
{
if(Auth::guest() or ! Auth::user()->isAdmin($id))
return Redirect::guest('user/logout');
});
For this case, you can directly check from the filter like this:
Route::filter('admin',function()
{
if(Auth::guest() or Auth::user()->role != 10)
return Redirect::guest('user/logout');
});
You do not need to pass params.
Also have a look to this link if you really want to pass params.
Link of Laravel Doc: here

Resources