Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.34 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use Carbon\Carbon;
  6. use Illuminate\Database\Eloquent\Model;
  7.  
  8. class Film extends Model
  9. {
  10.     protected $fillable = ['title','plot','user_id','released_at'];
  11.     protected $dates = ['released_at'];
  12.  
  13.  
  14.     //add the full time to the released at attribute (date mutators).
  15.     public function setReleasedAtAttribute($date)
  16.     {
  17.         $this->attributes['released_at'] = Carbon::createFromFormat('Y-m-d',$date);
  18.     }
  19.  
  20.     public function getReleasedAtAttribute($data)
  21.     {
  22.         return (new Carbon($data));
  23.     }
  24.  
  25.  
  26.     /** A scope that gets the released film records only
  27.      * @param $query
  28.      */
  29.     public function scopeReleased($query)
  30.     {
  31.         $query->where('released_at','<=',Carbon::now());
  32.     }
  33.  
  34.     /** A scope that gets the unreleased film records only
  35.      * @param $query
  36.      */
  37.     public function scopeUnreleased($query)
  38.     {
  39.         $query->where('released_at','>',Carbon::now());
  40.     }
  41.  
  42.     /*
  43.     *A laravel relationship - one to many, get the owner of a movie
  44.     *
  45.     */
  46.     public function owner()
  47.     {
  48.         return $this->belongsTo('App\User');
  49.     }
  50.  
  51.     /*
  52.     *A laravel relationship - Many to Many, get the associated genres of the specified movie
  53.     *
  54.     */
  55.     public function genres()
  56.     {
  57.         return $this->belongsToMany('App\Genre');
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement