ldc-dam/app/Models/Panier.php

104 lines
2.1 KiB
PHP
Raw Normal View History

2019-07-31 12:22:24 +02:00
<?php
namespace App\Models;
2019-07-31 12:22:24 +02:00
2022-04-19 14:49:09 +02:00
use App\Models\User;
2019-07-31 12:22:24 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Panier extends Model
{
use SoftDeletes;
2022-04-01 10:18:04 +02:00
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'mediacount', 'mediacover'
];
2022-04-01 10:18:04 +02:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'description', 'libelle', 'statut'
];
2019-07-31 12:22:24 +02:00
2022-04-01 10:18:04 +02:00
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'statut' => 'boolean'
];
2019-07-31 12:22:24 +02:00
2022-04-01 10:18:04 +02:00
// * RELATIONSHIPS
2019-07-31 12:22:24 +02:00
2022-04-01 10:18:04 +02:00
/**
* BelongsTo user relation
* Get the user that owns the panier.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function owner()
{
return $this->belongsTo(User::class, 'user_id');
}
2022-04-01 10:18:04 +02:00
/**
* BelongsToMany users relation
* The users that belong to the panier.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function usersFavoris()
{
return $this->belongsToMany(User::class);
2022-04-01 10:18:04 +02:00
}
/**
* BelongsToMany mediaList relation
* The mediaList that belong to the panier.
2022-04-01 10:18:04 +02:00
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function mediaList()
2022-04-01 10:18:04 +02:00
{
return $this->belongsToMany(Media::class, 'panier_media');
2022-04-01 10:18:04 +02:00
}
// * GETTERS
/**
* Get mediacount attribute
*
* @return boolean
*/
public function getMediacountAttribute(): int
{
return $this->mediaList()->count();
2022-04-01 10:18:04 +02:00
}
/**
* Get mediacover attribute
*
* @return string
*/
public function getMediacoverAttribute(): string
{
$media = $this->mediaList()->whereHas('typeMedia', function ($q) {
// * Where type is Photo
$q->where('codeTypemedia', 1);
})->first();
if ($media) {
return $media->thumb_relative_path;
2022-04-01 10:18:04 +02:00
}
return '';
2022-04-01 10:18:04 +02:00
}
2019-07-31 12:22:24 +02:00
}