Repository Pattern on Laravel
1) In my file AuteurRepository how should I create my function index()?
You can give it the name what you want, index(), allRecords(), ... . And perform the query you need.
My second question is in my AuteurController I don't understand what to do ?
If your repository looks like this:
class AuteurRepository{ public function index() { return Auteur::oldest()->paginate(5); }}
In your controller you can access the repo index function like this:
class AuteurController extends Controller{ protected $auteurs; public function __construct(AuteurRepository $auteurs) { $this->auteurs = $auteurs; } public function index(Request $request) { $auteurs = $this->auteurs->index(); return view('admin.auteurs.index', compact('auteurs')) }}
EDIT
Also, you can customize a bit the query. For example:
In the repository accept a parameter in the index method:
class AuteurRepository{ public function index($filters) { $pagination = $filters['pagination']; $order = $filters['order']; return Auteur::orderBy('created_at', $order) ->paginate($pagination); }}
And in the controller create the array to pass as parameter:
$filters = [ 'pagination' => 5, 'order' => 'asc', ];
or
$filters = [ 'pagination' => 10, 'order' => 'desc', ];
Or you can get the values from the request (be sure to leave a default value in case the request input is null)
$filters = [ 'pagination' => $request->input('pagination')?: 5, 'order' => $request->input('order')?: 'asc', ];
and then pass the parameter to the repo:
$auteurs = $this->auteurs->index($filters);
Hope it helps ;-)