我正在使用 laragon 在 laravel 中使用 vue 创建 CRUD,但是在为图像创建存储路由时出现错误,我已经在公共文件中创建了符号链接,而不是将图像存储在该文件中,而是在我的数据库中正在创建的路由是这样的临时文件:C:\Windows\Temp\php574E.tmp,浏览器中出现的错误是“路径不能为空”,我不知道我的 laragon 的配置是否可能是这样设备有点混乱或者编码有错误。希望你能帮我解决这个错误。
`<?php
namespace App\Http\Controllers;
use App\Models\Concert;
use App\Models\Artist;
use Inertia\Inertia;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class ConcertController extends Controller
{
public function index()
{
return Inertia::render('Concerts/Index',[
'concerts' => Concert::paginate(10)
]);
}
public function create()
{
return Inertia::render('Concerts/Create',[
'artists' => Artist::all()
]);
}
public function store(Request $request)
{
$request->validate([
'name' => 'required|max:30',
'description' => 'required|max:200',
'date' => 'required|date',
'duration' => 'required|date_format:H:i:s',
'image' => 'required|file|mimes:png,jpg,gif',
'artists' => 'required|array'
]);
$concert = Concert::create($request->all());
if ($request->hasFile('image')) {
$imgName = microtime(true) . '.' . $request->file('image')->getClientOriginalExtension();
$request->file('image')->storeAs('public/storage/img', $imgName); //here is the error
$concert->image = '/img/' . $imgName; // Cambiado para que sea idéntico al código de libros
$concert->save();
}
$concert->artists()->sync($request->artists);
return redirect('concerts/create')->with('success', 'Concert created');
}
public function show(Concert $concert)
{
return Inertia::render('Concerts/Show',[
'concert' => $concert, 'artists' => $concert->artists
]);
}
public function edit(Concert $concert)
{
return Inertia::render('Concerts/Edit',[
'artists' => Artist::all(),
'concert' => $concert,
'artistsOfConcert' => $concert->artists
]);
}
public function updateConcert(Request $request, Concert $concert)
{
$request->validate([
'name' => 'required|max:30',
'description' => 'required|max:200',
'date' => 'required|date',
'duration' => 'required|date_format:H:i:s',
'id' => 'required|numeric',
]);
$concert = Concert::find($request->id);
$concert->update($request->input());
if ($request->hasFile('image')) {
Storage::disk('public')->delete($concert->image);
$imgName = microtime(true) . '.' . $request->file('image')->getClientOriginalExtension();
$request->file('image')->storeAs('public/storage/img', $imgName);
$concert->image = '/img/' . $imgName; // Cambiado para que sea idéntico al código de libros
$concert->save();
}
$concert->artists()->sync($request->artists);
return redirect('concerts')->with('success', 'Concert updated');
}
public function destroy(Concert $concert)
{
$concert->delete();
return redirect('concerts')->with('success','Concert deleted');
}
}
不要两次调用临时文件。
if ($request->hasFile('image')) {
$file = $request->file('image');
$imgName = microtime(true) . '.' . $file->getClientOriginalExtension();
$file->storeAs('public/storage/img', $imgName);
$concert->image = '/img/' . $imgName;
$concert->save();
}