<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Article;
use Inertia\Inertia;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class ArticleController extends Controller
{
public function index(Request $request)
{
$data = Article::when($request->term, function($query, $term){
$query->where('title', 'LIKE', '%'.$term.'%');
})->orderBy('id', 'desc')->paginate(5);
return Inertia::render('Admin/Article', ['data' => $data]);
}
public function store(Request $request)
{
Article::create($request->all());
Log::channel('admin')->info('Администратор '. Auth::user()->name. ' добавил статью: '. $request->title);
return redirect()->back();
}
public function update(Request $request)
{
if ($request->has('id')) {
Article::find($request->input('id'))->update($request->all());
return redirect()->back();
}
}
public function destroy(Request $request)
{
if ($request->has('id')) {
Article::find($request->input('id'))->delete();
Log::channel('admin')->info('Администратор '. Auth::user()->name. ' удалил статью: '. $request->title);
return redirect()->back();
}
}
}
<script>
import { defineComponent } from "vue"
import AdminLayout from "@/Layouts/AdminLayout.vue"
import { Head, Link } from '@inertiajs/inertia-vue3'
import Pagination from "@/Pages/Pagination.vue"
export default {
props: {
data: Object,
},
data() {
return {
isActive: false,
addActive: false,
editMode: false,
isAlert: false,
textAlert: {
text: null,
},
term: '',
form: {
title: null,
description: null,
text: null,
image_path: null,
},
}
},
components: {
AdminLayout, Head, Link, Pagination,
},
methods: {
openModal: function () {
this.isActive = true;
},
Alert: function (param) {
this.textAlert = {
text: param,
}
this.isAlert = true;
setTimeout(() => this.isAlert = false, 3000);
},
closeModal: function () {
this.isActive = false;
this.reset();
this.editMode=false;
},
reset: function () {
this.form = {
title: null,
description: null,
text: null,
image_path: null,
}
},
save: function (data) {
this.$inertia.post('/admin/article', data)
this.reset();
this.closeModal();
this.editMode = false;
this.Alert('Статья успешно добавлена!');
},
edit: function (data) {
this.form = Object.assign({}, data);
this.editMode = true;
this.openModal();
},
update: function (data) {
if (!confirm('Sure')) return;
data._method = 'PUT';
this.$inertia.post('/admin/article/' + data.id, data)
this.reset();
this.closeModal();
this.Alert('Статья успешно изменена!');
},
deleteRow: function (data) {
if (!confirm('Sure')) return;
data._method = 'DELETE';
this.$inertia.post('/admin/article/' + data.id, data)
this.reset();
this.closeModal();
},
search() {
this.$inertia.replace(this.route('article.index', {term: this.term}))
}
}
}
</script>
Сразу хочу чтобы Вы понимали что я прикрепил код не для того чтобы мне сделали уже готовое решение, просто иногда возникает момент что нужно видеть как устроено сейчас у меня все.
<template>
<form @submit.prevent="submit">
<input type="text" v-model="form.name" />
<input type="file" @input="form.avatar = $event.target.files[0]" />
<progress v-if="form.progress" :value="form.progress.percentage" max="100">
{{ form.progress.percentage }}%
</progress>
<button type="submit">Submit</button>
</form>
</template>
<script>
import { useForm } from '@inertiajs/inertia-vue3'
export default {
setup () {
const form = useForm({
name: null,
avatar: null,
})
function submit() {
form.post('/users')
}
return { form, submit }
},
}
</script>