How to Get Data from Laravel Controller and Compare It?

Wajid Naeem

In my Controller, I have the following.

public function check_login(Request $request)
{
    $users = User::all('user_type');
    if ($users === 'teacher') {
        echo 'teacher';
    } else if ($users === 'student') {
        echo 'student';
    } else {
        echo 'nothing';
    }
}

Unfortunately, the if and else if are not working.

Piazzi

When you use the method All() you're getting the whole collection. Try loop the $users like this:

$users = User::all();
foreach($users as $user)
{
    if($user->user_type == 'teacher')
    {
        echo 'teacher';
    }
    else if($user->user_type == 'student'){
        echo 'student';
    }

    else{
        echo 'nothing';
    }
}

Here you're comparing each object from the User Collection

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel: How to get data from View/Blade and pass to Controller

How to get calculated data from Controller to View and display it (laravel)?

How to get value of data attribute from button to controller in laravel

How to get n data from table in laravel controller?

Get data from Controller in Laravel 7

Laravel, How to get array of session in controller and compare with model

How to get logged in user data into Laravel controller

How to get ID User from Laravel Controller

Laravel how to pass data from controller to blade

How to pass data from controller to view in Laravel

how to get data into view from controller in cakephp

How to get data from the controller model to the view

How to get the data from ajax in controller codeigniter

how to get data from angularjs controller

How to get json data from controller in jsp?

How to get data back from emberjs controller

Laravel get data from dropdown list and pass it to the controller

Get count of only required column data from a laravel controller output

Laravel: Get id from database in controller and update data

Laravel - Get returned data from an API within the Controller

cant get data from show controller to view laravel 5.6

Unable to get the the data from AJAX request to Laravel controller and work with it?

Can't get data in laravel controller that sent from ajax

How to access a field data from users table from a controller in Laravel?

How to fetch data from table and compare it and display the result in laravel?

How to loop and compare multiple data passed from a single view Laravel

How to get multiple rows data of the form in controller in laravel 5.1

How to I get user role data in controller and displaying laravel

laravel 5.4,How to get logged in user data into controller