简体   繁体   English

在 Laravel 中,如何从用户表中检索随机 user_id 以生成模型工厂种子数据?

[英]In Laravel, how do I retrieve a random user_id from the Users table for Model Factory seeding data generation?

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.我想从已经存储在用户表中的用户 ID 之一生成一个随机的 user_id。 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.我很难过,因为我不知道如何正确显示数据输出以编码,我想知道如何让 Laravel 选择一个随机用户 ID 并插入到数据库中。 Thank you!谢谢! :) :)

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()->id,
    'body' => $faker->paragraph
  ];
});

Remember that this gets all the user data from your table and then choses an id randomly.请记住,这会从您的表中获取所有用户数据,然后随机选择一个 id。 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 分配给从上述工厂生成的 Reply 对象。

Alternately, you can query for a specific user in the above factory definition.或者,您可以在上述工厂定义中查询特定用户。

'user_id' => User::where('username', 'like', 'test@user.com')->get()->random()->id

If you have a test user set up in your DB this will avoid pulling all the user data.如果您在数据库中设置了测试用户,这将避免提取所有用户数据。

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.这可能比使用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另请参阅: Laravel - Eloquent 或 Fluent 随机行

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

It may not be that efficient but you can use it:它可能效率不高,但您可以使用它:

User::all('id')->random();

or

rand(1,User::count());

or

User::inRandomOrder()->limit(1)->get();

First one will be more faster than User::all()->random()->id;第一个会比User::all()->random()->id;更快User::all()->random()->id; because that all function by default gets '*' as column name parameter.因为默认情况下所有函数都将 '*' 作为列名参数。 So, it will get all the columns for all the rows.因此,它将获取所有行的所有列。

I personally like to use.我个人喜欢用。

App\User::pluck('id')->random()

Change the model name which model you want更改模型名称您想要的模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM