php - Eloquent next record -


i trying last 3 blog post database single variables (for templates). saw implementation @ other thred , works fine next record on third query returns null. opinion problem?

blogcontroller.php:

public function getindex($l = 'hu')     {         $post_last = post::orderby('created_at', 'desc')->first();         $post_2 = $post_last->next($post_last->created_at);         $post_3 = $post_2->next($post_2->created_at);          var_dump($post_3);     } 

post.php:(model)

<?php  namespace civitas;  use illuminate\database\eloquent\model;  class post extends model {     /**      * physical table name      */     protected $table = 'posts';       /**      * next result in record list      *      * @param $created_at      * @return mixed      */     public function next($c) {         return post::where('created_at', '<', $c)->get()->first();     } } 

i can't tell why function doesn't work, suggest try approach:

$posts = post::orderby('created_at', 'desc')->take(3)->get(); $post1 = $posts->shift(); $post2 = $posts->shift(); $post3 = $posts->shift(); 

this run 1 query instead of three. calling shift() on collection return first item , remove second post "first" next time call it.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -