php - laravel belongstomany with condition -


i have following model.

class training extends \eloquent {      // add validation rules here     public static $rules = [         'name' => 'required',         'city' => 'required',         'province' => 'required',         'budget_year' => 'required|integer',         's_date' => 'required|date',         'e_date' => 'required|date'     ];      // don't forget fill array     protected $fillable = [         'name',         'city',         'province',         'budget_year',         's_date',         'e_date'     ];      public function material(){         return $this->hasmany('material');     }      public function budget(){         return $this->belongstomany('budget')->withpivot('amount');     }      public function budgetbyid($training_id){         $this->belongstomany('budget')->where('training_id', '=', $training_id)->get();     }  } 

when debug budgetbyid method using db::getquerylog, query follow

select budgets.*,  budget_training.training_id pivot_training_id,  budget_training.budget_id pivot_budget_id  budgets inner join budget_training on budgets.id = budget_training.budget_id  budget_training.training_id null , training_id='6' 

which return 0 rows, when try modify query , run in pgadmin, following script works well.

select budgets.*,  budget_training.training_id pivot_training_id,  budget_training.budget_id pivot_budget_id  budgets inner join budget_training on budgets.id = budget_training.budget_id  budget_training.training_id='6' 

notice remove training_id null and laravel generated query. wrong budgetbyidmethod?

you have called get() , didn't use return here:

public function budgetbyid($training_id){     // = in optional in case     $this->belongstomany('budget')->where('training_id', '=', $training_id); } 

you should use this:

public function budgetbyid($training_id){     // = in optional in case     return $this->belongstomany('budget')->where('training_id', '=', $training_id); } 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -