File: //var/www/matriculas_api_dev/app/Repositories/CommuneRepository.php
<?php
namespace App\Repositories;
use App\Models\Commune;
class CommuneRepository
{
public function getAll()
{
$registers = Commune::where('status', true);
return $registers->orderBy('commune', 'asc')->get();
}
public function getById($id)
{
$register = Commune::where('status', true)->where('id', $id)->first();
if (!$register) {
return false;
}
return $register;
}
public function getByCode($code, $id = null)
{
$query = Commune::where('status', true)->where('code', strLower($code));
if ($id > 0) {
$query->where('id', '!=', $id);
}
$register = $query->first();
if (!$register) {
return false;
}
return $register;
}
public function getByName($data, $id = null)
{
$query = Commune::where('commune', strUpper($data->commune));
if ($id > 0) {
$query->where('id', '!=', $id);
}
$register = $query->first();
if (!$register) {
return false;
}
return $register;
}
public function create($data)
{
return Commune::create([
'code' => $data->code,
'commune' => $data->commune,
'region_id' => $data->region,
'created_at' => now(),
'updated_at' => now(),
]);
}
public function update($register, $data)
{
$register->code = $data->code;
$register->commune = $data->commune;
$register->region_id = $data->region;
$register->updated_at = now();
if ($register->save()) {
return true;
}
return false;
}
}