Advertisement
Guest User

Untitled

a guest
Feb 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * The problem : i want to retrieve articles with their current revision of the status of Pending.
  5. *
  6. * Go to the controller, see that im using the scope of `hasType`, follow through into the article model
  7. * you will see that we are using a `whereHas` . Whats funny is its working in the fact if `NONE` of the article revisions
  8. * have a status of pending it works! But if `1` has a status of `pending` it will return the latest revision even if its not
  9. * pending
  10. */
  11.  
  12. namespace App\Models;
  13.  
  14. use Illuminate\Database\Eloquent\Model;
  15.  
  16. class Article extends Model
  17. {
  18.  
  19. protected $guarded = ['id'];
  20.  
  21. const STATUSES = [
  22. 'Draft' => 'draft',
  23. 'Pending' => 'pending',
  24. 'Declined' => 'declined',
  25. 'Approved' => 'approved'
  26. ];
  27.  
  28. /*
  29. |--------------------------------------------------------------------------
  30. | Relations
  31. |--------------------------------------------------------------------------
  32. */
  33.  
  34. public function user()
  35. {
  36. return $this->belongsTo(User::class);
  37. }
  38.  
  39. public function revisions()
  40. {
  41. return $this->hasMany(ArticleRevision::class);
  42. }
  43.  
  44. public function currentRevision()
  45. {
  46. return $this->hasOne(ArticleRevision::class)->latest('id');
  47. }
  48.  
  49. /*
  50. |--------------------------------------------------------------------------
  51. | Scopes
  52. |--------------------------------------------------------------------------
  53. */
  54.  
  55. public function scopeOfType($query, $type)
  56. {
  57. return $query->whereHas('currentRevision', function($query) use($type) {
  58. $query->where('status', $type);
  59. });
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement