Doesn't work route model binding in laravel 8 - laravel

My route:
Route::get('/users/{user}', function(App\Models\User $user) {
dd($user);
});
where {user} - id of model. A user with this name is in the database.
I check so:
Route::put('/users/{user}', function(int $id) {
dd(App\Models\User::find($id)->toArray());
});
I tried to bind in RouteServiceProvider:
Route::bind('user', function ($value) {
return App\Models\User::where('id', $value)->firstOrFail();
});
UPDATE
When binding, I get an empty instance
What could be the problem?

its been awhile, if anyone still need it try route:clear

Related

I have problem when i logout from admin "Trying to get property 'id' of non-object" using laravel

I am trying to logout from admin but unfortunately, I face error Trying to get property 'id' of non-object How to fix this error? please help me thanks.
public function index(){
$user_permission = Users_Permissions::with('user')
->Where('user_id',Auth::user()->id)
->paginate(5);
return view('index',compact('user_permission'));
}
The problem is clear. When you log out, then Auth::user() is null. so there is no id . You can solve the issue like this.
public function index()
{
if (Auth::check()) {
$user_permission = Users_Permissions::with('user')->Where('user_id',Auth::user()->id)
->paginate(5);
return view('index',compact('user_permission'));
} else {
// The condition when no user logged in
// For an example
return redirect('login'); // This is just an example
}
For that, use try & catch in every function, and use auth middleware for specific routes while you are working with auth users.
so that specific routes are required to use auth, so you don't need to check in every function. auth is always present.
Route::middleware('auth')->group(function () {
Route::get('logout', 'AuthController#logout');
});
or else you can use middleware in a controller too,
class AuthController extends Controller
{
public function __construct(){
$this->middleware(['guest'])->except('logout');
}
}
It's because after logout you don't have user ID, so in this situation the User is null, and you want to get the if from null object. you can change this part of code like below
Users_Permissions::with('user')->Where('user_id',Auth::user()->id ?? 0)
then you pass the error. you can put every number or null in query based on your needs.

Return Current User in Route (User Object doesnt get passed through)

Basically, very simple: just a theoretical question.
Route::get('/', function(User $user) {
dd($user->id);
//Returns null
So how come, this returns null ?
i am passing through the (logged in) User object but it does not come through. Isnt this supposed to go like that?
yeah i mean you could go and say get the Auth instance and whatsoever, but generally speaking, this should definitely pass through the user object of the current logged in user right?
use App\User;
Route::get('/', function(User $user) {
echo Auth::user($user);
});
or
use Illuminate\Http\Request;
Route::get('/', function(Request $request) {
echo $request->user();
});
What you could do is use the Request to get the user object, and with that you have full access to whatever data you need from the user object.
// don't forget to include
use Illuminate\Http\Request;
Route::get('/', function(Request $request) {
dd($request->user());
});
From there you can use $request->user()->id; to get the current user id.
Retrieving The Authenticated User id
Route::get('/', function() {
dd(Auth::user()->id);
//or for laravel 5.7 or above
dd(Auth::id()); });
Try this code
if you use Auth :
Route::get('/', function() {
return Auth::user()->id
}
else
Route::get('/', function(User $user,$username) {
$user = $user->where('username',$username)->first();
return $user->id
}

Laravel show() parameter are null

I am using Laravel version 5.6.26. My show() parameter get null value. Why? and how to solve it? Thank you
web.php
Route::get('/peoples', 'UsersController#index')->name('peoples');
Route::get('/peoples/{id?}', 'UsersController#show');
UserController.php
public function show(User $user)
{
$user = User::where('id', $user->id)->first();
//$user = User::find($user->id)->first();
return dd($user);
//return view('profile',['user' => $user]);
}
user.blade.php when I click <a href="/peoples/{{$user->id}}" > , it show null
<h4><a href="/peoples/{{$user->id}}" > {{$user->name}}</a></h4>
result
https://imgur.com/QlaiFse
But if i do like this,I can get the value
public function show(User $user)
{
$user = User::where('id',1)->first();
//$user = User::find($user->id)->first();
return dd($user);
//return view('profile',['user' => $user]);
}
The way you defined your show method i.e. show(User $user) is called route model binding if your variable name ($user) is similar to your route declaration. But your route declaration is /peoples/{id?} so the User instance is not created in your controller. So just rename the route declaration as /peoples/{user?}.
The use case of route model binding is you don't want to query/load the model related to a route parameter. So if you define the route the way I've mentioned you won't need the $user = User::where('id', $user->id)->first(); query. Because Laravel will automatically instantiate that $user with the User model of id passed in the route.
If you want to query/load the User model manually then keep your existing route declaration as it is. And change your controller method declaration as show($id = null)
You are injecting the User model in your controller, but not accessing the route path. If you want to access the {id} which set in the route, you have to access that in your controller too.
So your UserController will be something like
public function show(User $user, $id)
{
$user = User::where('id', $id)->first();
dd($user);
}
When you type hinted in your controller's method show() with User, Laravel will automatically inject that using Service Container.
Your UserController.php's code should be:
public function show(User $user) {
// no need to fetch user again. Laravel had already done that for you.
dd($user);
return view('profile', compact('user'));
}

Laravel Model Binding

I have issue with model binding in routes and controller. Here is my routes:
Route::group(['prefix'=>'services/devops/domain-names'], function () {
Route::group(['middleware'=>'auth'], function () {
Route::get('/editAutoRenew/{domainname}', 'EnomController#editAutoRenew');
});
Route::post('/', 'EnomController#checkDomainName');
});
And here is function
public function editAutoRenew(DomainName $domainname)
{
dd($domainname);
}
But this gives me empty model. Why? How can I get my model?
I tried to route:list and there it shows {domainname}
You get in argument integer type, not object. Try to rewrite your function with:
public function editAutoRenew($domainname)
{
$domainnameObj = DomainName::findOrFail($domainname);
}
When you use:
DomainName::first();
You get all entries from your table domain_name, then command first() - get first of this records.

Is it possible with laravel to go from post.store to post.edit?

I can't figure out how to go from a store method directly to a edit page.
Routes:
Route::post('/', 'PostController#store')->name('posts.store');
Route::get('/', 'PostController#edit')->name('posts.edit');
post controller:
public function store(Request $request)
{
$post = new Post;
$post->save();
return view('posts.edit');
}
public function edit($id)
{
dd('edit post');
}
I keep getting view not found or other errors. I have checked php artisan route:list and the correct route is there. What am i missing here?
Well you should do a redirect to the route instead. See Redirecting to Named Routes
So this should work:
return redirect()->route('posts.edit');
At this point, you are trying to load a view.

Resources