Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 语法错误,意外'$工厂';(T_变量)、期望函数(T_函数)或常数(T_常数)_Laravel_Faker - Fatal编程技术网

Laravel 语法错误,意外'$工厂';(T_变量)、期望函数(T_函数)或常数(T_常数)

Laravel 语法错误,意外'$工厂';(T_变量)、期望函数(T_函数)或常数(T_常数),laravel,faker,Laravel,Faker,目前我使用的是Laravel8.17* 我试图使用faker库添加数据,但给定错误“语法错误、意外的“$factory”(T_变量)、预期函数(T_函数)或常量(T_常量)”可以帮助我解决此错误 ProductFactory.php <?php namespace Database\Factories; use App\Models\Model\Product; use Illuminate\Database\Eloquent\Factories\Factory; us

目前我使用的是Laravel8.17*

我试图使用faker库添加数据,但给定错误“语法错误、意外的“$factory”(T_变量)、预期函数(T_函数)或常量(T_常量)”可以帮助我解决此错误

ProductFactory.php

<?php

  namespace Database\Factories;

  use App\Models\Model\Product;
  use Illuminate\Database\Eloquent\Factories\Factory;
  use Illuminate\Support\Str;
  use Faker\Generator as Faker;
  
   class ProductFactory extends Factory
   {
     /**
       * The name of the factory's corresponding model.
       *
       * @var string
       */
       protected $model =  \App\Models\Model\Product::class;

       /**
         * Define the model's default state.
         *
         * @return array

      public function definition()
      {
       return [
        //
       ];
      } */


     $factory->define(App\Models\Model\Product::class,function(Faker $faker){
    return [
        'name'=>$this->$faker->Word,
        'detail'=>$this->$faker->paragraph,
        'price'=>$this->$faker->numberBetween(99,999),
        'stock'=>$this->$faker->randomDigit,
        'discount'=>$this->$faker->numberBetween(2,30)
    ];
 });
}

Laravel 8引入了基于类的模型工厂,因此示例定义必须与

<?php

  namespace Database\Factories;

  use App\Models\Model\Product;
  use Illuminate\Database\Eloquent\Factories\Factory;
  use Illuminate\Support\Str;
  use Faker\Generator as Faker;
  
   class ProductFactory extends Factory
   {
        /**
         * The name of the factory's corresponding model.
         *
         * @var string
         */
         protected $model =  \App\Models\Model\Product::class;

        /**
         * Define the model's default state.
         *
         * @return array
         */

        public function definition()
        {
            return [
                'name'=>$this->faker->Word,
                'detail'=>$this->faker->paragraph,
                'price'=>$this->faker->numberBetween(99,999),
                'stock'=>$this->faker->randomDigit,
                'discount'=>$this->faker->numberBetween(2,30)
            ];
        }
    } 

该代码不可编译。您不能在函数外的类中调用方法如果downvoting请说明downvote的原因。OP的错误可以通过根据答案更正类定义来解决——所以为什么downvoteI没有进行下一票,但有时您会在询问语法错误的问题上获得下一票,因为这些问题应该(在本例中已经)关闭。你的回答没有错,但对这类问题的首选回答是“标记/投票结束”。@TimLewis谢谢你的解释,我会记住这一点。我一直认为,即使是刚开始编写代码的人也可以寻求帮助,大多数情况下,刚开始编写代码的人都会遇到语法错误的问题;我从你那里看到了很多有用的答案,你的得票率高的答案远远超过了你的得票率低的答案。不要让一次否决票太麻烦你,但是,是的,要知道,在这类问题上,他们往往更常见。干杯