Tag : RAND()


Laravel 5 - Orderby random using rand() and DB::raw() example

Sometimes, we require to get randomly data using laravel query builder. you can use mysql rand() with order by. If you want to get random data using laravel eloquent then we need to use DB::raw(). In bellow example you can i use DB::raw(). In this example i use DB::raw('RAND()') inside orderBy() and also add limit of 8 number of record, so you can easily implement in your laravel project. so let's try... <pre class="prettyprint lang-php"> return DB::table("posts") ->select("posts.*") ->orderBy(DB::raw('RAND()')) ->take(8) ->get(); </pre>

By Hardik Savani | May 4, 2016 | | 3155 Viewer | Category : Laravel
READ MORE

    Laravel 5 - How to get difference of time in minutes?

    Laravel 5 - How to get difference of time in minutes?

    Open Post

    How to get last record from MySQL table in Codeigniter?

    How to get last record from MySQL table in Codeigniter?

    Open Post

    PHP - How to convert HTML file to PDF using Dompdf library?

    PHP - How to convert HTML file to PDF using Dompdf library?

    Open Post

    How to create custom facade in laravel 5.2?

    How to create custom facade in laravel 5.2?

    Open Post

    Laravel 5 custom pagination view example

    Laravel 5 custom pagination view example

    Open Post

    How to implement lazy loading image with example for your website?

    How to implement lazy loading image with example for your website?

    Open Post