2020.12.27
Laravel 中間テーブルはアルファベット順
顧客のテーブル(customers_table)とコースのテーブル(courses_table)を多対多のリレーションで作るときに
”base table or view not found 1146 table”と怒られたはなし。
目次
環境
laravel 5.5
laravel-admin 1.8.11
顧客テーブル
php artisan make:migration create_customers_table --create=customers
customers_table
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCustomersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('customer_firstname', 100);
$table->string('customer_lastname', 100);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('customers');
}
}
コーステーブル
php artisan make:migration create_courses_table --create=courses
courses_table
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCoursesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('courses', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('course_name', 100);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('courses');
}
}
顧客とコースの中間テーブル
×ダメなケースのテーブル customer_course
php artisan make:migration create_customer_course_table --create=customer_course
○良いケースのテーブル course_customer
php artisan make:migration create_course_customer_table --create=course_customer
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCourseCustomerTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('course_customer', function (Blueprint $table) {
$table->unsignedBigInteger('customer_id');
$table->unsignedBigInteger('course_id');
$table->primary(['customer_id', 'course_id']);
//外部キー制約
$table->foreign('customer_id')->references('id')->on('customers')->onDelete('cascade');
$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('course_customer');
}
}
ModelとController
App/Models/Customer
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
public function courses() {
return $this->belongsToMany('App\Models\Course');
}
}
App/Models/Course
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Course extends Model
{
customers() {
return $this->belongsToMany('App\Models\Customer');
}
}
App/Admin/CustomerController.php
<?php
namespace App\Admin\Controllers;
use App\Models\Customer;
use App\Models\Course;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
class CustomerController extends AdminController
{
中略
protected function form()
{
$form = new Form(new Customer());
$form->text('customer_lastname', __('苗字'))->required(true);
$form->text('customer_firstname', __('名前'))->required(true);
//コース
$form->multipleSelect('Courses','コース')->options(Course::all()->pluck('course_name','id'));
return $form;
}
}