Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. class Holiday extends Model
  2. {
  3. protected $table = 'holidays';
  4. protected $primaryKey = 'holiday_id';
  5. public $timestamps = false;
  6. public function dates(){
  7. return $this->hasOne('AppHolidayDates', 'holiday_id');
  8. }
  9. public function images(){
  10. return $this->hasMany('AppHolidayImages', 'holiday_id');
  11. }
  12. public function info(){
  13. return $this->hasOne('AppHolidayInfo', 'holiday_id');
  14. }
  15. public function pricing(){
  16. return $this->hasOne('AppHolidayPricing', 'holiday_id');
  17. }
  18. }
  19.  
  20. class HolidayInfo extends Model
  21. {
  22. protected $table = 'holiday_info';
  23. public $timestamps = false;
  24. protected $primaryKey = 'holiday_id';
  25. public function holiday(){
  26. return $this->hasOne('AppHoliday', 'holiday_id');
  27. }
  28. }
  29.  
  30. $holidays = Holidays::with('info')->get();
  31.  
  32. Holidays::where('country', $country)->with('info')->get();
  33.  
  34. SQLSTATE[42S22]: Column not found: 1054 Unknown column 'country' in 'where clause' (SQL: select * from holidays where country = australia)
  35.  
  36. SELECT * from holidays JOIN holiday_info ON holidays.holiday_id = holiday_info.holiday_id
  37.  
  38. public function info(){
  39. return $this->hasOne('AppHolidayInfo', 'holiday_id');
  40. }
  41.  
  42. Holiday::where(your condition)->with('info')->get();
  43.  
  44. public function info(){
  45. return $this->hasOne('AppHolidayInfo', 'holiday_id','holiday_id');
  46. }
  47.  
  48. hasOne($related, $foreignKey, $localKey )
  49.  
  50. $holidays = Holiday::where(your condition)->get();
  51.  
  52. foreach($holidays as $holiday){
  53. $holiday->info->youInfoFieldHere
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement