ldc-dam/app/Models/Panier.php
2022-04-01 10:18:04 +02:00

111 lines
2.6 KiB
PHP

<?php
namespace App\Models;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Panier extends Model
{
use SoftDeletes;
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'mediacount', 'mediacover'
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'description', 'libelle', 'statut'
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'statut' => 'boolean'
];
// * RELATIONSHIPS
/**
* 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');
}
/**
* BelongsToMany users relation
* The users that belong to the panier.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function usersFavoris()
{
return $this->belongsToMany(User::class, 'paniers_users', 'panier_id', 'user_id');
}
/**
* BelongsToMany medias relation
* The medias that belong to the panier.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function medias()
{
return $this->belongsToMany(Media::class, 'paniers_medias', 'panier_id', 'media_id');
}
// * GETTERS
/**
* Get mediacount attribute
*
* @return boolean
*/
public function getMediacountAttribute(): int
{
return $this->medias()->count();
}
/**
* Get mediacover attribute
*
* @return string
*/
public function getMediacoverAttribute(): string
{
$cover = '';
if ($media = $this->medias()->where('typemedia_id', 1)->first()) {
switch ($media->typemedia_id) {
case 1:
if ($media->extension == "pdf" || $media->extension == "ai" || $media->extension == "psd") {
$cover = $media->folder . $media->uid . "_thumb.png";
} else {
$cover = $media->folder . $media->uid . "_thumb." . $media->extension;
}
break;
case 2:
$cover = $media->folder . $media->uid . "_thumb." . $media->extension;
break;
}
}
return $cover;
}
}