Using Zizaco Entrust error in hasRole() - Laravel 4.1 - laravel-4

I'm working in a sample project using Zizaco Entrust.
All insalled via composer and following the instructions here.
The issue is I obtain the following error in my route.php:
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Call to undefined method Illuminate\Auth\GenericUser::hasRole()
I'm using Easy PHP with PHP 5.4.14 version and my User model is:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use Zizaco\Entrust\HasRole;
class User extends Eloquent implements UserInterface, RemindableInterface {
use HasRole;
/**
* 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');
/**
* Get the unique identifier for the user.
*
* #return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* #return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* #return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
Thanks for help me.

You should use same table and class name. As follows.
class User extends....
protected $table = 'user';
...

Related

Cannot override laravel default auth username method

Laravel Framework 5.6.38
I am trying to change the default username auth check from 'email' to 'id',
So i put the username() method inside User model file to override the default username method
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password','id'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function username()
{
return 'id';
}
}
It's not working with me . unless i change it from laravel vendor file
Illuminate\Foundation\Auth\AuthenticatesUsers.php
/**
* Get the login username to be used by the controller.
*
* #return string
*/
public function username()
{
return 'kocid';
}
The login now working but when i run composer update , the file return to it default, so i need a solution to successfully override this method using User model.
According to the documentation, https://laravel.com/docs/5.6/authentication#authentication-quickstart...you should do that in your LoginController
public function username()
{
return 'id';
}
Add the username method to your LoginController to override the method from the AuthenticatesUsers trait (Docs - Username Customization).
public function username()
{
return 'id';
}

Calling laravel artisan command from console

I am trying to teach myself Laravel command so that later I use it for scheduling them. This is my kernel file:
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
//
'App\Console\Commands\FooCommand',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('App\Console\Commands\FooCommand')->hourly();
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
And this is the command file inside \App\Console\Commands
namespace App\Console\Commands;
use Illuminate\Console\Command;
class FooCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'command:name';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
//
}
public function fire()
{
$this->info('Test has fired.');
}
}
I want to test the FooCommand command. How do it call this command from shell so that the result is "Test has fired."?
to run your commands manually: php artisan command:name.
Remove your fire function, you can handle this inside handle function.
Fix your schedule function at Kernel class
class Kernel extends ConsoleKernel
{
....
protected function schedule(Schedule $schedule)
{
$schedule->command('command:name')
->hourly();
}
}
To config your schedule please read this:
https://laravel.com/docs/5.4/scheduling

How to get all property for a custom entity in FOSUserBundle?

I am using FOSUserBundle
I created a User entity that extents baseuser, and I added a protected property I called $apiKey. The registration form works fine.
then, I created a userController that extends controller, and in a methoid to edit my user I have:
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('AppBundle:User')->find($id);
my $user has the apiKey property but this is empty (of course the field in the DB is not).
any idea?
thanks
UPDATE: user entity
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Group")
* #ORM\JoinTable(name="fos_user_user_group",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups;
/**
* #ORM\Column(type="string", unique=true, nullable=true)
*/
protected $apiKey;
/* public function __construct()
{
parent::__construct();
// your own logic
//$this->roles = array('ROLE_USER'); //default role for new users
}*/
public function __construct()
{
parent::__construct();
// your own logic
$this->groups = new ArrayCollection();
}
/**
* #return mixed
*/
public function getApiKey()
{
return $this->apiKey;
}
/**
* #param mixed $apiKey
*/
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
}
/**
* #return mixed
*/
public function getGroups()
{
return $this->groups;
}
/**
* #param mixed $groups
*/
public function setGroups($groups)
{
$this->groups = $groups;
}
}

many-to-many relationschop in laravel eloquent does not work with pluck()

I have a many to many relationshhip between 2 tables in laravel.
I just want to get the name of the afdeling with user_id=45.
I tried
$afdelingen = User::find(45)->afdelingen->pluck('name');
but is does not work. It works without the pluck but then i get a long string:
[{"id":3,"afdelingen":"Personeelszaken","user_id":0,"pivot":{"user_id":45,"afdeling_id":3}}]
How can i just get
Model 1 code:
<?php
class Afdeling extends Eloquent {
protected $guarded = array();
public static $rules = array();
protected $connection = 'mysql2';
protected $table = 'afdelingen';
public function users(){
return $this->belongstoMany('User','afdeling_user','afdeling_id');
}
}
Model 2 code:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* #var string
*/
protected $connection = 'mysql2';
protected $table = 'users';
//public function safetyreports(){
// return $this->hasMany('Safetyreport');
//}
public function afdelingen(){
return $this->belongstoMany('Afdeling','afdeling_user','user_id');
}
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* #return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* #return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* #return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
Since it's a many to many relationship, $afdelingen = User::find(45)->afdelingen->pluck('name'); will have a collection of afdelingen, not just one.
You can get the first one by using $afdelingen = User::find(45)->afdelingen()->first()->pluck('name');
Additionally, you can loop to grab all their names.
foreach(User::find(45)->afdelingen as $item) {
$afdelingen = $item->pluck('name');
}
Or if you want an array of the names...
$afdelingen = User::find(45)->afdelingen()->lists('name');

Sonata admin configureFormField

i use sonata admin and i have a 'Fonctionnare' entity. i changed the 'codeFonctionnaire' type of this entity to string but when i create the Fonctionnaire admin class and try to add new fonctionaire i got this error message:
Neither the property "codeFonctionnaire" nor one of the methods "setCodeFonctionnaire()", "_set()" or "_call()" exist and have public access in class "Examens\ExamensBundle\Entity\Fonctionnaire".
Fonctionnaire.php:
<?php
namespace Examens\ExamensBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Fonctionnaire
*/
class Fonctionnaire
{
/**
* #var string
*/
private $codeFonctionnaire;
//////
/**
* Get codeFonctionnaire
*
* #return string
*/
public function getCodeFonctionnaire()
{
return $this->codeFonctionnaire;
}
////////
FonctionnaireAdmin.php:
<?php
namespace Examens\ExamensBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Examens\ExamensBundle\Entity\Fonctionnaire;
class FonctionnaireAdmin extends Admin
{
protected $datagridValues = array(
'_sort_order' => 'ASC',
'_sort_by' => 'codeFonctionnaire'
);
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('codeFonctionnaire','text',array('label'=>'Code fonctionnaire'))
//////
}
what's wrong with the entity?
You need to regenerate the getters and setters of your Fonctionnaire class. Your IDE can do it for you.
Or at least, just add a
public function setCodeFonctionnaire($codeFonctionnaire) {
$this->codeFonctionnaire = $codeFonctionnaire;
}
EDIT
Here is what might be your complete Fonctionnaire class :
namespace TechVehi\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Fonctionnaire {
/**
* #var string
*
* #ORM\Column(name="codeFonctionnaire", type="string", length=255, nullable=true)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $codeFonctionnaire;
/**
* #param string $codeFonctionnaire
*/
public function setCodeFonctionnaire($codeFonctionnaire) {
$this->codeFonctionnaire = $codeFonctionnaire;
}
/**
* #return string
*/
public function getCodeFonctionnaire() {
return $this->codeFonctionnaire;
}
}
You might have forgotten other informations, like the ORM annotation to link your object to your database.
i deleted this code from fonctionnaire.orm.yml:
generator:
strategy: IDENTITY
and it works.

Resources