Home > database >  In which model to put a function that returns all active / inactive venues of the user?
In which model to put a function that returns all active / inactive venues of the user?

Time:04-23

User Model:

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

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

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function venues()
    {
        return $this->hasMany(Venue::class);
    }

    public function profile()
    {
        return $this->hasOne(Profile::class);
    }
}

Venue Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Venue extends Model
{
    use HasFactory;

    protected $fillable = ['user_id', 'city_id', 'category_id', 'title', 'address', 'phone', 'email', 'website', 'facebook', 'instagram', 'content_bg', 'content_en', 'cover_image', 'lat', 'lng'];

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }

    public function city()
    {
        return $this->belongsTo(City::class, 'city_id');
    }

    public function features()
    {
        return $this->belongsToMany(Feature::class, 'venue_feature');
    }

    public function images()
    {
        return $this->hasMany(VenueImage::class);
    }

    public function reviews()
    {
        return $this->hasMany(Review::class);
    }
}

Everything is fine, but now I want to have two methods where to call active / inactive venues of the user and I'm not sure where to place them in User Model or in Venue Model, generally which is better?

If I put them in Venue model (getUserActiveVenues and getUserInactiveVenues) and pass authenticated user to these methods, or to put them in User model (getActiveVenues and getInactiveVenues).

CodePudding user response:

add relations to the user model

    public function venues()
    {
        return $this->hasMany(Venue::class);
    }

    public function activeVenues()
    {
        return $this->hasMany(Venue::class)->where('active',true);
    }

    public function inActiveVenues()
    {
        return $this->hasMany(Venue::class)->where('active',false);
    }

then you can eager load the relevant type of venue. I had to guess at what you mean be 'active'

  • Related