2 votes

Property does not exist on the Eloquent builder instance

I am practicing with laravel but I have some doubts, I want to apply a middleware that prohibits the access to a route if a logged user has "0" in the column "wizard" of my table "companies" and redirects him to complete his process but if he has "1" in the field let him go to the route /dashboard.

declare my model as follows:

    <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Empresa extends Model
    {
        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'empresas';

        /**
        * The database primary key value.
        *
        * @var string
        */
        protected $primaryKey = 'id';

        /**
         * Attributes that should be mass-assignable.
         *
         * @var array
         */
        protected $fillable = ['nombreempresa',
        'nombrecomercial',
        'idfiscal',
        'logo',
        'adminid',
        'tel',
        'pais',
        'status',
        'email',
        'acceso',
        'verified',
        'categoria',
        'sector',
        'tipo',
        'serial',
        'wizard',
        'created_at',
        'updated_at'];
        public function user()
          {
            return $this->belongsTo('App\User');
          }
    }

The middleware code is:

namespace App\Http\Middleware;
use Illuminate\Support\Facades\Auth;
use Closure;
use App\Empresa;

class WizardVerification
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
       {
         if(Empresa::where('adminid', Auth::user()->id)->wizard && !Auth::check()){
            return redirect('/dashboard');
          }
         return $next($request);
       }
}

in my routes I called it that way:

Route::group(['middleware' => ['wizard']], function () {

/*Todas las rutas que esten en este grupo estaran restringidas si el usuario no completo su informacion y se restringira su paso si la no se encuentra logueado.*/
Auth::routes();
Route::get('/dashboard', 'DashboardController@index', function () {
    return redirect('/home');
});
Route::get('/home', 'HomeController@index')->name('home');
});

but when I do the test I get the error "propety [wizard] does not exist" what am I doing wrong? because I followed carefully this tutorial but I can't get it to work, I hope someone can explain me why my code is wrong, greetings!

1 votes

In addition to what @Shassain tells you, you've got the conditional wrong. You are saying si tiene wizard y no está logueado . If you are not logged in, Auth::user()->id will throw an error, so you should check that first. And if you have wizard you can't be not logged in The conditional will never be fulfilled. In any case, I understand what you mean si no está logueado O no tiene wizard . On the other hand, you are doing a redirect if the condition is met, so ALL the URLs will end in /dashboard . It seems to me that you have everything mixed up.

3voto

Shassain Points 2710

When you do:

Empresa::where('adminid', Auth::user()->id)

You obtain an object Builder which does not yet belong to an Enterprise model, so you cannot access the wizard property.

But if you do:

Empresa::where('adminid', Auth::user()->id)->get();

In this case you already get a Company collection (i.e. an array of company objects). Even so you would have to indicate the position of the object to which you want to access, this would be like this: Empresa::where('adminid', Auth::user()->id)->get()[0]->wizard (would be a solution).

But I would take another option:

public function handle($request, Closure $next)
{
  $data=Empresa::where('adminid', Auth::user()->id)->first();// obtengo solo un registro el cual es un objeto, entonces ahora puedo hacer
  if($data->wizard && !Auth::check()){
     return redirect('/dashboard');
  }
  return $next($request);
}

0 votes

hice la modificacion pero me arroja el error "Trying to get property 'id' of non-object"

0 votes

@Abdiel Hernadez that happens because your user is not logged in.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X