-1

currently i m using laravel 8.17.*

i m trying to to add data using faker library but given error "syntax error, unexpected '$factory' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST)" Help me to solve this error

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)
    ];
 });
}

**[enter image description here][1] [1]: https://i.stack.imgur.com/CvZND.png

1 Answers1

0

Laravel 8 introduces class based model factories so the sample definition must be like

<?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)
            ];
        }
    } 
Donkarnash
  • 12,433
  • 5
  • 26
  • 37
  • If downvoting pls care to state the reason for downvote. OP 's error can be resolved by rectifying the class definition as per the answer - so why the downvote – Donkarnash Dec 07 '20 at 16:23
  • I didn't downvote, but you sometimes get downvotes on questions that are asking about a syntax error, as those should be (and in this case has been) closed as such. There's nothing wrong with your answer, but flagging/voting to close is the preferred response to this type of question. – Tim Lewis Dec 07 '20 at 16:25
  • @TimLewis thanks for explaining will keep that in mind. I always thought the SO is place where even ones just starting to code can seek help and most of the times just starters would be facing issues with syntax errors. – Donkarnash Dec 07 '20 at 16:31
  • No worries :) Keep doing what you're doing; I'm seeing a lot of helpful answers from you, and your upvoted answers far outweigh your downvoted ones. Don't let a single downvote bother you too much, but yeah, just be aware that they tend to be more common on these types of questions. Cheers! – Tim Lewis Dec 07 '20 at 16:35