Saturday, March 11, 2017

Retriving two models and order them by date

I'm trying to retrive the 20 most recent elements from two differents models Post and Link and rank them by the field created_at in descending order.

Here is the link class

class Link extends \Illuminate\Database\Eloquent\Model {
  public $incrementing = false;

  public function author() {
    return $this->belongsTo('App\Models\Author');
  }

  public function category() {
    return $this->belongsTo('App\Models\Category');
  }
}

And here the post class

class Post extends \Illuminate\Database\Eloquent\Model {
  public $incrementing = false;

  public function author() {
    return $this->belongsTo('App\Models\Author');
  }

  public function category() {
    return $this->belongsTo('App\Models\Category');
  }
}

How can I do that with eloquent (I'm using it outside Laravel, with Slim)?



via Wizix

Advertisement