在 Laravel 中,如何从用户表中检索随机 user_id 以生成模型工厂种子数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44102483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Laravel, how do I retrieve a random user_id from the Users table for Model Factory seeding data generation?
提问by Simon Suh
Currently, in my ModelFactory.php, I have:
目前,在我的 ModelFactory.php 中,我有:
$factory->define(App\Reply::class, function (Faker\Generator $faker) {
return [
'thread_id' => 1,
'user_id' => 1,
'body' => $faker->paragraph
];
});
I would like to generate a random user_id from one of the user ID's already stored in the user table. I'm stumped because I don't know the way to display data output to code properly, and I was wondering how I would be able to allow Laravel to choose a random user ID and insert into the database. Thank you! :)
我想从已经存储在用户表中的用户 ID 之一生成一个随机的 user_id。我很难过,因为我不知道如何正确显示数据输出以编码,我想知道如何让 Laravel 选择一个随机用户 ID 并插入到数据库中。谢谢!:)
回答by ayip
Try the below.
试试下面的。
use App\User; // Assuming this is your User Model class with namespace.
$factory->define(App\Reply::class, function (Faker\Generator $faker) {
return [
'thread_id' => 1,
'user_id' => User::all()->random()->user_id,
'body' => $faker->paragraph
];
});
Remember that this gets all the user data from your table and then choses an id randomly. So if your table has huge amount of data, it is not recommended. Instead, in your Test Case, you can create a new User (via its own factory) and assign the id to the Reply object generated from the above factory.
请记住,这会从您的表中获取所有用户数据,然后随机选择一个 id。所以如果你的表数据量很大,不推荐。相反,在您的测试用例中,您可以创建一个新用户(通过其自己的工厂)并将 id 分配给从上述工厂生成的 Reply 对象。
Alternately, you can query for a specific user in the above factory definition.
或者,您可以在上述工厂定义中查询特定用户。
'user_id' => User::where('username', 'like', '[email protected]')->get()->random()->user_id
If you have a test user set up in your DB this will avoid pulling all the user data.
如果您在数据库中设置了测试用户,这将避免提取所有用户数据。
回答by Nelson Aguiar
It does not work like that 'user_id':
它不像'user_id'那样工作:
User::all()->random()->user_id
But that's how it works:
但这就是它的工作原理:
User::all()->random()->id
回答by Henk Poley
Any class that extends Illuminate\Database\Eloquent\Model
will be able to do this:
任何扩展的类Illuminate\Database\Eloquent\Model
都可以这样做:
User::inRandomOrder()->first()
Or to get a Collection of 3 items:
或获取 3 个项目的集合:
User::inRandomOrder()->limit(3)->get()
This might be more efficient than using User::all()->first()
, which ought to first query all Users.
这可能比 using 更有效User::all()->first()
,后者应该首先查询所有用户。
Your IDE (like PhpStorm) will probably be wildly confused that this is an option though.
您的 IDE(如 PhpStorm)可能会非常困惑,认为这是一个选项。
Also see: Laravel - Eloquent or Fluent random row
回答by Pranta Saha
I personally like to use.
我个人喜欢用。
App\User::pluck('id')->random()
Change the model name which model you want
更改模型名称您想要的模型