mercredi 2 décembre 2015

Cartalyst Sentinel Laravel - can't get id from logged usert

I am trying to call Sentinel:: from within an Eloquent model. However, I am getting the error

Non-static method Cartalyst\Sentinel\Sentinel::getUser() should not be called statically, assuming $this from incompatible context

Here is code from model User where i extend EloquentUser;

<?php namespace App;
use Illuminate\Database\Eloquent\Model;


use Illuminate\Database\Eloquent\SoftDeletes;
use Cartalyst\Sentinel\Users\EloquentUser;


class User extends EloquentUser {


    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

    /**
    * To allow soft deletes
    */
    use SoftDeletes;

    protected $dates = ['deleted_at'];

}

And this is how looks my method from Controller where i try to call Sentinel::getUser()->id;

 public function postCreate(StranicaRequest $request)
    {
        $stranica = new stranica($request->except('image'));
        $picture = "";
        if ($request->hasFile('image')) {
            $file = $request->file('image');
            $filename = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension()?: 'png';
            $folderName      = '/uploads/stranica/';
            $picture = str_random(10).'.'.$extension;
        }
        $stranica->user_id = Sentinel::getUser()->id;
        $stranica->image = $picture;
        $stranica->save();

        if ($request->hasFile('image')) {
            $destinationPath = public_path() . $folderName;
            $request->file('image')->move($destinationPath, $picture);
        }
        return redirect('admin/stranica');
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire