I want to message an email very user which is admin. But this code shows me the error Call to undefined method Illuminate\Database\Eloquent\Builder::all(). So how can I send every admin email message?
Try this:
public function index()
{
// notify a single user
$user = User::where('role', 'Admin')->first();
$project = [
'greeting' => 'Hi '.$user->login.',',
'body' => 'This is the project assigned to you.',
'thanks' => 'Thank you this is from codeanddeploy.com',
'id' => 1
];
$user->notify(new EmailNotification($project));
return view('customers', [
'customers' => Customer::all()
]);
}
To notify multiple users try
$users = User::where('role', 'Admin')->get();
foreach($users as $user) {
$user->notify(new EmailNotification($project));
}
Related
I have a POS Application and Inventory Application hosted differently but sharing the same database. I want to log into the Inventory from the POS System in laravel. I have worked something out but isn't working. Please I need assistance. It redirects me to the invenotry system login page. it's unable to authenticate and login me into the inventory system. Please any assistance?
This is the login function in my controller from the POS SYSTEM
public function inventoryLogin(){
$response = Http::asForm()->post('http://inventory.sys/api/login/from-pos', [
'email' => auth()->user()->email,
'role' => auth()->user()->role,
]);
return $response;
}
Also, my API.php in my INVENTORY SYSTEM
Route::post('login/from-pos', function(Request $request){
$rules = array(
'email' => 'email|required|max:300',
'role' => 'required|max:300',
);
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return $validator->errors();
}
if (request('role') == "Administrator") {
$user = User::where('email', request('email'))->firstOrFail();
$token = $user->createToken('auth_token')->plainTextToken;
auth()->login($user);
return redirect('/dashboard');
}else{
return response([
'status' => 419,
'Message' => "Sorry! unauthorized user"
]);
}
});
I want to validate Email and phone no. on update. but if the email already belongs to the id then it shouldn't validate, also if the email belongs to another user it should validate. same applies for the phone no. I tried unique on both email and phone_no it's not working
below is my controller code
public function edit($id)
{
$userlist= Userlist::find($id);
//dd($data);
return view('userlist.edit',compact('userlist'));
}
public function update(Request $request, $id)
{
$request->validate([
'firstname' => 'required',
'lastname' => 'required',
'email_id' => 'required|email',
'phone_no' => 'required|numeric',
]);
$userlist = Userlist::find($id);
$userlist->firstname = $request->firstname;
$userlist->lastname = $request->lastname;
$userlist->email_id = $request->email_id;
$userlist->phone_no = $request->phone_no;
$userlist->update();
return redirect()->route('userlist.index')->with('success', 'User updated Successfully!');
}
Assuming that your UserList table is called users, you can do this. If it's name other than users just change it below
'email' => 'required|email|unique:users,email,'. $id .''
I am trying to redirect users to their dashboard after successful login and if credentials doesn't exists or match the database records it should return laravel's default error messages.
It redirects to dashboard view if successful, but when an errors occurs, it doesn't redirect me back to the login, instead it shows a blank page.
below is my code
public function login(Request $request)
{
$message = array(
'required.email' => 'This is required',
'required.password' => 'This is required',
);
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:8',
]);
$email = $request->email;
$pass = $request->password;
if (Auth::guard('guest')->attempt([
'email' => $email,
'password' => $pass
], $request->get('remember'))) {
return redirect()->route('dashboard');
} else {
return back()->withInput($request->only('email', 'remember'));
}
}
this should suffice your need
return redirect('dashboard')->withErrors('Something went wrong!');
I have the following login controller :
public function userLogin(Request $request) {
$rules = [
'email' => 'required|exists:users',
'password' => 'required'
];
$request->validate($rules);
$data = [
'email' => $request->get('email'),
'password' => $request->get('password'),
];
if(Auth::attempt($data))
{
$user = Auth::User();
$userid = Auth::User()->id;
return response()->json([
'user' => $user,
'token' => $user->createToken('yourAppName')->accessToken ,
$userid
]);
}
else
{
return response()->json('Unauthorized', 401);
}
}
My web.php ( Token is generated with Laravel Passport )
Route::get('/customize/{id}', function ($id) {
if (
User::where('id', Auth::User()->id)->whereHas('profile', function ($query) use ($id) {
return $query->where('id', $id);
})
->exists()
) {
return view( 'welcome' );
}
return "no";
});
For some reason, when testing my logincontroller with POSTMAN, I actually receive a value for Auth::User()->id . But in web.php I receive Trying to get property 'id' of non-object. Any idea ?
here is a solution
you can find that user in db with email and password you have and get its id
and then use LoginUsingId($id) to login your user
Find out why you are not considered as an authenticated user. I can guess you send Authentication token in your request headers when you use Postman. that this is not possible when you hit the address in the browser directly.
I am trying to use Socialite and JWT together, however I am facing with some issues.
My goal is to update user's profile_image_url field in database if user already exists and give him token, if user doesn't exist, then create it and also give him token.
This is what I am doing right now:
public function handleProviderCallback(Request $request){
try{
$providerUser = Socialite::driver('facebook')->stateless()->userFromToken($request->fb_token);
$user = User::query()->firstOrNew(['email' => $providerUser->getEmail()]);
}catch(Exception $e){
return new JsonResponse(['status' => 'error', 'message' => 'Woops, some error happened']);
}
if(!$user->exists){
$user->name = $providerUser->getName();
$user->profile_image_url = $providerUser->getAvatar();
$user = $user->save();
}else{
$user->update(['profile_image_url' => $providerUser->getAvatar()]);
}
$token = JWTAuth::fromUser($user);
return $this->onAuthorized($token, $user);
}
And this is what I am returning onAuthorized:
protected function onAuthorized($token, $user){
return new JsonResponse([
'status' => '40002',
'message' => 'Successfully logged in with facebook',
'user' => [
'user_id' => $user->id,
'name' => $user->name,
'email' => $user->email,
'profile_image_url' => $user->profile_image_url,
'token' => $token
]
]);
}
But this gives me the following error on first try if user doesn't exist, even tho it saves the user into the database:
Type error: Argument 1 passed to Tymon\JWTAuth\JWT::fromUser() must
implement interface Tymon\JWTAuth\Contracts\JWTSubject, boolean given,
called in
/var/www/goodzapp.com/api/vendor/illuminate/support/Facades/Facade.php
on line 221
What I am doing wrong? Can someone please explain me. Any help would be much appreciated.