cbil360's avatar

Issues with seed and pivot tables while working with API lesson

I am working on the incremental API laracast alongwith @JeffreyWay(the difference being Laravel4 and Laravel5) and got stuck at a point. Now the laracast video has a different implementation of faker class for seeding the tables. So I tried to follow the same lines and integrated the code in the current version class. The pivot class LessonTagTableSeeder

use App\Lesson;
use App\Tag;
use Illuminate\Database\Seeder;
use DB;

use Laracasts\TestDummy\Factory as TestDummy;

class LessonTagTableSeeder {
    public function run()
    {
        $faker =  Faker::create();
        $lessonIds = Lesson::lists('id');
        $tagIds = Tag::lists('id');
       // TestDummy::times(30)->create('App\Lesson');

        foreach(range(1,30) as $index)
        {
            DB::table('lesson_tag')->insert([
               'lesson_id'=>$faker->randomElement($lessonIds),
               'tag_id'=>$faker->randomElement($tagIds)

            ]);
        }

    }
} 

My DatabaseSeeder

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\Lesson;
use App\Tag;
use DB;

class DatabaseSeeder extends Seeder {
    public function run()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS=0');
        Lesson::truncate();
        Tag::truncate();
        DB::table('lesson_tag')->truncate();

        Model::unguard();

         $this->call('LessonsTableSeeder');
         $this->call('TagsTableSeeder');
         $this->call('LessonTagTableSeeder');

         DB::statement('SET FOREIGN_KEY_CHECKS=1');
    }
}

I get a [Symfony\Component\Debug\Exception\FatalErrorException] Class 'CreateLessonTagPivotTable' not found when I run php artisan migrate:refresh --seed

One More Issue I face is I do have the default user model and tablein my application but when I try to test a post request using postman I get VerifyCSRFtoken error. I dont understand why,as I have a user entered,what else do I need to change,I am on a fresh install.

0 likes
1 reply
rodrigo.pedra's avatar
Level 56

Regarding the csrf issue, read the docs [ http://laravel.com/docs/5.0/routing#csrf-protection ]:

You do not need to manually verify the CSRF token on POST, PUT, or DELETE requests. The VerifyCsrfToken HTTP middleware will verify token in the request input matches the token stored in the session.

So when you are running your POST request from postman you should send your csrf_token alogn with your request. You'll need to manually generate it and manually add it in postman. One other option, NOT RECOMMENDED is to "disable" the middleware while in development:

// app/Http/Middleware/VerifyCsrfToken.php

public function handle($request, Closure $next)
{
    if (  env('APP_ENV')  === 'local') {  // BAD APPROACH!!!
        return $next($request);
    }

    return parent::handle($request, $next);
}

Regarding the migration error, it seems you have renamed a migration file (in the database/migrations folder, not in the database/seeds one) after running a previous migration.

Migration are stored in the database by filename, and the convention is to look for a class' name that matches the filename.

If this is the case and as you are refreshing, go into MySQL, drop your database, create it again and run:

php artisan migrate --seed
1 like

Please or to participate in this conversation.