の列名をオーバーライドしていないは、私は、このテーブルを持っているモデル
Video
Category
私は照会しようとしています1つの特定のカテゴリに属するすべての動画を取得するために、多対多の関係。
は、私の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
でNO id
フィールドを持っていないので、私はあなたが移行に見ることができるようにvideos_id
を使用し、で、(オーバーライドされなかった、videos.id
列のように見えますモデル。私は明示的にモデルのキーを、またリレーションシップでオーバーライドしました。
ピボットテーブルにアルファベット順を使用するのが一般的であることはわかりませんでした...そして、ピボットテーブルはで、categories_videos
ではありません。関係。
私は何が間違っているのか、なぜlaravelを使っているのかわからないvideos
。 id
だから私はいくつかの助けが必要です。私は自分自身を書く前に他のQ/Aを探してみましたが、解決策は見つかりませんでした。
ここ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');
}
}
誰かが、複雑なクエリ内の異なるテーブルのあいまいでない複数の「id」フィールドを避けるために、キーに「 _id」という名前を付けることをお勧めしました。私には意味があり、これは私がしたことです。一方、 'id 'だけを使用する方がいい理由は分かりません。たぶんそれはラベールの外でのコンベンションではないかもしれません...私が間違っていれば私を修正してください –
Jeflopo
ああ、答えは正しいです。 $ primaryKeyを使って動作します:Pありがとうございます! – Jeflopo
私はそれが非常に便利だと理解していますが、 'category_title'フィールドがある場合に備えて、他のフィールドに' video_title'という名前を付けるべきでしょうか?最後に、すべてを読みやすくします。なぜなら、手動でSQLクエリを実行している場合、カラムに 'videos.id'または' categories.id'というテーブルを置くことができるからです。フレームワーク(そして一般にOOで言えば)では、あなたはあなたのフィールドにこのようにアクセスするので、意味がなくなります: '$ video-> video_id'、これは無駄な繰り返しです。それは結局、それは本当に問題ではないと私は信じています。 – AntoineB