Skip to content

Instantly share code, notes, and snippets.

@lukepolo
Last active February 17, 2017 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukepolo/44348a6206dcac182a50279f98f26e0d to your computer and use it in GitHub Desktop.
Save lukepolo/44348a6206dcac182a50279f98f26e0d to your computer and use it in GitHub Desktop.
Current Revision Query
<?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);
});
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ArticleRevision extends Model
{
protected $guarded = ['id'];
/*
|--------------------------------------------------------------------------
| Relations
|--------------------------------------------------------------------------
*/
public function article()
{
return $this->belongsTo(Article::class);
}
}
<?php
namespace App\Http\Controllers;
use App\Models\Article;
use Illuminate\Http\Request;
class AdminArticlesController extends Controller
{
/**
* Display a listing of the resource.
*
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
dd($articles = Article::with('currentRevision')
->ofType(Article::STATUSES['Pending'])
->where('id', '3')
->first()->currentRevision->status
);
// still gives article revision status of draft!
}
}
@lukepolo
Copy link
Author

screen shot 2017-02-17 at 11 08 14 am

Here is my database

@lukepolo
Copy link
Author

Solution

 public function scopeOfType($query, $type)
    {
       return $query->wherehas('currentRevision', function($query) use($type) {
           $query->whereRaw('id = (select id from article_revisions where article_revisions.article_id = articles.id order by id desc limit 1)')
           ->where('status', $type);
       });
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment