I am new to laravel and can't think of a way how to do it, for example I have 100 posts in my app, i dont want a user to see all of them, I want him to see only some, just in random way. so not all users see all posts, they see only random ones.
use eloquent inRandomOrder()
. check here for more info
Use inRandomOrder()
combined with limit()
. In the example below, we take 10 random entries.
$results = Post::inRandomOrder()->limit(10)->get();
this may help you
public function getRandomPost()
{
$post = Post::inRandomOrder()
->where('approved', true)->first();
return redirect()->route('posts.show', ["id" => $post->id]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.