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 | | 2244 Viewer | Category : Laravel
READ MORE

    MySql comma separated column join in PHP Laravel 5

    MySql comma separated column join in PHP Laravel 5

    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

    Jquery - Display a loading gif image before a image loads using Eager ImageLoader plugin

    Jquery - Display a loading gif image before a image loads using Eager ImageLoader plugin

    Open Post

    AngularJS - convert comma separated string to array example

    AngularJS - convert comma separated string to array example

    Open Post

    How to send mail using mailable in laravel 5.3?

    How to send mail using mailable in laravel 5.3?

    Open Post

    Crop, Resize, Frames etc on selected image in php using Aviary

    Crop, Resize, Frames etc on selected image in php using Aviary

    Open Post