Back

How to solve the growing bloated ORM?


At first, my model code may be clear, but later, more and more model associations, more and more events will lead to my model is bloated.

<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Webpatser\Uuid\Uuid;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $table = 'customs';

    protected $fillable = [
        'avatar',
        'surname',
        'name',
        'score',
        'country',
        'city',
        'gender',
        'area',
        'address',
        'zip_code',
        'phone',
        'email',
        'id_number',
        'driver_number',
        'passport_number',
        'debit_card_number',
        'credit_card',
        'credit_card_timeout',
        'birthday',
        'like',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function hotels()
    {
        return $this->belongsToMany(Hotel::class, 'hotel_custom', 'custom_id', 'hotel_id')->where('status', 1);
    }

    public function orders()
    {
        return $this->hasMany(Order::class, 'custom_id');
    }


    public static function boot()
    {
        parent::boot();
        self::creating(function ($model) {
            $model->uuid = (string) Uuid::generate(4);
        });
    }
}

If I still need my model to do more ,For example, an administrator with an ID of 1 and a user with a status of 0 are disabled

<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Webpatser\Uuid\Uuid;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $table = 'customs';

    protected $fillable = [
        'avatar',
        'surname',
        'name',
        'score',
        'country',
        'city',
        'gender',
        'area',
        'address',
        'zip_code',
        'phone',
        'email',
        'id_number',
        'driver_number',
        'passport_number',
        'debit_card_number',
        'credit_card',
        'credit_card_timeout',
        'birthday',
        'like',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function hotels()
    {
        return $this->belongsToMany(Hotel::class, 'hotel_custom', 'custom_id', 'hotel_id')->where('status', 1);
    }

    public function orders()
    {
        return $this->hasMany(Order::class, 'custom_id');
    }

  
    public static function boot()
    {
        parent::boot();
        self::creating(function ($model) {
            $model->uuid = (string) Uuid::generate(4);
        });
    }
   
     // more
    public function isAdmin()
    {
        return $this->getKey() === 1;
    }
    
    public function isActive()
    {
        return $this->getAttribute('status') === 1;
    }
}

My model is growing, and I read a web article about trying to use a warehouse Repository,But I don't like it very much because I think ORM is enough to let me know what to do. This is my idea.

class User
{
	// This model focuses on writing about ORM, such as model association
}

class UserManager extends User
{
           // Here's the external correlation.
	   public function isAdmin()
    	  {
                return $this->getKey() === 1;
    	  }
}

So , consult you , how do you solve when your model is overstaffed ?


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us