所以我試圖從子類編輯屬性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');
}
}
現在我需要知道如何解決它的方法。
EDIT1
我得到的錯誤
類型錯誤:傳遞給應用程序參數1 \ HTTP \ \控制器:: CompanyController結構__()必須應用\庫\實例CompanyRepository ,空給出稱爲/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
@jfadich編輯! – DevJoeri
您將'null'作爲第一個參數傳遞給'parent :: __ construct(null,「restaurant」);'但父方法要求您傳遞'CompanyRepository'對象作爲第一個參數....您需要一個'CompanyRepository'對象來通過,而不是那個null –