0

Sorry for my bad english, I want to get a single row in my object. And I want that in random order. Im using array_rand() and it only return errors as stated below:

ErrorException: array_rand() expects parameter 1 to be array, object given in file C:\xampp\htdocs\user\TestProject\app\Http\Controllers\TestController.php on line

Here is my object.

"my_list": [
   {
     "id": 1,
     "name": "My Name Test",
     "address": [
         {
            "id": 1,
            "city": "Manila",
            "country": "Philippines" 
         }
     ]
   },
   {
     "id": 2,
     "name": "Your Name Test",
     "address": [
         {
            "id": 2,
            "city": "Cebu",
            "country": "Philippines",
         }
     ]
   }
]

The problem is I want only to get a single row to the my_list which is object and not an array.

Here is my code.

$course = Course::where('id', 1)->with('my_list')->first();
$random_list = array_rand($course->my_list);
return $random_list;

I also try adding number of row in the array_rand like this.

$random_list = array_rand($course->my_list, 1);

But still not working.

What did I missed?

1
  • Updated my answer. Could you tell me the reason why it's not working. Any errors being thrown? Commented Jan 17, 2019 at 6:25

6 Answers 6

2

Any Eloquent query returns, by default, a Collection, even for the underlying relationships. Since you are working with one, this should work:

$course->my_list->random(); 

This will return only one item. If you want more, you could pass an argument to the random() method specifying the count of items you want.

For more information, check the documentation.

Sign up to request clarification or add additional context in comments.

Comments

1

This Object is a Laravel collection. Please refer to the collection documentation. https://laravel.com/docs/5.7/collections#method-random

You can try $course->my_list->random()

1 Comment

This is also the answer of Mr. @Mozammil, tried it and it didn't work.
1

If you still wanna do this with your approach, can you try get_object_vars function to cast object into array.

$array = get_object_vars($object);

so that you can use them as an array in array_rand.

You might get an error, hence that it's an multi-dimensional array. Let me know so i may update.

Update for multidimensional:

Please refer to this.

// The second parameter of json_decode forces parsing into an associative array

$array = json_decode(json_encode($object), true);

Comments

1

try this:

$course = Course::where('id', 1)
   ->with(['my_list' => function($query) {
       $query->inRandomOrder();
}])->first();
return $course->my_list;

1 Comment

It returns all the row in the my_list, anyway I added ->first(); in the $query->inRandomOrder()->first(), and working now.
1

Try this method:

$course = Course::where('id', 1)
   ->with(['my_list' => function($query) {
       $query->inRandomOrder()->first();
   }])->first();
return $course->my_list;

this method is more efficient since you will only get 1 row from my_list not like when you use $course->my_list->random() which retrieves all data and from there select a random row.

Comments

0
$random_list = $course['my_list']->random(number);

ps: number = number of element you want to get ,

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.