Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. Schema::create('schedules', function(Blueprint $table)
  2. {
  3. $table->increments('id');
  4. $table->integer('user_id', false, true);
  5. $table->integer('client_id', false, true);
  6. $table->datetime('for');
  7. $table->enum('type', array('template', 'revision', 'common'));
  8. $table->string('name', 50)->default('Untitled Template');
  9. $table->boolean('is_published');
  10. $table->timestamp('published_at');
  11. $table->softDeletes();
  12. $table->timestamps();
  13. });
  14.  
  15. Schema::create('shifts', function(Blueprint $table)
  16. {
  17. $table->increments('id');
  18. $table->integer('schedule_id', false, true);
  19. $table->integer('user_id', false, true);
  20. $table->foreign('schedule_id')->references('id')->on('schedules')->onDelete('cascade');
  21. $table->softDeletes();
  22. $table->timestamps();
  23. });
  24.  
  25. User::find($userId)
  26. ->shifts()
  27. ->with('schedule')
  28. ->where('is_published', true)
  29. ->orderBy('year', 'asc')
  30. ->orderBy('month', 'asc')
  31. ->orderBy('day', 'asc')
  32. ->take(5)
  33. ->get();
  34.  
  35. User::find($userId)
  36. ->schedules()
  37. ->has('shifts')
  38. ->where('is_published', true)
  39. ->orderBy('year', 'asc')
  40. ->orderBy('month', 'asc')
  41. ->orderBy('day', 'asc')
  42. ->take(5)
  43. ->get();
  44.  
  45. $results = User::find($userId)
  46. ->schedules()
  47. ->where('is_published', true)
  48. ->orderBy('year', 'asc')
  49. ->orderBy('month', 'asc')
  50. ->orderBy('day', 'asc')
  51. ->take(5)
  52. ->get();
  53.  
  54. public function schedules(){
  55. return $this->belongsToMany('Schedule', 'shifts');
  56. }
  57.  
  58. foreach($results as $result){
  59. echo $result->pivot
  60. }
  61.  
  62. class Shift extends IlluminateDatabaseEloquentRelationsPivot
  63.  
  64. public function newPivot(Eloquent $parent, array $attributes, $table, $exists){
  65. if ($parent instanceof User) {
  66. return new Shift($parent, $attributes, $table, $exists);
  67. }
  68. return parent::newPivot($parent, $attributes, $table, $exists);
  69. }
  70.  
  71. // User model
  72. public function shifts()
  73. {
  74. return $this->hasManyThrough('Shift', 'Schedule');
  75. }
  76.  
  77. $now = CarbonCarbon::now();
  78.  
  79. $upcomingShifts = $user->shifts()
  80. ->where('schedules.is_published', 1)
  81. ->where('schedules.for', '>', $now)
  82. ->orderBy('schedules.for')
  83. ->take(5)
  84. ->get();
  85.  
  86. // User model
  87. public function getUpcomingShifts($limit = 5)
  88. {
  89. $joinTable = $this->shifts()->getParent()->getTable(); // schedules
  90.  
  91. $now = CarbonCarbon::now();
  92.  
  93. return $this->shifts()
  94. ->where($joinTable.'.is_published', 1)
  95. ->where($joinTable.'.for', '>', $now)
  96. ->orderBy($joinTable.'.for')
  97. ->take($limit)
  98. ->get();
  99. }
  100.  
  101. // then
  102. $user->getUpcomingShifts();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement