<?php
namespace App\Repository;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
interface EloquentRepositoryInterface {
/**
* Get all models.
*
* @param array $columns
* @param array $relations
* @return Collection
*/
public function all(array $columns = ['*'], array $relations = []): Collection;
/**
* Get all trashed models.
*
* @return Collection
*/
public function allTrashed(): Collection;
/**
* Find model by id.
*
* @param int $modelId
* @param array $columns
* @param array $relations
* @param array $appends
* @return Model
*/
public function findById(
int $modelId,
array $columns = ['*'],
array $relations = [],
array $appends = []
): ? Model;
/**
* Find trashed model by id.
*
* @param int $modelId
* @return Model
*/
public function findTrashedById(int $modelId) : ? Model;
/**
* Find only trashed model by id.
*
* @param int $modelId
* @return Model
*/
public function findOnlyTrashedById(int $modelId) : ? Model;
/**
* Create a model.
*
* @param array $payload
* @return Model
*/
public function create(array $payload) : ? Model;
/**
* Update existing model.
*
* @param int $modelId
* @param array $payload
* @return bool
*/
public function update(int $modelId, array $payload) : bool;
/**
* Delete model by id.
*
* @param int $modelId
* @return bool
*/
public function deleteById(int $modelId): bool;
/**
* Restore model by id.
*
* @param int $modelId
* @return bool
*/
public function restoreById(int $modelId): bool;
/**
* Permanently delete model by id.
*
* @param int $modelId
* @return bool
*/
public function permanentlyDeleteById(int $modelId): bool;
}
<?php
namespace App\Http\Controllers;
use App\Repository\UserRepositoryInterface;
class UserController extends Controller {
private $userRepository;
public function __construct(UserRepositoryInterface $userRepository) {
$this->userRepository = $userRepository;
}
public function index() {
return response()->json($this->userRepository->all(), 200);
}
}
C:\xampp\htdocs\lv\routes\api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('users', [UserController::class, 'index']);