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');
}
Related
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.
In a search controller of Laravel, I am trying to split a string '$travelIdea->tags' into an array '$listtags', and then perform searching to each $listtags[$i], but failed to do so (in "Else" part of if($scope == 'Destination')).
I know that what I should do is similar to a for-loop.
Could anyone give me some suggestion on this?
public function search(Request $request)
{
$search = $request->get('search');
$partialsearch = $request->get('partialsearch');
$scope = $request->get('scope');
if($scope == 'Destination'){
if($partialsearch)
$travelIdea=TravelIdea::where('destination','LIKE','%'.$search."%")->get();
else $travelIdea=TravelIdea::where('destination','%'.$search."%")->get();
}
else {
$listtags = explode(',', $travelIdea->tags);
$foreach($i as $i){
if($partialsearch)
$travelIdea=TravelIdea::where('listtags[$i]','LIKE','%'.$search."%")->get();
else $travelIdea=TravelIdea::where('listtags[$i]','%'.$search."%")->get();
return view('travelIdea.search', compact('travelIdea'));
});
}
}
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');
}
}
I'm working on a project wich needs an online status on the user so i just added an on auth.logout event to listen for logouts and put the status to false for offline. In the event when logged the $event->user value is null. So I tried checking the function that calls Auth::logout() in my project and for some reason the code is being triggered before all the other code. Any one has any idea ?
Here it still has the User Model data.
protected function logout()
{
$user = Auth::user();
$user->online = 0;
$user->save()
//Auth::logout();
return redirect()->intended(route('page.login'));
}
But here the $user = null
protected function logout()
{
$user = Auth::user(); //$user = null
$user->online = 0;
$user->save();
Auth::logout(); //Is this taking priority over all code somehow ?
return redirect()->intended(route('page.login'));
}
There is absolutely no way that code is jumping precedence. If that was possible, software development would be an impossible feat of coincidence and magic -- more so than it already is.
It's much more likely that that code is executing twice. Try setting up the controller logic so that it can function if $user is null. I would bet money that logout() is running multiple times.
protected function logout()
{
$user = Auth::user();
if ($user)
{
$user->online = 0;
$user->save();
Auth::logout();
}
return redirect()->intended(route('page.login'));
}
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();