Hưng Trịnh
Hưng Trịnh

Reputation: 1047

Laravel how to get data random row use Postgres SQL?

In Laravel 5 framework: How can I select a random row using Eloquent Postgres SQL?

i want get random 3 recomment product.

i find code:

$recomment_product = Product::whereRaw("name = '".$product->name."' and gender = '".$product->gender."' and client_target = '".$product->client_target."'")->orderByRaw(DB::raw("RAND()"))->take(3)->get();

But it not work. Please Help!

Upvotes: 0

Views: 192

Answers (1)

IndianCoding
IndianCoding

Reputation: 2683

Eloquent has inRandomOrder() method.

$recomment_product = Product::where('name', $product->name)
    ->where('gender', $product->gender)
    ->where('client_target', $product->client_target)
    ->inRandomOrder()
    ->take(3)
    ->get();

Upvotes: 3

Related Questions