2019-07-31 12:22:24 +02:00
|
|
|
<?php
|
|
|
|
|
|
2019-09-12 11:08:25 +02:00
|
|
|
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'
|
|
|
|
|
];
|
2019-08-06 14:18:36 +02:00
|
|
|
|
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');
|
|
|
|
|
}
|
2019-08-06 14:18:36 +02:00
|
|
|
|
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()
|
|
|
|
|
{
|
2022-06-20 14:57:58 +02:00
|
|
|
return $this->belongsToMany(User::class);
|
2022-04-01 10:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-06-20 14:57:58 +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
|
|
|
|
|
*/
|
2022-06-20 14:57:58 +02:00
|
|
|
public function mediaList()
|
2022-04-01 10:18:04 +02:00
|
|
|
{
|
2022-06-20 14:57:58 +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
|
|
|
|
|
{
|
2022-06-20 14:57:58 +02:00
|
|
|
return $this->mediaList()->count();
|
2022-04-01 10:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get mediacover attribute
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getMediacoverAttribute(): string
|
|
|
|
|
{
|
2022-06-20 14:57:58 +02:00
|
|
|
$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
|
|
|
}
|
2022-06-20 14:57:58 +02:00
|
|
|
return '';
|
2022-04-01 10:18:04 +02:00
|
|
|
}
|
2019-07-31 12:22:24 +02:00
|
|
|
}
|