-1

I need some help with this...I'm making a function for tags with a tutorial, but in my database ( post_tag ), my post_id isn't saved, only tag_id and id.In my posts table, after create a post I receive the id of post, but here, in my post_tag isn't.Do you know why guys...?

My controller

public function create(){

        $tags = Tag::all();

        return view('posts.create')->withTags($tags);
    }
public function store(Request $request )
    {



        $data = request()->validate([

            'caption' => 'required|max:255',
            'image' => 'required|image',
        ]);
        $post = new Post;

        $post->tags()->sync($request->tags, false);



        $imagePath = request('image')->store('uploads', 'public');

        $image = Image::make(public_path("storage/{$imagePath}"))->fit(1600, 1100);
        $image->save();

        auth()->user()->posts()->create([
            'caption' => $data['caption'],
            'image' => $imagePath,
        ]);



         return redirect('/profile/' . auth()->user()->id);
    }

My create_post_tag_table

  public function up()
    {
        Schema::create('post_tag', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('post_id')->unsigned();
            $table->foreign('post_id')->references('id')->on('posts');

            $table->integer('tag_id')->unsigned();
            $table->foreign('tag_id')->references('id')->on('tags');

        });
    }

My create_posts_table

  public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->string('caption');
            $table->string('image');
            $table->timestamps();


            $table->index('user_id');
        });
    }

Andrei Nagy
  • 241
  • 2
  • 11
  • I'm pretty sure you have a problem in the model of those tables. Since you are not using Laravel's convention for naming tables, you have to specific the foreign keys. – César Escudero Jul 15 '19 at 20:40
  • 1
    Maybe should I share Post and Tag models? – Andrei Nagy Jul 15 '19 at 20:54
  • I answered something yesterday that sounds very similar. Please take a look here: https://stackoverflow.com/questions/57031847/general-error-1364-field-doesnt-have-a-default-value/57031950#57031950 – Watercayman Jul 15 '19 at 20:57

3 Answers3

1

You have created a new Post object, but you have not saved it yet in this line:

$post = new Post;

Thus, in this line, immediately following:

$post->tags()->sync($request->tags, false);

There is no id in the database for this post as yet (nor will there be an id on the Post model), and thus sync will fail every time because it can't find the id.

After you new up the Post object, save() it and then you can sync.


On a different note that may help in other ways, your post model has a big int as its id:

Schema::create('posts', function (Blueprint $table) {    
    $table->bigIncrements('id');
}

But your post_tag table is only an integer. This mismatch may cause some issues. Suggest changing to match:

Schema::create('post_tag', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('post_id')->unsigned(); // <--- suggest change this to bigInteger
        $table->foreign('post_id')->references('id')->on('posts');
Watercayman
  • 7,970
  • 10
  • 31
  • 49
0

I may be wrong, but since you don't have the onUpdate or onDelete properties in your table associated with your foreign keys, it could be the problem.

0

The correct solution was this:

$post = auth()->user()->posts()->create([
        'caption' => $data['caption'],
        'image' => $imagePath,
    ]);

I just added this to my code, and all was ok.

Andrei Nagy
  • 241
  • 2
  • 11