控制器
在这里我删除了旧的图像,然后添加一个新的图像,我已经按照代码行在互联网上,但我觉得有点不对劲
public function update(Request $request, Article $article)
{
$request->validate([
"title" => "required|string|max:255",
"body" => "required",
"category_id" => "required",
"is_active" => "required",
]);
if ($request->file("image") == "") {
$article->update([
"title" => $request->title,
"slug" => Str::slug($request->title),
"body" => $request->body,
"category_id" => $request->category_id,
"user_id" => Auth::id(),
"is_active" => $request->is_active,
"views" => 0,
]);
} else {
Storage::disk("local")->delete("public/article/" . $article->image);
$image = $request->file("image");
$image->storeAs("public/article", $image->hashName());
$article->update([
"title" => $request->title,
"slug" => Str::slug($request->title),
"body" => $request->body,
"category_id" => $request->category_id,
"user_id" => Auth::id(),
"image" => $image->hashName(),
"is_active" => $request->is_active,
"views" => 0,
]);
}
return redirect(route("article.index"))->with("success", "Data Updated Successfully");
}
视图
我修改了这行来自Laravel的训练营页面的代码。
import React from "react";
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
import InputError from "@/Components/InputError";
import PrimaryButton from "@/Components/PrimaryButton";
import { useForm, Head } from "@inertiajs/inertia-react";
export default function Edit({ auth, categories, article }) {
const { data, setData, put, processing, reset, errors } = useForm({
title: article.title,
body: article.body,
category_id: article.category_id,
image: null,
is_active: article.is_active,
});
const submit = (e) => {
e.preventDefault();
put(route("article.update", article.id)), { onSuccess: () => reset() };
};
return (
<AuthenticatedLayout
auth={auth}
header={
<h2 className="font-semibold text-xl text-gray-800 leading-tight">
Article Edit: {article.title}
</h2>
}
>
<Head title="Article Edit" />
<div className="px-20 py-10">
<form onSubmit={submit}>
<label htmlFor="" className="font-semibold text-lg">
Title
</label>
<input
name="title"
type="text"
value={data.title}
className="px-3 py-1 mt-1 mb-5 block w-full border-gray-300 focus:border-slate-900 focus:ring-slate-900 rounded-md shadow-sm"
onChange={(e) => setData("title", e.target.value)}
/>
<label htmlFor="" className="font-semibold text-lg">
Body
</label>
<textarea
name="body"
id=""
rows="10"
className="block mt-1 mb-5 rounded-md shadow-sm border-gray-300 focus:border-slate-900 focus:ring-slate-900 w-full"
value={data.body}
onChange={(e) => setData("body", e.target.value)}
></textarea>
<label htmlFor="" className="font-semibold text-lg">
Category
</label>
<select
name="category_id"
id=""
className="block mt-1 mb-5 rounded-md shadow-sm border-gray-300 focus:border-slate-900 focus:ring-slate-900 w-full"
onChange={(e) => setData("category_id", e.target.value)}
>
{categories.map((category) =>
category.id == article.category_id ? (
<option value={category.id} selected>
{category.category_name}
</option>
) : (
<option value={category.id}>
{category.category_name}
</option>
)
)}
</select>
<label htmlFor="" className="font-semibold text-lg">
Image
</label>
<input
onChange={(e) => setData("image", e.target.files[0])}
type="file"
name=""
id=""
className="block mt-1 mb-1 w-full text-sm text-slate-500
file:mr-4 file:py-2 file:px-4
file:rounded-md file:border-0
file:text-sm file:font-semibold
file:bg-gray-500 file:text-slate-50 file:cursor-pointer
"
/>
<img
src={"/storage/article/" + article.image}
alt=""
className="w-52 mb-5"
/>
<label htmlFor="" className="font-semibold text-lg">
Status
</label>
<select
name="is_active"
id=""
className="block mt-1 mb-5 rounded-md shadow-sm border-gray-300 focus:border-slate-900 focus:ring-slate-900 w-full"
onChange={(e) => setData("is_active", e.target.value)}
>
{article.is_active == 1 ? (
<>
<option value={1} selected>
Publish
</option>
<option value={2}>Draf</option>
</>
) : (
<>
<option value={1}>Publish</option>
<option value={2} selected>
Draf
</option>
</>
)}
</select>
<InputError message={errors.title} className="mt-2" />
<InputError message={errors.body} className="mt-2" />
<InputError message={errors.category_id} className="mt-2" />
<InputError message={errors.image} className="mt-2" />
<InputError message={errors.is_active} className="mt-2" />
<PrimaryButton className="mt-4" processing={processing}>
Save
</PrimaryButton>
</form>
</div>
</AuthenticatedLayout>
);
}
1条答案
按热度按时间mkshixfv1#
根据Inertia Docs
使用multipart/form-data请求上载文件在某些语言中不支持put、patch或delete方法。解决方法是使用post上载文件。
因此,在发送更新时,您可以使用
post()
,然后添加包含适当请求方法的新属性,而不是使用put()
。