I am trying to create a factory for a "posts" model
I need the community_id field filled with random selections from the communities table in the id column. I also need to fill the user_id field with random selections from the communities table in the id column.
Can you please help me with how to code this?
Thanks in advance!
I was able to come up with an answer and it is the following:
Using
DB::table('users')->pluck('id')allows us to use the DB facade provided by Laravel to reference theuserstable and pluck theidcolumn.We are then saving this array of results in a
$userIdsvariable which we then use in thereturninside thedefinition()method, where in the'user_id'key value pair we call$faker->randomElement($userIds)which is a helper function that will select a random item from an array of values passed to it, which in this case we have passed an array of the user ids.I hope this solution helps those who might need assistance in the future with this problem.