John Davidson

php - take 10 records and skip 10 records in Laravel

0 comments
Message:


I have 20 records in my SQL table. I want to take the first 10 records (0-10) for the sender_id then skip and take the last 10 records (10-20) for the receiver_id


    public function definition()
{
return [
'sender_id' => User::limit(10)->inRandomOrder()->get()->first()->id,
'receiver_id' => User::skip(10)->take(10)->inRandomOrder()->get()->first()->id,
];
}

The problem is that even after defining the limit at 10 I still have records with id higher than 10 in the 'sender_id' which is supposed to be (1,2,3,4,5,6,7,8,9,10)



My Answer:
> DB::table('table_name')->select('*')->take(10)->get(),
'receiver_id' => DB::table('table_name')->select('*')->skip(10)->take(10)->get()
];
}

Rate this post

3 of 5 based on 7711 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us