mardi 1 juin 2021

Laravel 5 Property [userType] does not exist on this collection instance

I keep getting property [userType] does not exist error when Im trying to make log in condition here is my Controller code below. I also tried to make Auth controller but I just keep getting the same error from this.

<?php

namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\User;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/dashboard';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    protected function redirectTo()
{
    $user = User:all();
    if($user->userType == 'admin')
    {
        return 'dashboard';
    }
    else
    {
        return 'verify first';
    }
}
}

and here is my model code

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *  protected $table = 'users';
    *public $primaryKey ='id';
     * @var array
     */
  
    protected $fillable = [
        'name', 'email', 'password','userType',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function posts(){
        return $this->hasMany('App\Post');
    }

   
}

anyway on how can I fix this? Please show me the correct way to create a redirect in login because I just want to create a login with admin, user, and guest mode.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire