1
votes

In many to many join I get this error:

Illuminate/Database/QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constrain t fails (dbtest.note_tag, CONSTRAINT note_tag_note_id_foreign FOREIGN KEY (note_id) REFERENCES tags (id) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into note_tag (note_id, tag_id) values (28, 1))'

NotesController:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Note extends Model
{
    protected $fillable = [
        'body',
    ];
    public function card()
    {
        return $this->belongsTo(Card::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function tags()
    {
        return $this->belongsToMany(Tag::class);
    }
}

TagsController:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{


    public function notes()
    {
        return $this->belongsToMany(Note::class);
    }
}

migration=> tag:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTagsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });

        Schema::create('note_tag', function (Blueprint $table) {

            $table->integer('note_id')->unsigned()->index();
            $table->foreign('note_id')->references('id')->on('tags')
                ->onDelete('cascade')->onUpdate('cascade');

            $table->integer('tag_id')->unsigned()->index();
            $table->foreign('tag_id')->references('id')->on('notes')
                ->onDelete('cascade')->onUpdate('cascade');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tags');
        Schema::dropIfExists('note_tag');
    }
}

in view:

<form action="/cards/{{$card->id}}/note" method="post">
        {{csrf_field()}}
        <textarea  name="body" title="body" placeholder="Please Enter Your Note....">{{old('body')}}</textarea>

        <select style="display: block; width: 300px;margin-top: 20px" name="tags[]" title="tags" multiple>

            @foreach($tags as $tag)

                <option value="{{$tag->id}}">{{$tag->name}}</option>

            @endforeach

        </select>

        <button type="submit">Add Note</button>
    </form>

in Controller:

public function store(Card $card,Request $request)
    {

        //dd($request->input('tags'));

        $this->validate($request,[
            'body' => 'required|min:6'
        ]);

        $note = new Note($request->all());
        $note->user_id = 1;
        $card->notes()->save($note);

        $tagsId = $request->input('tags');
        $note->tags()->attach($tagsId);

        //\Session::flash('flash_message','Your note has been created.');
        //$request->session()->flash('flash_message','Your note has been created.');
        flash('note created successfully');

        return back();
    }
1
Please do not vandalize your question by removing the content. I'll roll it back. - Modus Tollens

1 Answers

0
votes

You are trying to pass a tag of as a note in your foreign keys.

$table->foreign('tag_id')->references('id')->on('notes')
    ->onDelete('cascade')->onUpdate('cascade');

Should be ->on(tags)

$table->foreign('tag_id')->references('id')->on('tags')
    ->onDelete('cascade')->onUpdate('cascade');