A
A
Andrej Kopp2021-04-07 16:55:07
MySQL
Andrej Kopp, 2021-04-07 16:55:07

How to upload an image to a Laravel CRUD model and store the result in a database?

When creating an entry or editing it, a temporary path is written in the field of the table called image instead of the file name. I have a function like this:

public function update(Request $request, Round $round)
    {
        $request->validate([
            'name' => 'required'
        ]);

        $image = $request->file('image');
        $extension = $image->extension();
        $imageName = $image->getClientOriginalName();
        $image->move(storage_path('app/public/images/battles/battle'.$request->battle_id.'/round'.$request->rnum), $imageName);

        $request->merge([
            'image' => $imageName,
            'status' => $request->has('status') ? true : false
        ]);

        dd($request->all());

        $round->update($request->all());

        return redirect()->back()
            ->with('success', 'Запись обновлена.');
    }

It can be seen from it that I am trying to merge the array with the status section, this field with the checkbox works fine, I thought that by the example I could add the image name to the array for updating, but something went wrong.

dd prints out an array like this:
array:15 [▼
  "_token" => "RDfCfKC368hhGem8R8XwJjqE9jllPZGmYaREfTPc"
  "_method" => "PUT"
  "name" => "Без сожалений"
  "start" => "2021-04-07T01:54"
  "end" => "2021-04-07T01:54"
  "battle_id" => "2"
  "rnum" => "4"
  "title" => "tryrt"
  "htitle" => "trytry"
  "description" => "yrtyrt"
  "keywords" => "rtytry"
  "introtext" => "<p>rtytry</p>"
  "content" => "<p>trytryt</p>"
  "status" => true
  "image" => Illuminate\Http\UploadedFile {#1450 ▼
    -test: false
    -originalName: "logo (2).jpg"
    -mimeType: "image/jpeg"
    -error: 0
    #hashName: null
    path: "/var/www/bezbflava.ru/data/mod-tmp"
    filename: "phpavmPLE"
    basename: "phpavmPLE"
    pathname: "/var/www/bezbflava.ru/data/mod-tmp/phpavmPLE"
    extension: ""
    realPath: "/var/www/bezbflava.ru/data/mod-tmp/phpavmPLE"
    aTime: 2021-04-07 13:40:30
    mTime: 2021-04-07 13:40:30
    cTime: 2021-04-07 13:40:30
    inode: 3155818
    size: 151782
    perms: 0100600
    owner: 510
    group: 510
    writable: false
    readable: false
    executable: false
    file: true
    dir: false
    link: false
  }
]

In the array, a nested array is displayed near the image section, and the pathname section is stored in the base instead of the image. How can I write the name of the uploaded file to the database? I would be very grateful for help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrej Kopp, 2021-04-07
@sequelone

It was decided this way.

/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Round  $round
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Round $round)
    {
        $request->validate([
            'name' => 'required'
        ]);

        $request->merge([
            'status' => $request->has('status') ? true : false
        ]);

        $round->update($request->all());

        if($request->has('image')) {

            $image = $request->file('image');
            $extension = $image->extension();
            $imageName = $image->getClientOriginalName();
            $image->move(storage_path('app/public/images/battles/battle'.$request->battle_id.'/round'.$request->rnum), $imageName);
            $round->image = $imageName;

        }

        $round->save();

        return redirect()->back()
            ->with('success', 'Запись обновлена.');
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question