Я создал способ предварительного просмотра и удаления основного изображения статьи.
Код: Выделить всё
[i]image) }}" alt="{{ $article->title }}">
@if($article->image !== 'default.jpg')
[url=#]id}}" title="Remove image" onclick="removeImage(event)">
[/i]
[/url]
@endif
Код: Выделить всё
public function deleteImage($id, $fileName) {
$article = Article::find($id);
$article->image = "default.jpg";
$article->save();
if (File::exists(public_path('images/articles/' . $fileName))) {
File::delete(public_path('images/articles/' . $fileName));
}
}
public function edit($id) {
$article = Article::find($id);
$attached_tags = $article->tags()->get()->pluck('id')->toArray();
return view(
'dashboard/edit-article',
[
'categories' => $this->categories(),
'tags' => $this->tags(),
'attached_tags' => $attached_tags,
'article' => $article
]
);
}
public function update(Request $request, $id) {
$validator = Validator::make($request->all(), $this->rules, $this->messages);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator->errors())->withInput();
}
$fields = $validator->validated();
$article = Article::find($id);
// If a new image is uploaded, set it as the article image
// Otherwise, set the old image...
if (isset($request->image)) {
$imageName = md5(time()) . Auth::user()->id . '.' . $request->image->extension();
$request->image->move(public_path('images/articles'), $imageName);
} else {
$imageName = $article->image;
}
$article->title = $request->get('title');
$article->short_description = $request->get('short_description');
$article->category_id = $request->get('category_id');
$article->featured = $request->has('featured');
$article->image = $request->get('image') == 'default.jpg' ? 'default.jpg' : $imageName;
$article->content = $request->get('content');
// Save changes to the article
$article->save();
//Attach tags to article
if ($request->has('tags')) {
$article->tags()->sync($request->tags);
} else {
$article->tags()->sync([]);
}
return redirect()->route('dashboard.articles')->with('success', 'The article titled "' . $article->title . '" was updated');
}
< /code>
Скрипт: < /p>
function removeImage(event) {
var defaultImg = document.getElementById('defaultImage').value;
var input = document.getElementById('file');
var img = document.getElementById('imagePreview');
var imageContainer = img.parentNode.parentNode;
event.preventDefault();
if (event.currentTarget.classList.contains('edit')) {
var id = event.currentTarget.dataset.uid;
var fileName = img.getAttribute('src').split('/').reverse()[0];
var url = `${APP_URL}/dashboard/articles/delete-image/${id}/${fileName}`;
if (confirm('This action is irreversible. Are you sure?')) {
var CSRF_TOKEN = document.querySelectorAll('meta[name="csrf-token"]')[0].getAttribute('content');
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
img.src = defaultImg;
document.getElementById('delete-image').remove();
}
}
}
xmlhttp.open('POST', url, true);
xmlhttp.setRequestHeader("X-CSRF-TOKEN", CSRF_TOKEN);
xmlhttp.send();
}
} else {
imageContainer.classList.add('d-none');
img.src = "";
input.value = "";
}
}
Как я могу достичь желаемого результата?
Подробнее здесь: https://stackoverflow.com/questions/796 ... in-laravel
Мобильная версия