Answer the question
In order to leave comments, you need to log in
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('Статья была успешно обновлена!');
}
@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
<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("Фото было успешно добавлено!");
}
Answer the question
In order to leave comments, you need to log in
1. to upload files, the form must have a parameterenctype="multipart/form-data"
<form action="{{route('gallery.store')}}" method="post" enctype="multipart/form-data">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question