Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer nullable column in laravel

Tags:

sql

php

laravel

Is it possible to have integer column nullabale in laravel?

I have table image where i store multiple image and get id of my posts products website_banner and I need them to be nullable. For example if I upload multiple image for my product not get error of my posts column have not default value!

my migrations:

Schema::create('images', function (Blueprint $table) {
            $table->increments('id');
            $table->string('image');
            $table->integer('post_id')->unsigned();
            $table->integer('product_id')->unsigned();
            $table->integer('banner_id')->unsigned();
            $table->timestamps();
        });

Schema::table('images', function($table) {
            $table->foreign('post_id')->references('id')->on('posts');
            $table->foreign('product_id')->references('id')->on('products');
            $table->foreign('banner_id')->references('id')->on('banners');
        });
like image 918
mafortis Avatar asked Dec 24 '22 12:12

mafortis


1 Answers

You must call nullable(), like so:

$table->integer('banner_id')->nullable()->unsigned();
like image 77
Summer Su Avatar answered Jan 02 '23 11:01

Summer Su