PHP Notice: Undefined variable: threads in Psy Shell code on line 1
$threads->each(function($thread){ factory('App\Reply', 10)->create([ 'thread_id' => $thread->id ]); });
what error??
$factory->define(App\Reply::class, function (Faker $faker) {
return [
'thread_id' => function () {
return factory('App\Thread')->create()->id;
},
'user_id' => function () {
return factory('App\User')->create()->id;
},
'body' => $faker->paragraph
];
});
where the $threads come from?
public function index()
{
$threads = Thread::latest()->get();
return view('threads.index', compact('threads'));
}
threadsController.php
No need to use get()
public function index()
{
$threads = Thread::latest();
return view('threads.index', compact('threads'));
}
If you want a single record
public function index()
{
$threads = Thread::latest()->first();
return view('threads.index', compact('threads'));
}
where this was called?
$threads->each(function($thread){ factory('App\Reply', 10)->create([ 'thread_id' => $thread->id ]); });
you able to get the $threads collection before this?
in tinker, you need to also run the database query. Tinker is telling you that it does not know what $threads is
can screenshot how all the commands are called..
use alternative command like:
$threads=Thread::all();
$threads->each(function($thread){ factory('App\Reply', 10)->create([ 'thread_id' => $thread->id, 'user_id' => $thread->user_id ]); });
Please or to participate in this conversation.