JustAPC's avatar

Cannot insert string into a table

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:

0 likes
7 replies
MohamedTammam's avatar

You need a path for the file, and also make sure it's a file.

if ($request->hasFile('image')) {
        if ($dish->image != null) Storage::delete($dish->image);
		
		$image_url = $request->image->store('dish_images');
		$data['image'] = $image_url;
        $dish->image = $data['image'];		
}
JustAPC's avatar

@MohamedTammam isn't this the same as saying array_key_exists? because if I upload an image the result will be:

if I don't, this is the result:

MohamedTammam's avatar

@JustAPC TBH, I don't know what's the difference between interacting with the $request object itself and extracting the the data using all() but I fore sure request object has a lot of methods including store, file, has etc. That why I preferer to deal with directly whenever it's possible.

JustAPC's avatar

@MohamedTammam it kinda worked, but i had an input type file and an input type text. I wanted to pass either a file, or a url or nothing to the DB. If I want to pass the file or no url it works. But if I use the input text, the string doesn't get uploaded to the DB, even if with a dd($data) the field is filled.

MohamedTammam's avatar

@JustAPC If your first problem got fixed, please close the discussion and open a new one for the new problem with more specific details.

DominiqueBosch's avatar
failed to open stream: Permission denied

This error usually means the directory you are trying to put the file in does not exists, or your application does not have the right permissions to write to that folder, this can happen when you've deployed to a server and the folder only has writing permissions for a certain user or group.

Please make sure you double-check if the directory you are trying to save the image in exists.

Depending on your needs you may want to check if your folder exists, and if not, create it:

 if(!Storage::exists('/path/to/your/directory')) {

    Storage::makeDirectory('/path/to/create/your/directory', 0775, true); //creates directory

}
JustAPC's avatar

@DominiqueBosch it does cause if I upload an actual image file, it will properly be uploaded in the db. But if I want to upload a URL image to the same DB field, it will output me that error.

Please or to participate in this conversation.