File: /var/www/api_matriculas/app/Repositories/CountryRepository.php
<?php
namespace App\Repositories;
use App\Models\Country;
use Exception;
class CountryRepository
{
public function getAll()
{
$registers = Country::where('status', true);
return $registers->get();
}
public function getById($id)
{
$register = Country::find($id);
if (!$register) {
throw new Exception("PaĆs no existe o fue eliminado", 404);
}
return $register;
}
public function getByName($country, $id = null)
{
$query = Country::where('country', $country);
if ($id > 0) {
$query->where('id', '!=', $id);
}
$register = $query->first();
if (!$register) {
return false;
}
return $register;
}
public function create($data)
{
return Country::create([
'country' => ($data->country),
'nationality' => ($data->nationality),
'status' => $data->status == 0 ? false : true,
'created_at' => now(),
]);
}
public function update($register, $data)
{
$register->country = ($data->country);
$register->status = $data->status == 0 ? false : true;
$register->updated_at = now();
if ($register->save()) {
return true;
}
return false;
}
}