Hello everyone,
I have a database table set with these informations:
Schema::create('dishes', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->string('name');
$table->tinyInteger('quantity')->default('1');
$table->string('ingredients')->nullable();
$table->boolean('available');
$table->decimal('price', 5, 2);
$table->text('description')->nullable();
$table->unsignedBigInteger('dishcategory_id')->nullable();
$table->foreign('dishcategory_id')->references('id')->on('dishcategories')->onDelete('set null');
$table->string('image')->nullable();
$table->timestamps();
});
And I have a landing page showing all the dishes. I also have a dishcontroller that uses all the functions. When I try to upload a file from local storage the database table seeds correctly. If I try to leave the image section empty (which I should be able to because the value is nullable) I get an error.
Dishcontroller.php
public function store(Request $request)
{
$data = $request->all();
$currentUserId = Auth::id();
$new_dish = new Dish();
if (Str::startsWith($data['image'], 'dish_images')) {
if (array_key_exists('image', $data)) {
$image_url = Storage::put('dish_images', $data['image']);
$data['image'] = $image_url;
}
}
$new_dish->fill($data);
$new_dish->user_id = $currentUserId;
$new_dish->image = $data['image'];
$new_dish->save();
return redirect()->route('admin.dishes.show', $new_dish)->with('message-create', "$new_dish->name");
}
public function update(Request $request, Dish $dish)
{
$data = $request->all();
// dd($data);
if (array_key_exists('image', $data)) {
if ($dish->image != null) Storage::delete($dish->image);
$image_url = Storage::put('dish_images', $data['image']);
$data['image'] = $image_url;
$dish->image = $data['image'];
}
$dish->update($data);
return redirect()->route('admin.dishes.show', $dish)->with('message-update', "$dish->name");
}
Error:
