2014-07-02 14 views
0

新來Laravel的方法型號新的類實例,仍然新鮮OOP!我認爲這與OOP有關,而不是嚴格的Laravel。Laravel/PHP:在本型號

所以我的主要問題是,我想通過我的模型稱爲收入到一個叫PlannerController控制器的方法從所謂的「fin_income_category」一個數據庫表傳遞的所有行。要做到這一點,我創建的收入稱爲getIncomeCategories()

首先內的靜態方法,這裏是量入爲出我__construct方法:

public function __construct($income, array $attributes = array()){ 
    parent::__construct($attributes); 

    $this->table = $income; 
} 

這裏是getIncomeCategories方法也收入中:

public static function getIncomeCategories(){ 
    $category = new self('fin_income_category'); 
    $categories = $category->all(); 
    return $categories; 
} 

最後,這裏是PlannerController中的編輯($ id)方法,我將調用此方法並將類別傳遞給我的視圖。請注意,只有在這個函數的第一個語句是一個問題......別人做工精細:

public function edit($id) 
{ 
    $income_categories = Income::getIncomeCategories(); 

    $newIncome = new Income('fin_income'); 
    $newRecord = $newIncome->where('id', '=', $id)->get(); 

    return View::make('planner.edit', array('record'=>$newRecord, 'categories'=>$income_categories)); 
} 

當我運行的代碼,這樣我收到一個錯誤從Laravel:

ErrorException

缺少參數1用於收入:: __構建體(), 稱爲/opt/lampstack/frameworks/laravel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php上線615和限定

在其他情況下,我已經實例化一個新的收入,我還沒有收到這個錯誤。

回答

0

更改getIncomeCategories()這個

public static function getIncomeCategories(){ 
      $category = new self('fin_income_category'); 
      return $category->get()->toArray(); 
    } 

之所以你的代碼沒有工作是因爲在

Illuminate\Database\Eloquent\Model; the code snippet is found below 

public static function all($columns = array('*')) 
{ 
    $instance = new static; 

    return $instance->newQuery()->get($columns); 
} 

發現雄辯all()被實例化這勢必靜態類被叫類,這是你的Income模型類,你的情況,並在此過程中,要求構造函數中的參數