mardi 5 septembre 2017

Laravel Polymorphic (tags) SQL error, the unknown column issue

trying to set up a Many to Many Polymorphic relationship for users and teams and struggling with an error that others have experienced on SO and the web but I've not been able to solve my problem from their experiences. So going back to basics with a fresh project using an example from Many To Many Polymorphic Relations and Laravel 5.4

Their example works perfectly except when I try to set up a M2M relationship with App\User instead of App\Video or App\Post.

When I run my project using http://localhost:8888/manytomany/public/create

I get this Error:

(2/2) QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'field list' (SQL: insert into `taggables` (`tag_id`, `taggable_type`, `user_id`) values (1, App\Team, 1))
in Connection.php (line 647)
at Connection->runQueryCallback('insert into `taggables` (`tag_id`, `taggable_type`, `user_id`) values (?, ?, ?)', array(1, 'App\\Team', 1), object(Closure))

The only difference I see is that User Extends Authenticatable whereas all the other Models extend Eloquent\Model.

Clues most welcome. Many thanks Kevin

App\Post

use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
    protected $fillable=['name'];
    public function tags(){
        return $this->morphToMany('App\Post','taggable');
    }
}

App\Video

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
    //
    public function tags(){
        return $this->morphToMany('App\Video','taggable');
    }
    protected $fillable=['name'];
}

App\User

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    public function tags(){
        return $this->morphToMany('App\User','taggable');
    }
}

App\Tag

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

    class Tag extends Model
    {
        //

        protected $fillable=['name'];
    }

App\Taggable

  class Taggable extends Model
    {
        //
    }

Routes: web.php

/*
|--------------------------------------------------------------------------
| Web Routes
|------------------------------------------------------------------------
*/ 


use App\Post;
use App\Tag;
use App\Video;
use App\User;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/create',function(){
   $post  = Post::create(['name'=>'My First Post']);
   $video = Video::create(['name'=>'My First Video']);
   $user = User::create(['name'=>'Kevin','email'=>'example@gmail.com','password'=>'1234']);
   $tag = Tag::find(1);
   $post->tags()->save($tag);
   $user->tags()->save($tag);
});

Migrations Videos (same structure for Posts & Tags)

class CreatePostsTable extends Migration
{

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }


}

Users...

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

Taggables

   public function up()
    {
        Schema::create('taggables', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('tag_id');
            $table->integer('taggable_id');
            $table->string('taggable_type');

            $table->timestamps();
        });
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire