N
N
noName72022-03-22 18:13:56
Laravel
noName7, 2022-03-22 18:13:56

Edit and add buttons not working in admin panel?

Photos are added from the database, when you click on the "edit" button, the "successfully updated" window pops up, but nothing changes.
Controller:

public function edit($id)
    {
        //
        $gallery = gallery::find($id);
        // $gallery=gallery::orderBy('created_at','desc')->get();
        return view('admin.table.gallery.edit',[
            'gallery'=>$gallery
        ]);
    }
public function update(Request $request, gallery $gallery)
    {
        //
        $gallery->photo = "img/galery/" . $_FILES['imgRed']['name']; //файл
        if (!empty($_FILES)) {
        //перемещение
        move_uploaded_file(
        $_FILES['imgRed'] ['tmp_name'],
        'img/galery/' . $_FILES['imgRed']['name']
        );
    }
        // // если не меняем картнку, то оставлять имя
        if ($_FILES['imgRed']['name']=="") {
        $gallery->photo = $request->imgHidden;
        }
        $gallery->save();
        return redirect()->back()->withSuccess('Статья была успешно обновлена!');
    }

index:
@foreach($gallery as $gal)
     <tr>  
         <td>{{$gal['id']}}</td>  
         <td><img src="/{{$gal['photo']}}" alt="картинка" srcset"" style="width:30%;"></td>
                            
          <td class="project-actions text-right">
          <a class="btn btn-info btn-sm" href="{{route('gallery.edit',$gal['id'])}}">
            <i class="fas fa-pencil-alt"></i>
                                Редактировать
                              </a>
           <form action="" method="POST"    style="display: inline-block">
             @csrf
             @method('DELETE')
                  <button type="submit" class="btn btn-danger btn-sm delete-btn">
                   <i class="fas fa-trash"></i>
                        Удалить
                   </button>
               </form>
               </td>
               </tr>
   @endforeach

Also "add" button doesn't work either, error: Undefined index: img
<form action="{{route('gallery.store')}}" method="POST">
                    @method('POST')
                   @csrf
                <div class="form-group">
                    <label for="img">Изображение статьи</label>
                    <input type="file" name="img" class="form-control">
                </div>
                <!-- /.card-body -->
                <div class="card-footer">
                  <button type="submit" class="btn btn-primary">Добавить</button>
                </div>
              </form>

public function create()
    {
        return view('admin.table.gallery.create');
    }
public function store(Request $request)
    {
        //добавляем к имени файла адрес папки для БД
        $gallery->photo = "img/galery/" . $_FILES['img']['name'];
        ////переносим файл из временной папки сервера в папку проекта
        if (!empty($_FILES)) {
        move_uploaded_file(
            $_FILES['img']['tmp_name'],
            'img/galery/' . $_FILES['img']['name']
        );
    }
        $post->save();
        return Redirect()->back()->withSuccess("Фото было успешно добавлено!");
    }

Where is the mistake? Tables with text work, but pictures don't.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nokimaro, 2022-03-22
@noName7

1. to upload files, the form must have a parameterenctype="multipart/form-data"

<form action="{{route('gallery.store')}}" method="post" enctype="multipart/form-data">

2. for some reason, in one code there is "img" in the other "imgRed"
3. study the documentation on how to correctly and safely work with uploading files in laravel, so as not to drag $_FILES into the code
https://laravel.com/docs /9.x/filesystem#file-uploads

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question