PHP Fatal error: Call to undefined function factory() in Psy Shell code on line 1
PHP Fatal error: Call to undefined function factory() in Psy Shell code on line 1
Guys, please help, after 3 days of researching this error on the internet I haven't found any answer, I guess it is related to the fact that I am watching Jeffrey's Laravel 6 tutorials, and I am using 8 at the moment. The issue appeared while trying to create fake test data for the database using this in Laravel Tinker Articless:factory()->count(10)->create();. I understand it is not able to connect with the model, but I don't know why as I followed every little thing.
PS it works fine for the User, I can create test user data
Here is the code:
class Articless extends Model
{
use HasFactory;
protected $guarded = [];
public function path(){
return route('articles',$this);
}
public function user(){
return $this->belongsTo(User::class);
}
class Articless extends Model {
use HasFactory;
protected $guarded = [];
public function path()
{
return route('articles',$this);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
I have followed and applied the conventions: but still.... sam error
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Articless extends Model {
use HasFactory;
protected $guarded = [];
public function path()
{
return route('articles',$this);
}
public function user()
{
return $this->belongsTo(User::class);
}
protected static function newFactory()
{
return ArticleFactory::new();
}
}
I think your issue is that you calling method factory() on \App\Models\User::class.
But instead you should call it like this \App\Models\User::factory().
Try to replace your code:
namespace Database\Factories;
use App\Models\Articless;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class ArticlessFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Articless::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'user_id'=>\App\Models\User::factory(),
'title'=>$this->faker->sentence,
'excerpt'=>$this->faker->sentence,
'body'=>$this->faker->paragraph
];
}
}
Thanks for all your contributions, I will just continue without this, I hope it is not very important, I can upload manually, Thank you all for your time spent with me. I hope one day I will be able to help you as you did to me.
I have had similar problems and I just started the whole tutorial over again. I am presently following "Build a forum with tdd" on laracasts. that is laravel 5.8 and i am using 8. it is not easy but it is good practice, and it all translates over. A lot of the stuff in that collection has yet to be duplicated in laravel 6, 7 or 8 tutorials.
Finally fixed it, whoever encounters this error, I have run "composer dumpautoload", after it worked as normal. You don't imagine how happy I am, after almost one week of trying to solve this minor issue-> getting the result. Even though experienced guys will laugh on this, I am really happy now :)))))))))))))))))))))))))))))
I have the same issue getting. In my case I have created retailer as a new guard /middleware and running Retailer::factory()->count(100)->make(); gives me the error
Retailer::factory()->count(100)->make();
PHP Error: Call to undefined method App\Http\Middleware\Retailer::factory() in Psy Shell code on line 1