Route not defined when passing parameter to a route - laravel

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
}

Related

Laravel: What are functions in routes doing?

Can anyone tell why the documentation of Laravel, and others, show functions in routes that return / do something? In what context can you use this?
For example, I try to figure out Molly Connect.
Here is the corresponding code from https://github.com/mollie/laravel-mollie/blob/master/docs/mollie_connect.md
Route::get('login', function () {
return Socialite::with('mollie')
->scopes(['profiles.read']) // Additional permission: profiles.read
->redirect();
});
Route::get('login_callback', function () {
$user = Socialite::with('mollie')->user();
Mollie::api()->setAccessToken($user->token);
return Mollie::api()->profiles()->page(); // Retrieve payment profiles available on the obtained Mollie account
});
Its just a shortcut, to avoid having to create separate controller files and indirectly referencing those functions. Functionally, your example is no different from doing this:
Route::get('login_callback', 'LoginController#callback')
And then, LoginController.php
class LoginController
{
public function callback()
{
$user = Socialite::with('mollie')->user();
Mollie::api()->setAccessToken($user->token);
return Mollie::api()->profiles()->page();
}
}
See here

Automatically Load/Show Laravel View File

I am thinking of any techniques of autoloading the view files according to url.
For example:
public function addProducts()
{
return view('admin.addProducts');
}
public function editProducts()
{
return view('admin.editProducts');
}
public function allProducts()
{
return view('admin.allProducts');
}
Here, the Controller's method name is identical to view file name. So, I am thinking, if it is possible to load the view files without writing same kind of method again and again.
Enlighten me.
If your route only needs to return a view, you may use the Route::view method.
For example:
Route::view('/welcome', 'welcome');
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
read more here
It's the call PHP magic, man. https://repl.it/#Piterden/PHP-call-magic?language=php
public function __call($method, $parameters)
{
if (str_contains($method, 'Product')) {
return view("admin.{$method}");
}
}
btw, it's not a good practice for controller.

Implicit route, with firstOrCreate instead of findOrFail

Is it possible to create an implicit route where if it is not found the thing is created? I am using Laravel 5.5.13.
For instance this is my implicit route:
Route::post('thumbs/{player}', 'ThumbController#store');
And in my controller it is this:
public function store(Request $request, Player $player)
{
$thumb = new Thumb($request->all());
$player->thumbs()->save($thumb);
return response()->json($thumb, 201);
}
So now if I go to the endpoint of ..../api/thumb/1 it will create a thumb related with Player of id 1. However instead of a id number I wanted to provide it a string like this:
..../api/thumb/PLAYER_NAME
So example of ..../api/thumb/Blagoh, then my endpoint should first find if a player exists by name "Blagoh", and if it doesn't then it should create it. I couldn't figure this one out.
What you should do is adding into boot method of RouteServiceProvider something like this:
Route::bind('player', function ($value) {
if ($player = \App\Player::find($value)) {
return $player;
}
return Player::create(['name' => $value]);
});
It's called explicit binding and you can update logic as showed above. Reference - Route model binding

Laravel Backpack - getting current record from crud controller

In my crud controller I am trying to get the name of the person who is currently being edited.
so
http://192.168.10.10/admin/people/93/edit
In the people crud controller
public function setup() {
dd(\App\Models\People::get()->first()->name)
}
This returns the first person not the person currently being edited.
How do I return the current person (with an id of 93 in this example)
Ok, So since you use backpack look into CrudController to see how the method looks:
public function edit($id)
{
$this->crud->hasAccessOrFail('update');
$this->data['entry'] = $this->crud->getEntry($id);
$this->data['crud'] = $this->crud;
$this->data['fields'] = $this->crud->getUpdateFields($id);
$this->data['id'] = $id;
return view('crud::edit', $this->data);
}
So now you can overwrite the edit function and change whatever you want. You can even create a custom edit page if you so wish.
Setup on the other hand is usually used to add things like
$this->crud->addClause(...);
Or you can even get the entire constructor and put it in the setup method because setup call looks like this:
public function __construct()
{
// call the setup function inside this closure to also have the request there
// this way, developers can use things stored in session (auth variables, etc)
$this->middleware(function ($request, $next) {
$this->setup();
return $next($request);
});
}
So you could do something like \Auth::user()->id;
Also it's normal to work like this. If you only use pure laravel you will only have access to the current id in the routes that you set accordingly.
Rahman said about find($id) method. If you want to abort 404 exception just use method findOrFail($id). In my opinion it's better way, because find($id)->name can throw
"Trying to get property of non-object error ..."
findOrFail($id) first fetch user with specified ID. If doesn't exists just throw 404, not 500.
The best answer is:
public function edit($id)
{
return \App\Models\People::findOrFail($id);
}
Good luck.
you need person against id, try below
public function setup($id) {
dd(\App\Models\People::find($id)->name);
}

Missing argument 1 in laravel controller

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

Resources