2 years ago

Trying to insert Optional Value 'Image' to Server

Posted 2 years ago by seyidaniels

Hello guys, i am quite new to Laravel and was creating a social media website as practice where users update their status like the facebook thing but the issue am having is that its not every post that should have an image attached to it.

So when ever i send a post without an image it gives me this error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image' cannot be null (SQL: insert into stories (body, image, user_id, updated_at, created_at) values (My anme os Ololade, , 2, 2017-12-14 14:00:08, 2017-12-14 14:00:08))

Which shows that there is no value for the image. However, i updated the migration file and made the image nullable like this: $table->binary('image')->nullable(); It works and image upload becomes optional but on checking my sql database i do not get to see a blob file whenever i upload images as all values under the image column is null.

Please is there a way to override this error laravel gives me? That is: If there is no image, it should go ahead and update status or if i could pass a validation via my controller that the image data is not required.

here is my code:

public function up() { Schema::create('stories', function (Blueprint $table) { $table->increments('id');



//Migrations file

public function createStory(Request $request){ $image = $request['image']; $story = new Story(); $story->body = $request['body']; $story->image = $image; $request->user()->stories()->save($story); return response()->json(['message' => 'Your Story has been posted']); }

//The Function that creates a post

Please sign in or create an account to participate in this conversation.