I´m creating a CRUD with vue in laravel using laragon, but I have an error creating storage routes for images, I already create the symbolic link in the public file and instead of getting the images stored in that file, in my database the routes that are being created are temporal files like this: C:WindowsTempphp574E.tmp and the error that appears in the browser is "path cannot be empty", I don´t know if maybe the configuration of laragon in my device is a little messed up or the coding has errors. Hope you can help me solving this error.
`<?php
namespace AppHttpControllers;
use AppModelsConcert;
use AppModelsArtist;
use InertiaInertia;
use IlluminateHttpRequest;
use IlluminateSupportFacadesStorage;
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');
}
}
2
Answers
Dont call for the temporary file twice.
You are updating data before removing the image so when you trying to remove image the path will be empty so that is causing error.
Check this code: