Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Schema::create('schedules', function(Blueprint $table)
- {
- $table->increments('id');
- $table->integer('user_id', false, true);
- $table->integer('client_id', false, true);
- $table->datetime('for');
- $table->enum('type', array('template', 'revision', 'common'));
- $table->string('name', 50)->default('Untitled Template');
- $table->boolean('is_published');
- $table->timestamp('published_at');
- $table->softDeletes();
- $table->timestamps();
- });
- Schema::create('shifts', function(Blueprint $table)
- {
- $table->increments('id');
- $table->integer('schedule_id', false, true);
- $table->integer('user_id', false, true);
- $table->foreign('schedule_id')->references('id')->on('schedules')->onDelete('cascade');
- $table->softDeletes();
- $table->timestamps();
- });
- User::find($userId)
- ->shifts()
- ->with('schedule')
- ->where('is_published', true)
- ->orderBy('year', 'asc')
- ->orderBy('month', 'asc')
- ->orderBy('day', 'asc')
- ->take(5)
- ->get();
- User::find($userId)
- ->schedules()
- ->has('shifts')
- ->where('is_published', true)
- ->orderBy('year', 'asc')
- ->orderBy('month', 'asc')
- ->orderBy('day', 'asc')
- ->take(5)
- ->get();
- $results = User::find($userId)
- ->schedules()
- ->where('is_published', true)
- ->orderBy('year', 'asc')
- ->orderBy('month', 'asc')
- ->orderBy('day', 'asc')
- ->take(5)
- ->get();
- public function schedules(){
- return $this->belongsToMany('Schedule', 'shifts');
- }
- foreach($results as $result){
- echo $result->pivot
- }
- class Shift extends IlluminateDatabaseEloquentRelationsPivot
- public function newPivot(Eloquent $parent, array $attributes, $table, $exists){
- if ($parent instanceof User) {
- return new Shift($parent, $attributes, $table, $exists);
- }
- return parent::newPivot($parent, $attributes, $table, $exists);
- }
- // User model
- public function shifts()
- {
- return $this->hasManyThrough('Shift', 'Schedule');
- }
- $now = CarbonCarbon::now();
- $upcomingShifts = $user->shifts()
- ->where('schedules.is_published', 1)
- ->where('schedules.for', '>', $now)
- ->orderBy('schedules.for')
- ->take(5)
- ->get();
- // User model
- public function getUpcomingShifts($limit = 5)
- {
- $joinTable = $this->shifts()->getParent()->getTable(); // schedules
- $now = CarbonCarbon::now();
- return $this->shifts()
- ->where($joinTable.'.is_published', 1)
- ->where($joinTable.'.for', '>', $now)
- ->orderBy($joinTable.'.for')
- ->take($limit)
- ->get();
- }
- // then
- $user->getUpcomingShifts();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement