mardi 4 juin 2019

Repository Pattern on Lavel

I would like to use the Repository Patern but I am stuck about the syntax. I want to get my function index().

First step, I create my folder Repositories and I create the file AuteurRepository.php

enter image description here

In my file AuteurController I have this:

public function index()
{
   $auteurs = Auteur::oldest()->paginate(5);
   return view('admin.auteurs.index', compact('auteurs'))
           ->with('i', (request()->input('page', 1)-1)*5);
}

And in my Model I only have a file Auteur

protected $fillable = ['name', 'firstname'];

I have 2 questions:

1) In my file AuteurRepository how should I create my function index()?

I have tried this ?

<?php 

namespace App\Repositories; 
use App\Auteur; 

class AuteurRepository
{
    public function index()
    {
        return Auteur::oldest()->paginate(5);
    }
}


?>

My second question is in my AuteurController I don't understand what to do ?

I have this for now

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Auteur;
use App\Repositories\AuteurRepository; 

class AuteurController extends Controller
{


    protected $auteurs;

    public function __construct(AuteurRepository $auteurs)
    {
        $this->auteurs = $auteurs; 
    }

    public function index(Request $request)
    {

        return view('admin.auteurs.index', compact('auteurs'))
    }
}

Aucun commentaire:

Enregistrer un commentaire