PHP code example of nathandunn / model-repositories

1. Go to this page and download the library: Download nathandunn/model-repositories 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/ */

    

nathandunn / model-repositories example snippets




namespace App\Records;

use App\Users\User;
use Illuminate\Database\Eloquent\Builder;
use NathanDunn\Repositories\Repository;

class RecordRepository extends Repository
{
    /**
     * @param Record $record
     */
    public function __construct(Record $record)
    {
        parent::__construct($record);
    }

    public function forUser(User $user): Builder
    {
      return $this->model->where('user_id', '=', $uuid);
    }
}



namespace App\Http\Controllers;

use App\Http\Resources\RecordResource;
use App\Records\RecordRepository;
use Illuminate\Http\Request;

class RecordsController extends Controller
{
    protected RecordRepository $recordRepository;
    
    public function __construct(RecordRepository $recordRepository)
    {
        $this->recordRepository = $recordRepository;
    }
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $user = $request->user();
        $records = $this->recordRepository->getForUser($user);
        
        return RecordResource::collection($records);
    }
}