Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * The problem : i want to retrieve articles with their current revision of the status of Pending.
- *
- * Go to the controller, see that im using the scope of `hasType`, follow through into the article model
- * you will see that we are using a `whereHas` . Whats funny is its working in the fact if `NONE` of the article revisions
- * 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
- * pending
- */
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- class Article extends Model
- {
- protected $guarded = ['id'];
- const STATUSES = [
- 'Draft' => 'draft',
- 'Pending' => 'pending',
- 'Declined' => 'declined',
- 'Approved' => 'approved'
- ];
- /*
- |--------------------------------------------------------------------------
- | Relations
- |--------------------------------------------------------------------------
- */
- public function user()
- {
- return $this->belongsTo(User::class);
- }
- public function revisions()
- {
- return $this->hasMany(ArticleRevision::class);
- }
- public function currentRevision()
- {
- return $this->hasOne(ArticleRevision::class)->latest('id');
- }
- /*
- |--------------------------------------------------------------------------
- | Scopes
- |--------------------------------------------------------------------------
- */
- public function scopeOfType($query, $type)
- {
- return $query->whereHas('currentRevision', function($query) use($type) {
- $query->where('status', $type);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement