I'm trying to authenticate 2 users by inheritance in a Laravel project.
In my migration I have only 1 column that can be null, that column is email.
With that column I'm expecting to double authenticate professors and alumns, I have also 2 types of registers, one has the input email and the other not.
In my database I have 2 users, one is professor, and the other alumn, professor has email, and the other has email also, because they belong to the same table but that email is NULL in alumn row.
I'm trying to check when I login if that user with email column is null, my view returns alumn.
If it's not null my view returns professor.
I tried to check if email is null in my Laravel controller.
This is my code
public function index()
{
$user = User::where('email', '=', Input::get('email'))->first();
if ($user == 'NULL'){
return ('alumn');
}
if ($user != 'null'){
return ('professor');
}
}
And my Laravel router looks like this.
Route::get('/home', 'HomeController#index')->name('home');
I also tried this function in my controller instead the other one.
if (User::where('email', '=', Input::get('email'))->count() > 0) {
// user found
}
And with exists() instead of count().
If you are wondering, I'm returning just a string right now for testing purposes.
The issue you are having is within your conditionals. ($user == 'NULL') and ($user != 'null'). What you are checking for currently if the User object is the follow string: "NULL".
These are not how you check for null. There are many options that will work.
if (empty($user)){
return view('alumn');
}
// OR
if (!$user) {
return view('alumn');
}
// OR
if (is_null($user)) {
return view('alumn');
}
Would work. You could also use is_null. If you wanted to check that user equals null you cannot put quotation marks around null.
The first() method will return null if there's no data so use is_null() instead like :
if ( is_null($user) ) {
return view('alumn');
}else{
return view('professor');
}
FYI, first() will return you null when there is no data in the database, so I hope this will help you
public function index()
{
$user = User::where('email', '=', Input::get('email'))->first();
if ( is_null($user) ) {
return view('alumn');
} else {
return view('professor');
}
}
All the answers above didn't work in my case.
So i did this to make it worth.
public function index()
{
$user = Auth::user();
if ($user->email == ''){
return ('alumne');
}
else{
return ('professor');
}
print_r(Auth::user());
}
First i printed my Auth::user to check if all was working right, then i tried to save the authentification in a variable called user.
Then i checked with a conditional and all worked fine.
public function index() {
$user = User::where('email', '=', Input::get('email'))->first();
if (empty($user)) {
return view('alumn');
} else {
return view('professor');
}
}
Related
Users, roles, permissions have in my project.I have use custom login. I call Auth::user() return null.
How can I fix that?
public function check(LoginRequest $request)
{
$userInfo = User::where('email', '=', $request->email)->first();
if(!$userInfo){
return back()->with('message', 'We do not recognize your email address');
}
if($userInfo){
if(Hash::check($request->password, $userInfo->password)){
$request->session()->put('isUser', $userInfo->id);
$user = User::where('id', '=', session('isUser'))->first();
dd(Auth::user());
// dd($userInfo->roles);
// return redirect('home');
} else {
return back()->with('message', 'Incorrect password');
}
}
}
You need to pass the user instance to Auth::login() like this: Auth::user($user);;
Im got error in section elseif, i want make HOD looking only at its own department data
public function index(User $user)
{
if(auth()->user()->role == 'Admin')
{
$form = Form::all();
}
elseif(auth()->user()->positions == 'HOD')
{
$form = Form::all()->department('user_id', \Auth::user()->id)->get();
}
else
{
$form = Form::where('user_id', \Auth::user()->id)->get();
}
return view('form.index', ['list_form' => $form]);
}
what should i change in elseif code ?
Try to do a dd() on auth()->user()->positions if it returns nothing, the relation between User model and Positions doesnt exist, or is set up wrong.
I have a simple If Statement in my controller that is not triggering properly.
I have the following route:
Route::resource ('/my-health-hub', 'MyHealthHubController');
I have the following in my controller:
public function index(Request $request)
{
$test = Auth::check();
if ($test = false){
return redirect('/login');
} else {
$providers = $request->user()->providers()->get();
return view ('my-health-hub', compact('providers'));
}
}
When a user is logged in, the second part of the If Statement is triggered properly. However, when logged out, the If Statement still tries to trigger the second part of the If Statement. I did dd($test) to verify the value was false when logged out and it was. So, the second part of the statement should not be triggering when user is logged out.
When you try to make a logic comparison you use two assignment operator for compare
in your case you use one assignment operator = so it will return true in first case all the time
so all you need to fix it to add == instead of =
public function index(Request $request)
{
$test = Auth::check();
if ($test == false){
return redirect('/login');
} else {
$providers = $request->user()->providers()->get();
return view ('my-health-hub', compact('providers'));
}
}
We can remove else block completely. It can be written as below (some code optimizations).
public function index(Request $request)
{
$test = Auth::check();
if ($test){
$providers = $request->user()->providers()->get();
return view ('my-health-hub', compact('providers'));
}
return redirect('/login');
}
This will be obvious to someone else
I have a route that works and goes to the correct controller
Route::get('v1/holidays/{country}/{year}/{month}/{official?}',
'retrieveHolidayController#test'
so if i go to
http://example.com/v1/holidays/US/2014/03/01
it will go where I want to go
however I want the link to look like
http://example.com/v1/holidays?country=US&year=2014&month=03&official=01
How can I do this please ?
You redefine your route to
Route::get('v1/holidays', 'retrieveHolidayController#test');
Then in your controller you can get the param values with $request
public function test(Request $request)
{
if ( $request->has('country') && $request->country != '') {
$country = $request->country;
}
if ( $request->has('year') && $request->year != '') {
$year = $request->year;
}
.... // and the others. Then you can query like this
$holidays = Holiday::when($country, function($query) use ($country) {
return $query->where('country', $country);
})
->when($year, function($query) use ($year) {
return $query->where('year', $year);
})
->get();
//Using 'when' only executes the closure if the variable exists
}
Now, you can use your URL just the way you wanted: http://example.com/v1/holidays?country=US&year=2014&month=03&official=01
Make country,year and monthalso optional:-
Route::get('v1/holidays/{country?}/{year?}/{month?}/{official?}', 'retrieveHolidayController#test')
Route::get('v1/holidays', 'retrieveHolidayController#test');
Route::get('v1/holidays/{country}/{year}/{month}/{official?}', function($country){
return redirect()->to(action('retrieveHolidayController#test', ["country"=>$country,......]));
});
access to http://example.com/v1/holidays/US/2014/03/01
redirect to http://example.com/v1/holidays?country=US&year=2014&month=03&offical=01
if official param is null redirect param offical= param is nullable
not feel good
so
isset($official){
$paramArry["official"] = $official;
}
Not sure what I am doing wrong here but I am having trouble getting the $id of the post to pass to the form request when checking to see if the person editing owns the post. "Job" would be a job posting. Below is the logic in the JobsRequest.
public function authorize()
{
$job=Job::find($this->id);
if($job->user_id == Auth::user()->id){
return true;
}else{
return false;
}
The above keeps returning as false. The update method in the controller is below
public function update(JobsRequest $request, $id)
{
$job=Job::find($id);
$job_data=$request->all();
$job->update($job_data);
return redirect('/jobs/'.$job->id.'/edit');
}
To grab the id within the FormRequest object, you'd need to use the following...
$id = $this->route('id');
Go to the AuthServiceProvider.php and write
$gate->define('show-product',function($user,$product){
return $user->id==$product->customer_id;
});
Then write your controller
$product= Product::find($id);
Auth::loginUsingId(3);
//Auth::logout();
if(Gate::denies('update',$product)){
App::abort('404','Product Not Found');
}
// $this->authorize('update',$product);
return $product->name;
I think It's working perfectly
Try this, which gets the id from the route parameter.
public function authorize(){
$job_id = $this->route('id');
$job=Job::find($job_id);
$user = $this->user();
if($job->user_id == $user->id) return true;
return false;
}
Thanks for pointing me in the right direction. I got this straight from the docs.. This worked for me.
$jobId = $this->route('jobs');
return Job::where('id', $jobId)
->where('user_id', Auth::id())->exists();