Many to many laravel tidak berjalan tabel quote_tag tidak terisi

Halo! saya menggunakan laravel 5.8, kenapa many to many tidak berjalan? seperti di playlist laravel quote saya membuat tabel quote_tag untuk hubungan quote dengan tagnya. Tetapi ketika data diinsert tabel quote_tag tadi tidak terisi

Quote Model:

<?php

namespace App\Models;

use Auth;
use Illuminate\Database\Eloquent\Model;

class Quote extends Model
{
    public function tags()
    {
        return $this->belongsToMany('App\Models\Tag');
    }
}

Tag Model :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    public function quotes()
    {
        return $this->belongsToMany('App\Models\Quote');
    }
}

QuoteController :

<?php

namespace App\Http\Controllers;

use Auth;
use App\Models\User;
use App\Models\Quote;
use App\Models\Tag;
use Illuminate\Http\Request;

class QuoteController extends Controller
{
 public function store(Request $request)
    {
        $slug = str_slug($request->title, '-');
        // check slug
        if (Quote::where('slug', $slug)->first() != null) {
            $slug = $slug . '-' . time();
        }
        // validation
        $this->validate($request, [
            'title' => 'required|min:5',
            'content' => 'required|min:5'
        ]);
        $request->tags = array_diff($request->tags, [0]);
        if (empty($request->tags)) {
            return redirect('quotes/create')->withInput($request->input())->with('tag_error', 'Tag tidak boleh kosong');
        }
        // Mass Assignment
        $quote = Quote::create([
            'title' => $request->title,
            'slug' => $slug,
            'content' => $request->content,
            'user_id' => Auth::user()->id
        ]);
        $quote->tags()->attach($request->tag);
        return redirect('quotes')->with('message', 'Data kutipan berhasil ditambahkan!');
    }
}

Migartion quote_tag :

<?php

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

class CreateQuoteTagTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('quote_tag', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('quote_id')->unsigned();
            $table->integer('tag_id')->unsigned();
            $table->timestamps();
            // Foreign Key
            $table->foreign('quote_id')->references('id')->on('quotes');
            $table->foreign('tag_id')->references('id')->on('tags');
        });
    }

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

avatar mmuqiit
@mmuqiit

32 Kontribusi 5 Poin

Dipost 5 tahun yang lalu

1 Jawaban:

Jawaban Terpilih

Eh sudah solved, hanya kurang s nya saja:((

avatar mmuqiit
@mmuqiit

32 Kontribusi 5 Poin

Dipost 5 tahun yang lalu

Login untuk ikut Jawaban