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

    How to use sweetalert message box in laravel 5?

    How to use sweetalert message box in laravel 5?

    Open Post

    How to install laravel framework on windows xampp server from scratch?

    How to install laravel framework on windows xampp server from scratch?

    Open Post

    How to get keys name from array using array helper in PHP Laravel ?

    How to get keys name from array using array helper in PHP Laravel ?

    Open Post

    Laravel 5 - How to prevent browser back button after user logout?

    Laravel 5 - How to prevent browser back button after user logout?

    Open Post

    How to get filename from file path in MySQL?

    How to get filename from file path in MySQL?

    Open Post

    Laravel 5 export to pdf using maatwebsite example

    Laravel 5 export to pdf using maatwebsite example

    Open Post