[英]Repository Pattern on Laravel
I would like to use the Repository Pattern
but I am stuck on the syntax. 我想使用
Repository Pattern
但我坚持语法。 I want to get my function index(). 我想得到我的函数index()。
First step, I create my folder Repositories
and I create the file AuteurRepository.php
第一步,我创建文件夹
Repositories
然后创建文件AuteurRepository.php
In my file AuteurController I have this: 在我的文件AuteurController中我有这个:
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 在我的模型中,我只有一个文件Auteur
protected $fillable = ['name', 'firstname'];
I have 2 questions: 我有两个问题:
1) In my file AuteurRepository how should I create my function index() ? 1)在我的文件AuteurRepository中我应该如何创建我的函数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 ? 我的第二个问题是在我的AuteurController中我不明白该怎么办?
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'))
}
}
1) In my file AuteurRepository how should I create my function index()?
1)在我的文件AuteurRepository中我应该如何创建我的函数index()?
You can give it the name what you want, index(), allRecords(), ... . 你可以给它命名你想要的,index(),allRecords(),.... And perform the query you need.
并执行您需要的查询。
My second question is in my AuteurController I don't understand what to do ?
我的第二个问题是在我的AuteurController中我不明白该怎么办?
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: 在您的控制器中,您可以像下面这样访问repo index函数:
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: 然后将参数传递给repo:
$auteurs = $this->auteurs->index($filters);
Hope it helps ;-) 希望能帮助到你 ;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.