簡體   English   中英

多對多Laravel 5.3不會覆蓋模型的列名

[英]Many to many Laravel 5.3 is not overriding column name for a model

我有這張桌子:

在此處輸入圖片說明

我有這個模型:

Video 
Category

我正在嘗試查詢多對多關系,以獲取屬於一個特定類別的所有視頻。

我有一個路由domain.dev/category/{category}指向我的CategoryController及其各自的視圖。 我通過$request收到它。

我試圖從控制器中的模型中獲取數據(用php回顯)並將變量傳遞給視圖並執行以下操作:

@foreach ($category->videos as $video) {
    {{ $video->title }}
}
@endforeach

但是,當Laravel執行查詢時,它將引發異常,並顯示查詢:

QueryException in Connection.php line 769:
    SQLSTATE[42S22]: Column not found: 1054 Unknown column 'videos.id' in 'on clause' (SQL: select `videos`.*, `videos_categories`.`category_id` as `pivot_category_id`, `videos_categories`.`video_id` as `pivot_video_id` from `videos` inner join `videos_categories` on `videos`.`id` = `videos_categories`.`video_id` where `videos_categories`.`category_id` is null)

它看起來像videos.id列,不重寫(因為我沒有id在現場videos中,我使用videos_id ,你可以在遷移看見,在模型中,我已經明確地重寫鑰匙放在了車型,還在關系中。

我不知道這是一個常見的做法是用字母順序排列的數據透視表... ...我的數據透視表是videos_categories而不是categories_videos ,因為它應該“按照慣例”,所以我也重寫的關系表名。

我不知道自己在做什么錯,或者不知道為什么要使用videos id所以我需要一些幫助。 在編寫我自己的文檔之前,我還嘗試過搜索其他問題,但沒有找到任何解決方案。

這是CategoryController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Category;

class CategoryController extends Controller
{
    /**
     * Shows the Category page
     *
     * @return Response
     */
    public function index(Request $request)
    {
        $category = Category::where('category', $request->category)->first();
        // THINGS I TRIED
        /*
        $videos = $category->videos()->get();

        foreach ($category->videos as $video) {
            echo $video->title;
        }
        */

        return view('category.index', ['category' => $category]);
    }
}

移居

<?php

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

class CreateVideosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('videos', function (Blueprint $table) {
            $table->engine = 'InnoDB';

            $table->increments('video_id')->unique();
            $table->string('title', 255);
            $table->string('embed_code', 255);
            $table->timestamps();
        });
    }

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

<?php

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

class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->engine = 'InnoDB';

            $table->increments('category_id')->unique();
            $table->string('category', 45);
            $table->string('thumbnail');
        });
    }

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

數據透視表的遷移

<?php

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

class CreateVideosCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('videos_categories', function (Blueprint $table) {
            $table->engine = 'InnoDB';

            $table->integer('video_id')->unsigned()->nullable();
            $table->integer('category_id')->unsigned()->nullable();

            $table->foreign('video_id')
                ->references('video_id')
                ->on('videos')
                ->onUpdate('cascade')
                ->onDelete('cascade');

            $table->foreign('category_id')
                ->references('category_id')
                ->on('categories')
                ->onUpdate('cascade')
                ->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('videos_categories', function (Blueprint $table) {
            $table->dropForeign(['video_id', 'category_id']);
            $table->dropColumn('video_id');
            $table->dropColumn('category_id');
        });
        Schema::dropIfExists('videos_categories');
    }
}

楷模

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
    protected $table = 'videos';
    protected $primarykey = 'video_id';

    public function categories() {
        return $this->belongsToMany('App\Category', 'videos_categories', 'video_id', 'category_id');
    }
    public function tags() {
        return $this->belongsToMany('App\Tag', 'videos_tags', 'video_id', 'tag_id');
    }
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = 'categories';
    protected $primarykey = 'category_id';
    public $timestamps = false;

    public function videos()
    {
        return $this->belongsToMany('App\Video', 'videos_categories', 'category_id', 'video_id');
    }
}

在您的模型中,該屬性必須是$primaryKey而不是$primarykey $primaryKey

順便說一句,默認值是id而不是model_id是有原因的,這是因為這是命名列的標准方式,我建議不要在沒有充分理由的情況下違背此類約定,因為您面臨的挑戰,還因為它可以簡化與其他開發人員的最終協作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM