user3209130
user3209130

Reputation: 346

Request class injection in repository class

I have following repository class

    <?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);
    }
}?>

and controller class

<?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));
    }
}

this gives me error

Type error: Argument 2 passed to App\Repositories\CustomerRepository::getAll() must be an instance of Illuminate\Support\Facades\Request, none given, called in C:\xampp\htdocs\avmaxapi\app\Http\Controllers\CustomerController.php on line 21

i know i havenot passed 2nd argument but should not it inject automatically

and i donot want to pass request object everytime from controller;

Upvotes: 0

Views: 917

Answers (1)

Leo
Leo

Reputation: 7420

use use Illuminate\Http\Request;

and not: use Illuminate\Support\Facades\Request;

at the following:

<?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);
    }
}?>

Upvotes: 0

Related Questions