cviebrock / eloquent-taggable

Easily add the ability to tag your Eloquent models in Laravel.
MIT License
559 stars 72 forks source link

Integrity constraint violation: 1048 Column 'taggable_id' cannot be null #49

Closed ajiehatajie closed 7 years ago

ajiehatajie commented 7 years ago

this issue Integrity constraint violation: 1048 Column 'taggable_id' cannot be null

but problem is solved if column taggable_id allow null in mysql

controller store

$requestData = $request->all();

$post = New Post($requestData);
$post->tag([$request->input('tag')]);

model

namespace App;

use DB;
use Carbon\Carbon;
use willvincent\Rateable\Rateable;
use Cviebrock\EloquentTaggable\Taggable;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
class Post extends Model
{
    use Rateable, Taggable, Sluggable;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'posts';

    /**
    * The database primary key value.
    *
    * @var string
    */
    protected $primaryKey = 'id';

thanks you

cviebrock commented 7 years ago

taggable_id can't be null because it represents a polymorphic relations table.

The issue for you is that you've created the new Post object but haven't saved it before tagging it. So the Post doesn't have an id yet that we can link in the morph table.

The easiest fix:

$requestData = $request->all();

$post = New Post($requestData);
$post->save();  // <--- ADD THIS
$post->tag([$request->input('tag')]);