【发布时间】:2017-08-08 07:48:28
【问题描述】:
所以我试图从子类编辑属性category,但由于某些原因,我得到了一个错误。这我知道为什么,因为需要有 2 个参数,但在父类中已经设置了一个。
代码:
孩子
class RestaurantController extends CompanyController
{
public function __construct(){
parent::__construct(null, "restaurant");
//$this->category = "restaurant";
}
public function getCompany($slug){
$company = parent::index($slug);
return view("restaurant.profile")->withInformation($company);
}
}
父母
class CompanyController extends Controller
{
protected $company;
public $category;
public function __construct(CompanyRepository $company, $category = '')
{
$this->category = $category;
$this->company = $company;
}
public function index($slug)
{
$company = $this->company->getCompany($this->category, $slug);
return compact('company');
}
}
现在我需要知道如何解决它。
编辑1
我遇到的错误
类型错误:传递给 App\Http\Controllers\CompanyController::__construct() 的参数 1 必须是 App\Repositories\CompanyRepository 的实例,给定 null,在 /var/www/atify.info/dev-system 中调用/app/Http/Controllers/RestaurantController.php 第 16 行
edit2
这个孩子
class RestaurantController extends CompanyController
{
public function getCompany($slug){
$company = parent::index($slug);
return view("restaurant.profile")->withInformation($company);
}
}
父母
use App\Repositories\CompanyRepository;
class CompanyController extends Controller
{
protected $company;
public function __construct(CompanyRepository $company)
{
$this->company = $company;
}
public function index($slug)
{
$company = $this->company->getCompany($slug);
return compact('company');
}
}
所以我需要一个类别(用于额外检查。否则您可能会在另一个具有错误功能的孩子中检索公司),因为我有很多孩子,每个孩子都有特殊的功能
【问题讨论】:
-
请包含您遇到的错误。
-
@jfadich 已编辑!
-
您将
null作为第一个参数传递给parent::__construct(null, "restaurant");,但父方法要求您将CompanyRepository对象作为第一个参数传递....您需要实例化一个CompanyRepository要传递的对象而不是那个 null -
@MarkBaker True 我知道 null 中断,但
CompanyRepository设置在CompanyController中。是不是有某种东西会忽略第一个论点。有点像默认值?
标签: php laravel oop laravel-5.2