Method Illuminate\Auth\SessionGuard::users does not exist - laravel-5.6

I'm having a problem with Auth. I'm just learning about Laravel, I'm doing login. I don't know how to fix it it says:
Method Illuminate\Auth\SessionGuard::users does not exist.
this is my code in login function
public function getlogin(Request $request){
$this->validate($request, [
'email'=> 'required|max:32',
'password'=> 'required|max:32|min:8',
]);
if (Auth::attempt(['email'=>$request->email,'password'=>$request->password])) {
$user = users::where('email','=',$request->email)->first();
return redirect('/messenger')->with('usersignin');
}
return "ooopps something wrong";
}
and this is where the name from the database will be display
<div class="">
<h1>Welcome
#if(session('user'))
{{session('user')}}
#elseif(session('usersignin'))
{{ucwords(Auth::users()->fname)}}
#endif</h1>
</div>

You need to use user instead of users, user is provided with Auth and will get the current logged in user id.
$id = \Auth::user()->id;
Or you want to get the user
$user = \Auth::user();

I solved this error by running the below command
php artisan jwt:secret
php artisan cache:clear
php artisan config:cache

in your if statement just use it as below
if (auth()->attempt(['email'=>$request->email,'password'=>$request->password])) {
$user = users::where('email','=',$request->email)->first();
return redirect('/messenger')->with('usersignin');
}

Related

Laravel spatie , assignrole() is not working when team_id is used?

this is modified seeder code
$user = User::where('id', 1)->get();
$role = Role::where('id', 1)->get();
echo $role;
$user->assignRole([$role->id]);
this is the output
[{"id":1,"team_id":2,"name":"Admin","guard_name":"web","created_at":"2023-01-10T06:40:56.000000Z","updated_at":"2023-01-10T06:40:56.000000Z"}]
Exception
Property [id] does not exist on this collection instance.
at vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php:969
965▕ */
966▕ public function __get($key)
967▕ {
968▕ if (! in_array($key, static::$proxies)) {
➜ 969▕ throw new Exception("Property [{$key}] does not exist on this collection instance.");
970▕ }
971▕
972▕ return new HigherOrderCollectionProxy($this, $key);
973▕ }
1 database/seeders/CreateAdminUserSeeder.php:32
Illuminate\Support\Collection::__get("id")
+22 vendor frames
24 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
you can see that the id is there on echo but I cant assign the role using its id
this only happed when teams set to trues in config file.
I am missing something or is it a bug?(highly doubt it )
$role = Role::where('id', 1)->get(); returns a collection of roles
Instead you can either do:
$role = Role::find(1);
or
$role = Role::where('id', 1)->first();
To clear the cache when adding new permissions/roles run the following artisan commands:
php artisan cache:clear
For reference you can run the following to clear everything cached:
php artisan cache:clear
php artisan view:clear
php artisan config:clear
php artisan route:clear

Laravel telegram bot sdk

I have test command and I want to call or sent request this command inside my controller, pls help me!
You can call the PHP artisan command inside your controller using the following way.
use Illuminate\Support\Facades\Artisan;
$project = TelegramBaza::select('name')->where('chat_id', $chatId)->first();
if($project && $project->name == 'ANORHOME'){
//Call artisan command
$exitCode = Artisan::call('message:send', [
'user' => $user, '--queue' => 'default'
]);
}
Documentation

This action is unauthorized in laravel 8

I'm using spatie/laravel-permission and when I add my permission to my api route, I'm getting
message: "This action is unauthorized.
but my user does have permission
My route
Route::get('/customer/items', [ CustomerController::class, 'getItems'])->middleware('can:customer');
My CustomerController
public function getItems()
{
$items = Item::all();
return [
'items' => $items
];
}
I was this problem because my guard was auth:api
Route::prefix('coin')->middleware(['auth:api','can:manageCoins'])->group(function () {
Route::get('', [CoinController::class, 'getAllPagination']);
});
in role table and permission table filed guard_name = web
I change all guard_name to api
fixed it middleware can work for guard api
note : run this command php artisan optimize:clear

Laravel 404 Not Found - But Route Exists

Hello im trying to get information about user on my view.
Here is my UserController
public function getUser(Request $request, $id)
{
$user = User::findOrFail($id);
return view('admin.user', ['user' -> $user]);
}
here is my web.php
Route::get('admin/user/{id}', "UsersController#getUser");
and my user view
#extends('admin.layouts.app')
#section('contents')
<h1>User {{ $user }} </h1>
#endsection
I am trying to display user information in this view, like name etc, but im recives 404
Not Found page. What im doing wrong. Im using Laravel 6
404 error may refer to a User not being found, since you have a findOrFail() query. It may have nothing to do with your routes.
Just double check with:
php artisan route:list
just to make sure the route is being registered correctly.
I think you firstly use any prefix for this route.For this it will give you an error.To check route list.
php artisan route:list
it will give you all route.
And Here you don't need (Request $request) because here you just need the id.it not the problem..i give you just this suggestion
public function getUser($id)
{
$user = User::findOrFail($id);
return view('admin.user', ['user'=> $user]);
}
why you use '-> ' you should use '=>'

Login by code seems to not work in laravel

Basically i'm trying to send by email a link that lets you login with a specific account and then redirects you to a page.
I can seccessfully generate link and send them via email using URL functionalities in laravel using this code:
Generating the link:
$url = "some/page/".$travel_id;
$link = URL::temporarySignedRoute(
'autologin', now()->addDay(), [
'user_id' => 3,
'url_redirect' => $url,
]
);
And sending the mail:
Mail::send('emails.travel', $data, function ($message) use ($data) {
$message->from('mail#mail.com', 'blablabla');
$message->to('reciever#mail.com', 'blablabla')->subject('test');
});
There is a route that catches the link sent by mail that is supposed to log you in with the user (in this case, the one with the id '3') and redirect you to some page but when it redirects, it prompts you to the login page, as if you are not logged.
Here is the route:
Route::get('/autologin', function (Request $request) {
$user = User::findOrFail($request->user_id);
if (! $request->hasValidSignature()) {
abort(403);
}
Auth::login($user);
return redirect($request->input('url_redirect'));
})->name('autologin');
When i try to do a Auth::check() after the Auth::login($user); it returns true, so is the user logged in?
I also tried to use Auth::loginUsingId($request->user_id); with no different results.
Any idea of what's happening?
So i found the problem,
I was logging in with a backpack user but i was using the default auth feature of laravel.
Turns out i need to use: backpack_auth()->login($user); instead of Auth::login($user); if i want to login using a backpack user.
Also use backpack_auth()->check() instead of Auth::check().

Resources