Extended User Model in a Package - laravel

Could you help me understand the right way to extend existing models? I'm developing a package and want to do as much as possible separated from the main application.
I want to have all existing functionality of the User model, but only add a relation to another model. In my package User I can have several (hasMany) Article. Instead of adding a method to \App\User I created a new model in my package class User extends \App\User:
namespace Package\Sample;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
/**
* Class User
* #package Package\Sample
*/
class User extends \App\User
{
use HasApiTokens, Notifiable;
public function articles()
{
return $this->hasMany(Article::class);
}
}
To make it work I add the configuration for my package auth.php:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => \Package\Sample\User::class,
],
],
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
My question: Is it a "best practice" to extend functionality of existing models?

I solved it by using the class_alias() php function.
In my package service provider I setup an alias of the model class defined for Authentication in /config/auth.php like this:
public function boot(){
class_alias(config("auth.providers.users.model"), 'ParentModel');
}
then I use ParentModel class where needed:
use ParentModel;
class Agent extends ParentModel {
...
}
Hope It makes sense for someone

that will prevent edits on the \Package\Sample\User::class. any new method needed will result in a package update.
why not declare a trait in your package containing your methods and use them in the App\User::class like what laravel is using.
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;

I tried using the class_alias and although it works for basic usage, when things got more complicated my user class couldn't cut it. For example, my notifications where using the local package user type and not showing in app.
After doing more research I found using a trait IS the proper way like others have mentioned.
I found a comprehensive guide here: https://laravelpackage.com/08-models-and-migrations.html#approach-2-using-a-polymorphic-relationship
the gist:
Create a trait in your package:
Important here you can setup whatever the relationship you need depending on you db modeling.
// 'src/Traits/HasPosts.php'
<?php
namespace JohnDoe\BlogPackage\Traits;
use JohnDoe\BlogPackage\Models\Post;
trait HasPosts
{
public function posts()
{
return $this->morphMany(Post::class, 'author');
}
}
Add the use in whatever user classes it applies to in your app
// 'App\Models\User.php'
<?php
namespace App\Models;
use JohnDoe\BlogPackage\Traits\HasPosts;
class User extends Authenticatable
{
use HasPosts;
...
Lastly, you'll need to add an extra field in the db for the user_type where you are using the user_id.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddUserTypeToPostTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->tinyText('user_type')
->comment('User class type')
->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('posts', function (Blueprint $table) {
$table->dropColumn('user_type');
});
}
}

You need to Add config(['auth.providers.users.model' => Myname\Myproject\App\Models\User::class]); to the boot-method inside my package-service-provider.
And Create new Class in your package.
namespace Myname\Myproject\App\Models;
class User extends \App\User
{
public function roles(){
return $this->belongsToMany('Myname\Myproject\App\Models\Role', 'user_role', 'user_id', 'role_id');
}
}

Related

Using Sanctum with Laravel Spark conflict

My setup
Laravel 8
Laravel Spark Mollie
I'm constantly hitting a brick wall when calling API requests with Spark & Sanctum. I've installed Sanctum with no problem and migrated.
I've added use Laravel\Sanctum\HasApiTokens; to app/Models/User.php and added use HasApiTokens; to the class.
My Api.php route
Route::group([
'middleware' => 'auth:sanctum'
], function () {
Route::get('categories', [\App\Http\Controllers\categories::class, 'fetchCategories']);
});
When I call the Api I get this error
ErrorException
Declaration of Laravel\Sanctum\HasApiTokens::tokenCan(string $ability) should be compatible with Laravel\Spark\User::tokenCan($ability)
I've tried changing use Laravel\Sanctum\HasApiTokens; to Laravel\Spark\HasApiTokens on User.php. The error goes away, but whenever I try calling the Api, it returns me back to the login homepage.
Any ideas? As the Spark documentation doesn't really explain how Sanctum or Api protection work.
The problem is that your main User class extends the User class from the vendor Spark library. This User model uses the trait named HasApiTokens which is not the same as Sanctum
Since you don't want to change the file from the vendor directory, one fix I found was to copy the original SparkUser model class from the vendor and create a new one like this and remove the trait HasApiTokens since you don't want to use it anymore.
<?php
namespace App\Models\Users;
use Illuminate\Support\Str;
use Laravel\Spark\Billable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class SparkUser extends Authenticatable
{
use Billable, Notifiable; // HasApiTokens was removed from the original SparkUser class
/**
* Get the profile photo URL attribute.
*
* #param string|null $value
* #return string|null
*/
public function getPhotoUrlAttribute($value)
{
return empty($value) ? 'https://www.gravatar.com/avatar/'.md5(Str::lower($this->email)).'.jpg?s=200&d=mm' : url($value);
}
/**
* Make the team user visible for the current user.
*
* #return $this
*/
public function shouldHaveSelfVisibility()
{
return $this->makeVisible([
'uses_two_factor_auth',
'country_code',
'phone',
'card_brand',
'card_last_four',
'card_country',
'billing_address',
'billing_address_line_2',
'billing_city',
'billing_state',
'billing_zip',
'billing_country',
'extra_billing_information'
]);
}
/**
* Convert the model instance to an array.
*
* #return array
*/
public function toArray()
{
$array = parent::toArray();
if (! in_array('tax_rate', $this->hidden)) {
$array['tax_rate'] = $this->taxPercentage();
}
return $array;
}
}
And now all I had to change was my original User class model to use this new model like this and add the trait HasApiTokens from Sanctum!
use App\Models\SparkUser; // Modified from the original in the vendor folder
use Laravel\Sanctum\HasApiTokens;
class User extends SparkUser
{
use HasApiTokens;
...
}

Laravel Spatie Permissions - how to change default model 'role' by another model

please how can i change the default model 'role' by another model in my project (laravel project) has the same architecture as 'role'; the default model package.
in the config/permission.php config file contains:
'models' => [
/*
* When using the "HasPermissions" trait from this package, we need to know which
* Eloquent model should be used to retrieve your permissions. Of course, it
* is often just the "Permission" model but you may use whatever you like.
*
* The model you want to use as a Permission model needs to implement the
* `Spatie\Permission\Contracts\Permission` contract.
*/
'permission' => Spatie\Permission\Models\Permission::class,
/*
* When using the "HasRoles" trait from this package, we need to know which
* Eloquent model should be used to retrieve your roles. Of course, it
* is often just the "Role" model but you may use whatever you like.
*
* The model you want to use as a Role model needs to implement the
* `Spatie\Permission\Contracts\Role` contract.
*/
'role' => Spatie\Permission\Models\Role::class,
],
i want to have something like this:
'role' => App\Fonction::class,
the documentation says that i have to implement the
Spatie\Permission\Contracts\Role` contract.
any idea how can i do this in the right way.
the Fonction Model :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Fonction extends Model
{
protected $fillable = [
'nom','description'
];
public function comptes()
{
return $this->hasMany('App\Compte') ;
}
}
You can extend Spatie\Permission\Models\Role class. It already implements Spatie\Permission\Contracts\Role interface. Check details in doc.
<?php
namespace App;
use Spatie\Permission\Models\Role;
class Fonction extends Role
{
protected $fillable = [
'nom','description'
];
public function comptes()
{
return $this->hasMany('App\Compte') ;
}
}

Use trait in Laravel database migration

My superior told me I could create a seeder-like trait which I can then use inside a migration. When the migration is being run on the server the database automatically gets seeded while migrating instead of running a separate seeder after the migration succeeded.
Now I created a trait which I included in the database migration.
<?php
namespace App\Services;
use App\Models\Module\Module;
use App\Models\Plan\Plan;
/**
* PlanAndModuleGenerator class.
*/
trait PlanAndModuleGenerator
{
private static $plans = [
'free',
'basic',
'premium',
];
public function up()
{
foreach ($this->plans as $planName) {
// Get or create Plan.
$plan = Plan::create([
'machine_name' => '',
'name' => $planName
]);
}
}
}
My superior told me they did this before, but I can't find anything like this on the internet. I included my trait like this.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\Services\PlanAndModuleGenerator;
class ModulePlan extends Migration
{
use PlanAndModuleGenerator;
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('module_plan', function (Blueprint $table) {
$table->unsignedInteger('plan_id');
$table->unsignedInteger('module_id');
$table->foreign('plan_id')->references('id')->on('plans');
$table->foreign('module_id')->references('id')->on('modules');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('module_plan');
}
}
When I run the migration the up function inside my trait isn't executed. I know this because the Plan table isn't seeded yet. Any ideas on how I might tackle this? As my superior isn't in the office the upcoming days and I can't access the repository where they did this before.
Besides this, can anyone tell me how I can properly debug this trait? The way I am doing this now, just running the migration and wait for errors, seems a bit cumbersome.
I don't see any reason for this trait at all, but if you really want to use it you would need to alias the up method of the trait and then call that in your up method of the migration:
class ModulePlan extends Migration
{
use PlanAndModuleGenerator { up as traitUp; }
public function up()
{
...
$this->traitUp();
}
}
It would be better to just use a different name for the method in the Trait, but there is no reason for this trait in the first place it would seem.
It will definitly not work because you have two up methods, one in your trait and the one in your migration. You need to delete the up in your migration and use the one in your trait as shown below. Here is the trait
<?php
namespace App\Services;
use App\Models\Plan\Plan;
use App\Models\Module\Module;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
/**
* PlanAndModuleGenerator class.
*/
trait PlanAndModuleGenerator
{
private static $plans = [
'free',
'basic',
'premium',
];
public function up()
{
$createmigration = Schema::create('module_plan', function (Blueprint $table) {
$table->unsignedInteger('plan_id');
$table->unsignedInteger('module_id');
$table->foreign('plan_id')->references('id')->on('plans');
$table->foreign('module_id')->references('id')->on('modules');
});
if ($createmigration) {
foreach ($this->plans as $planName) {
// Get or create Plan.
$plan = Plan::create([
'machine_name' => '',
'name' => $planName
]);
}
}
}
}
Confirm first that the migration was created before creating your Plan.
Here is how your migration should look like
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\Services\PlanAndModuleGenerator;
class ModulePlan extends Migration
{
use PlanAndModuleGenerator;
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('module_plan');
}
}
Hope this will help

ORM not working in Default UserController & User Model in laravel

I want to get User wise Role. here is I'm facing error ....
UserController.php ( user controller file )
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\UserRequest;
use App\Employee;
use App\Role;
use App\User;
use App\Site;
use App\Client;
use App\ProjectType;
use App\UserPermission;
use Auth;
use DB;
use App\Project;
class UsersController extends BaseController {
public function __construct() {
$this->isSetClientAndProjectType();
$data = User::with('Role')->first();
echo "<pre>";print_r(json_decode($data)); die;
}
}
User.php ( user model file )
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable {
use SoftDeletes;
use Notifiable;
protected $fillable = [
'name', 'role_id', 'password', 'siteid', 'email', 'status', 'allowed_to_bypass_pm', 'allowed_to_bypass_admin'
];
protected $hidden = [
'password', 'remember_token',
];
// Get users roles
public function Role() {
return $this->hasMany('App\Role', 'role_id', 'id');
}
}
Error is
How can i solve this error?
Help me guys.
Thank You.
If a user has many "roles" it should be public function roles().
You have defined:
A single user has a role_id
Therefore you need:
If a user has a single role it would be:
public function role() {
return $this->belongsTo('App\Role');
}
The reverse on the Role model would be:
public function users() {
return $this->belongsToMany('App\User');
}
Since many users can have the same role.
Hope this helps.
You need to add belongsTo relationship
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable {
use SoftDeletes, Notifiable;
protected $fillable = [
'name', 'role_id', 'password', 'siteid', 'email', 'status', 'allowed_to_bypass_pm', 'allowed_to_bypass_admin'
];
protected $hidden = [
'password', 'remember_token',
];
// Get user's role
public function role() {
return $this->belongsTo('App\Role');
}
}
Now fetch data
$user = User::with('role')->find(1);
$role = $user->role;
You need to make sure your table structure and foreign key references are compatible with the model relationship methods used.
For example, you have used "HasMany" relationship on User model. For that, you will have to make sure that each record/row in users table(User model) "has many" associated records/rows in roles table(Role model).
Here HasMany method assumes a foreign key "role_id" on roles table(Role Model). On not finding of which, it throws error.
You first need to take in consideration the table structure of roles and users table(User and Role model) as per your requirement, and than add model relationship methods accordingly.
It can be a bit tricky if you are using the methods for the first time, you can refer the laravel documentation for the same:
eloquent-relationships

Changing the name of the "users" table to account Laravel 5

Fairly new to Laravel and would like to follow the table conventions I'm used to.
The default table name for accounts is "users" and I'd like to change it to "account." If anything, I'd like to change it to "user" and remove the plural.
I've already used migrate to make a cloned table of users called "account" and I'm just trying to figure out what all I have to do to existing code to make it work for logging in
It looks like I'll have to somehow update "app/Http/Auth/AuthController.php", but I'm not quit sure what it is I'll have to do...
Do I need to:
Update "use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;"?
Update in AuthController "returnUser::create" to "returnAccount::create"? If so do I need to go somewhere else in the code where that class User is being created?
I guess another option is just scrapping their AuthController and establishing my own and just calling a new object of Account... Is this the route I should take?
I would simply extend the User class and overrule some things if you want to have the Model named Account:
Edit the table property in the Account class, see: https://github.com/laravel/laravel/blob/master/app/User.php#L24
Account extends User {
protected $table = 'accounts';
}
Once your class Account is created edit the configured authentication class, see:
https://github.com/laravel/laravel/blob/master/config/auth.php#L31
If you only want to overrule the table used by User, edit the User class:
protected $table = 'accounts';
To be honest, why bother? Taylor provided this skeleton for you to kickstart your application, why not use that especially if you're new to Laravel?
Firstly, create account(s) migration - plural is widely acceptable
Migration must contain all important fields
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAccountsTable extends Migration
{
public function up()
{
Schema::create('accounts', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::drop('accounts');
}
}
Then create Account model,
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class Account extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'accounts';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
}
head over to config\auth.php and change this line:
'model' => App\User::class,
to
'model' => App\Account::class,

Resources