0

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!

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.