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

    PHP - Infinite Scroll Pagination using JQuery Ajax Example

    PHP - Infinite Scroll Pagination using JQuery Ajax Example

    Open Post

    How to integrate stripe payment gateway in Laravel 5.4 Application ?

    How to integrate stripe payment gateway in Laravel 5.4 Application ?

    Open Post

    How to change date format in jquery using moment JS ?

    How to change date format in jquery using moment JS ?

    Open Post

    Laravel 5.2 chat message module using socket.io, redis, express and nodejs from from scratch.

    Laravel 5.2 chat message module using socket.io, redis, express and nodejs from from scratch.

    Open Post

    Laravel 5 Facebook authentication using Socialite Package

    Laravel 5 Facebook authentication using Socialite Package

    Open Post

    How to create authentication(login and registration) in Laravel 5.2?

    How to create authentication(login and registration) in Laravel 5.2?

    Open Post