はじめに
はじめまして! webエンジニア転職を目指して三ヶ月が経ちました。 スクールでは共同開発が始まり、カート機能を実装しております。 そこで、終盤において商品追加機能を担当し、 復習&アウトプットも兼ねてご覧の皆さんに共有したいと思い、記事投稿に至ります! 至らぬ点が多々あるとは思いますが、何かの実装の参考になれば幸いです。😆0, 目次
1, 概要 2, 環境 3, 参考テーブル 4, 【null値許容】の設定 参考にしたサイト1, 概要
DB確認(Tinker)
本記事では、前回実装した商品データを元にDBへnull値も含めて追加していきたいと思います。 前回:[【Laravel】商品追加機能の実装について](https://qiita.com/nakasan773/items/d644e5675140632d852d) 今回:↓ ・ id:16 の商品を追加 ・ null設定3箇所2, 環境
macOS Big Sur 11.2.2 Apache 2.4.46 MySQL 5.7 PHP 7.2.34 Laravel 5.8 実は今回、共同開発において VirtualBoxを用いたVagrantによる仮想開発環境で統一したかったのですが、 投稿主のmacが最近出たばかりのM1チップが搭載されております。 そして、悲しいことに当時(2月中旬)はVirtualBoxなどバージョンが追いついておらず、 仕方なくAWS(Could9)での環境設定を余儀なくされました。。3, 参考テーブル
テーブルは事前に作ってあると思います。 今回の内容では、こちらのファイルの中身を変更する必要はありません。 カラム名など確認する際の参考として一応載せておきます。××××_××_××_××××××_create_m_products_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateMProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('m_products', function (Blueprint $table) {
$table->increments('id');
$table->string('product_name', 64);
$table->unsignedInteger('category_id');
$table->integer('price');
$table->string('description', 256);
$table->unsignedInteger('sale_status_id');
$table->unsignedInteger('product_status_id');
$table->timestamp('regist_date');
$table->unsignedInteger('user_id');
$table->char('delete_flag', 1)->default(0);
$table->foreign('user_id')->references('id')->on('m_users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('m_categories')->onDelete('cascade');
$table->foreign('sale_status_id')->references('id')->on('m_sale_statuses')->onDelete('cascade');
$table->foreign('product_status_id')->references('id')->on('m_product_statuses')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('m_products');
}
}
4, 【null値許容】の設定
$ make:migration --table (作ったテーブル名) change_(作ったテーブル名)_table
Created Migration: ××××_××_××_××××××_change_m_products_table
これでchangeテーブルが作成されました。
ファイルを開いたら、
▶︎upメソッド
▶︎downメソッド
それぞれにnull許容(変更)の設定をしていきます。
××××_××_××_××××××_change_m_products_table
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeMProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('m_products', function (Blueprint $table) {
// それぞれのカラムにNULLを許容
$table->unsignedInteger('category_id')->nullable()->change();
$table->unsignedInteger('sale_status_id')->nullable()->change();
$table->unsignedInteger('product_status_id')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('m_products', function (Blueprint $table) {
//カラム削除
$table->dropColumn(['category_id']);
$table->dropColumn(['sale_status_id']);
$table->dropColumn(['product_status_id']);
});
}
}
と、ここでエラーが出てくるかと思います。
こちらは別記事でまとめてます。
簡単に解決できますので、こちらを参考にしてみてください!↓
参考:composer.jsonのエラーについて
以上でnull値としてDBに登録できたかと思います!