我有以下存储库类
<?php
namespace App\Repositories;
use App\Customer;
use App\Comment;
use App\CustomerCode;
use Illuminate\Support\Facades\Request;
class CustomerRepository {
use ValidatesRequests;
public function getAll($search=null,Request $request)
{
if($search){
return Customer::where('name','like',"%$search%")->paginate(5);
}
return Customer::paginate(5);
}
}?>
和控制器类
<?php
namespace App\Http\Controllers;
use App\Repositories\CustomerRepository;
use Illuminate\Http\Request;
class CustomerController extends Controller{
private $customerRepo;
function __construct(CustomerRepository $customerRepo){
$this->customerRepo = $customerRepo;
}
function index(Request $request){
return $this->customerRepo->getAll($request->input('search',null));
}
}
这给了我错误
类型错误:参数2传递给 App \ Repositories \ CustomerRepository :: getAll()必须是。的实例 照亮\支持\外墙\请求,没有给出,调用 C:\ XAMPP \ htdocs中\ avmaxapi \程序\ HTTP \控制器\ CustomerController.php 在第21行
我知道我没有通过第二个论点,但不应该自动注入
我不希望每次都从控制器传递请求对象;
答案 0 :(得分:0)
使用use Illuminate\Http\Request;
而不是:use Illuminate\Support\Facades\Request;
以下内容:
<?php
namespace App\Repositories;
use App\Customer;
use App\Comment;
use App\CustomerCode;
use Illuminate\Http\Request;
class CustomerRepository {
use ValidatesRequests;
public function getAll($search=null,Request $request)
{
if($search){
return Customer::where('name','like',"%$search%")->paginate(5);
}
return Customer::paginate(5);
}
}?>