ldc-mesbr-app/app/Models/Contact.php

125 lines
2.5 KiB
PHP

<?php
namespace App\Models;
use App\Models\Log;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Contact extends Model
{
use HasFactory;
/**
* @var string
*/
const CREATED_AT = 'contact_createdate';
/**
* @var string
*/
const UPDATED_AT = 'contact_updatedate';
/**
* @var string
*/
protected $table = 'contact';
/**
* @var string
*/
protected $primaryKey = 'contact_id';
/**
* @var array
*/
protected $fillable = [
'contact_ref',
'contact_prenom',
'contact_nom',
'contact_email',
'contact_cp',
'contact_ville',
'contact_country',
];
/**
* OneToMany
* Contact -> Mur
*/
public function murs()
{
return $this->hasMany('App\Models\Mur', 'mur_contactId', 'contact_id');
}
/**
* OneToMany
* Contact -> Panier
*/
public function paniers()
{
return $this->hasMany('App\Models\Panier', 'panier_contactId', 'contact_id');
}
/**
* ManyToOne
* Contact -> Site
*/
public function site()
{
return $this->belongsTo('App\Models\Site', 'contact_siteId');
}
public function scopeSearchName($query, $name)
{
if ($name) {
$query->where('contact_nom', 'like', '%' . $name . '%');
}
}
public function scopeSearchFirstname($query, $firstname)
{
if ($firstname) {
$query->where('contact_prenom', 'like', '%' . $firstname . '%');
}
}
public function scopeSearchEmail($query, $email)
{
if ($email) {
$query->where('contact_email', 'like', '%' . $email . '%');
}
}
public function scopeSearchVille($query, $ville)
{
if ($ville) {
$query->where('contact_ville', 'like', '%' . $ville . '%');
}
}
public function scopeSearchCp($query, $cp)
{
if ($cp) {
$query->where('contact_cp', 'like', '%' . $cp . '%');
}
}
public function scopeSearchCountry($query, $country)
{
if ($country) {
$query->where('contact_country', $country);
}
}
/**
* log relation
*
* @return HasMany
*/
public function logs()
{
return $this->hasMany(Log::class, 'log_contactId', 'contact_id');
}
}