PHP code example of robertgdev / laravel-base-repository
1. Go to this page and download the library: Download robertgdev/laravel-base-repository library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
robertgdev / laravel-base-repository example snippets
namespace App\Repositories;
use App\Models\User;
use RobertGDev\BaseRepository\BaseRepository;
/**
* @extends BaseRepository<User>
*/
class UserRepository extends BaseRepository
{
public function __construct(User $model)
{
parent::__construct($model);
}
public function model(): string
{
return User::class;
}
}
class UserController
{
public function __construct(
private readonly UserRepository $users,
) {}
public function show(string $id)
{
return $this->users->findOrFail($id);
}
}
class OrderRepository extends BaseRepository
{
public function __construct(Order $model)
{
parent::__construct($model);
}
public function model(): string
{
return Order::class;
}
public function findPendingForUser(int $userId): Collection
{
return $this->query()
->where('user_id', $userId)
->where('status', 'pending')
->orderBy('created_at', 'desc')
->get();
}
public function totalRevenueForMonth(Carbon $date): string
{
return $this->query()
->whereMonth('created_at', $date->month)
->whereYear('created_at', $date->year)
->sum('total');
}
}