<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kristina</title>
    <description>The latest articles on DEV Community by Kristina (@kod25).</description>
    <link>https://dev.to/kod25</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1371959%2F3b4c342f-4f25-4588-b362-9413d9152b60.png</url>
      <title>DEV Community: Kristina</title>
      <link>https://dev.to/kod25</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kod25"/>
    <language>en</language>
    <item>
      <title>How to Customize User Model in Laravel</title>
      <dc:creator>Kristina</dc:creator>
      <pubDate>Mon, 25 Mar 2024 07:00:00 +0000</pubDate>
      <link>https://dev.to/kod25/how-to-customize-user-model-in-laravel-3b79</link>
      <guid>https://dev.to/kod25/how-to-customize-user-model-in-laravel-3b79</guid>
      <description>&lt;p&gt;Packages are the cornerstone of PHP and Laravel development. Without them, we would have to reinvent (or copy&amp;amp;paste) the wheel for every single project. However, a package may not fully meet all your needs, so it's important that they are well-built and easily customizable.&lt;/p&gt;

&lt;p&gt;One of the very well-known and one of our favorite packages is the &lt;a href="https://spatie.be/docs/laravel-medialibrary/introduction"&gt;Laravel-medialibrary from Spatie&lt;/a&gt;. The main model the package works with is called &lt;code&gt;Media&lt;/code&gt;. 90% of the time, it fulfills all of our project’s needs, but for the remaining 10% we need to extend or tweak its functionality and that’s where the “swappability” feature comes into play.&lt;/p&gt;

&lt;p&gt;If you are interested in how it’s implemented, feel free to head to its source code and browse. We will mention a simplified version of how we did it for our package further in this article.&lt;/p&gt;




&lt;p&gt;Craftable PRO is an advanced Laravel admin panel and CRUD generator that goes beyond simple roles and permissions management. With Craftable PRO, you can effortlessly generate a fully functional admin panel based on your database structure. Additionally, it streamlines media management, facilitates translation handling, and offers various other powerful features. Simplify the development process of your entire Laravel backend with Craftable PRO.&lt;/p&gt;




&lt;p&gt;One of the most important out-of-the-box models in Craftable PRO is definitely the &lt;code&gt;CraftableProUser&lt;/code&gt;, which is the default authenticatable model. In the beginnings of the package history, however, it used to reside inside the vendor package and be hard-coded in many places of the codebase - and attempting to extend its functionality or swap it for a different model would have proved to be a great challenge.&lt;/p&gt;

&lt;p&gt;We have implemented this feature recently and it turned out to be easier than expected.&lt;/p&gt;

&lt;p&gt;The gist of it is as follows:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Add an attribute to config for the model’s class name:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'craftable_pro_user_model' =&amp;gt; Brackets\CraftablePro\Models\CraftableProUser::class
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Create a base model which contains only the necessary code without which the package would not function:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace Brackets\CraftablePro\Models;

use Brackets\CraftablePro\Helpers\Initials;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class BaseCraftableProUser extends Authenticatable
{
    use HasRoles;

    protected $guard = 'craftable-pro';

    protected $fillable = [
        'first_name',
        'last_name',
        'email',
    ];

    protected function initials(): Attribute
    {
        return Attribute::make(
            get: fn ($value) =&amp;gt; Initials::new()-&amp;gt;generate($this-&amp;gt;first_name . ' ' . $this-&amp;gt;last_name)
        );
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Replace all hard-coded mentions of the model with the config value. Instead of:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$user = CraftableProUser::updateOrCreate([
  'email' =&amp;gt; $email,
], [
  'first_name' =&amp;gt; 'Administrator',
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$user = config('craftable-pro.craftable_pro_user_model')::updateOrCreate([
  'email' =&amp;gt; $email,
], [
  'first_name' =&amp;gt; 'Administrator',
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let’s go and see how this new feature can be configured and used within Craftable Pro. For a quicker overview, the official documentation is located here: &lt;a href="https://docs.craftable.pro/basic-features/users#custom-user-model"&gt;Users – Craftable PRO Documentation&lt;br&gt;
&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Task:&lt;/strong&gt; add a &lt;code&gt;full_name&lt;/code&gt; attribute and a &lt;code&gt;teams&lt;/code&gt; relationship to the authenticatable user model without having to rewrite any package code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1. Create a new Eloquent model extending the base class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace App\Models;

use Brackets\CraftablePro\Models\BaseCraftableProUser;

class AdminUser extends BaseCraftableProUser
{
    protected $table = 'craftable_pro_users';
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It can also be called the same as the original &lt;code&gt;CraftableProUser&lt;/code&gt; model, since it is in a different namespace. Despite that, we will call it &lt;code&gt;AdminUser&lt;/code&gt; for clarity.&lt;/p&gt;

&lt;p&gt;If you want to keep using the underlying &lt;code&gt;craftable_pro_users&lt;/code&gt; database table, you have to specify it because it does not get inherited from the base model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2. Register the new model in the config/craftable-pro.php file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you don’t see the &lt;code&gt;config/craftable-pro.php&lt;/code&gt; file, it first has to be published through the &lt;code&gt;php artisan vendor:publish --tag=craftable-pro-config&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return [
    /*
     * The fully qualified class name of the Craftable Pro user model.
     */
    'craftable_pro_user_model' =&amp;gt; App\Models\AdminUser::class,
    ...
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3. Configure the guards and auth providers in &lt;code&gt;config/auth.php&lt;/code&gt; file:&lt;/p&gt;

&lt;p&gt;Unfortunately (but understandably) there is no way to use a config value from one config file within another config file, so we have to specify the new model here as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use App\Models\AdminUser;

return [
  'providers' =&amp;gt; [
        'craftable-pro-users' =&amp;gt; [
            'driver' =&amp;gt; 'eloquent',
            'model' =&amp;gt; MyCraftableProUser::class,
        ],
  ],
  'guards' =&amp;gt; [
        'craftable-pro' =&amp;gt; [
            'driver' =&amp;gt; 'session',
            'provider' =&amp;gt; 'craftable-pro-users',
        ],
  ],
  ...
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4. Customize the new AdminUser model however you like&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add attributes, relationships, appends, eager loading… - whatever you want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace App\Models;

use Brackets\CraftablePro\Models\BaseCraftableProUser;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class AdminUser extends BaseCraftableProUser
{
    protected $table = 'craftable_pro_users';

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this-&amp;gt;appends = [...parent::getAppends(), 'full_name'];
        $this-&amp;gt;with = ['teams'];
    }

    public function getFullNameAttribute(): string
    {
        return $this-&amp;gt;first_name . ' ' . $this-&amp;gt;last_name;
    }

    public function teams(): BelongsToMany
    {
        return $this-&amp;gt;belongsToMany(Team::class);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5a. Update data in the database where necessary, mainly in polymorphic relationships:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmg5x91u305in29h4j0xj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmg5x91u305in29h4j0xj.png" alt="Database OLD" width="800" height="97"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;to&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2ltwg179t2bb3hyps82.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2ltwg179t2bb3hyps82.png" alt="Database NEW" width="800" height="84"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5b. If you are already using Laravel’s morph map, update the model class name instead:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Relation::morphMap([
  'CraftableProUser' =&amp;gt; CraftableProUser::class
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Relation::morphMap([
  'CraftableProUser' =&amp;gt; App\Models\AdminUser::class
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Customizing the user listing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To customize the user listing (Access tab), it is necessary to first copy the vue code from v&lt;code&gt;endor/brackets/craftable-pro/resources/js/Pages/CraftableProUser/Index.vue&lt;/code&gt; and replace with it the code in &lt;code&gt;resources/js/craftable-pro/Pages/CraftableProUser/Index.vue&lt;/code&gt; as the originally published &lt;code&gt;resources/js/craftable-pro/Pages/CraftableProUser/Index.vue&lt;/code&gt;file is only a stub that links to the vendor file.&lt;/p&gt;

&lt;p&gt;Let’s replace the name with the new &lt;code&gt;full_name&lt;/code&gt; attribute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ListingDataCell&amp;gt;
    &amp;lt;div class="flex items-center"&amp;gt;
        &amp;lt;Avatar
            :src="item.avatar_url"
            :name="`${item.first_name} ${item.last_name}`"
        /&amp;gt;
        &amp;lt;div class="ml-4"&amp;gt;
            &amp;lt;div class="font-medium text-gray-900"&amp;gt;
                &amp;lt;!-- TODO: maybe have full_name attribute? --&amp;gt;
                {{ item.first_name }} {{ item.last_name }}
            &amp;lt;/div&amp;gt;
            &amp;lt;div class="text-gray-500"&amp;gt;{{ item.email }}&amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/ListingDataCell&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code can now be refactored as such:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ListingDataCell&amp;gt;
    &amp;lt;div class="flex items-center"&amp;gt;
        &amp;lt;Avatar
            :src="item.avatar_url"
            :name="item.full_name"
        /&amp;gt;
        &amp;lt;div class="ml-4"&amp;gt;
            &amp;lt;div class="font-medium text-gray-900"&amp;gt;
                {{ item.full_name }}
            &amp;lt;/div&amp;gt;
            &amp;lt;div class="text-gray-500"&amp;gt;{{ item.email }}&amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/ListingDataCell&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don’t forget to run the &lt;code&gt;npm run craftable-pro:build&lt;/code&gt; command and you are done!&lt;/p&gt;

&lt;p&gt;In summary, packages are vital components in PHP and Laravel development, streamlining processes and saving valuable time by providing pre-built functionalities.&lt;/p&gt;

&lt;p&gt;The recent integration of this feature into Craftable PRO, particularly with the &lt;code&gt;CraftableProUser&lt;/code&gt; model, highlights the importance of well-designed and adaptable packages. What was once a complex task has now become manageable, thanks to thoughtful design and implementation.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>craftablepro</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
