you have not said what is wrong?
Hello guys I have a problem linking images in Laravel 10 controller
When I save the images in the path, they are saved as required, which is this path: project\storage\app\public\images.
In the index controller, I return to the blade table 'ajax yajura' with the product data. When I call these images in the controller, it takes me to this path: project\public\storage\images.
I've tried everything and it didn't work for me. my controller code
class SparePartController extends Controller { /** * Display a listing of the resource. */ use UploadImage ;
const imgPath= 'public/images/spare_parts/'; // img save path
public function index(Request $request)
{
if ($request->ajax()) {
$data = spare_part::select('*');
return Datatables::of($data)
->addIndexColumn()
->addColumn('img', function ($device) {
$imgUrl = Storage::url('public/images/spare_parts/' . $device->img); // <-- here the problem
// Check if the image exists
if ($device->img != 'no img') {
return '<img src="' . $imgUrl . '" alt="img error" style="max-width:70px">';
} else {
return 'no img';
}
})
->rawColumns(['action','status','img','created_at'])
->make(true);
}
$spare_part = spare_part::all();
return view('dashboard.spare_parts.index',compact('spare_part'));
}
public function store(Request $request)
{
$validated = $request->validate([
'img'=>'nullable|image|mimes:jpeg,png,jpg|max:2048',
]);
$req = $validated;
if ($request->file('img')){
$fileName = $this->uploadIm2(request(),'img','public/images/spare_parts/' );
$req['img'] = $fileName;
}else{
$fileName='no img';
$req['img'] = $fileName;
}
spare_part::create($req);
return redirect()->route('dashboard.spare_parts')->with('success', 'تم الاضافه بنجاح');
}
and here uploadIm2 function if you need
function uploadIm2(Request $request, string $requestKey, string $path): string {
// Get the uploaded image
$image = $request->file($requestKey);
// Generate a unique filename with extension
$filename = uniqid('', true) . '.' . $image->getClientOriginalExtension();
// Store the image in the path
$image->storePubliclyAs($path, $filename);
// Return the image name
return $filename;
}
I try this
php artisan storage:link
and try all this in controller
$imgUrl = asset('./storage/images/spare_parts/' . $device->img); $imgUrl = Storage::url('images/spare_parts/' . $device->img); $imgUrl = Storage::disk('public')->url('images/spare_parts/' . $device->img); $imgUrl = storage_path('public/images/spare_parts/' . $device->img); and
Also, the problem hasn't been resolved. It's calling the images from a different path, which is this path: project\public\storage\images. Even when I try to save the images in this path, it doesn't accept.
Thank you all for the help, but the problem was somewhat strange and not repeated. After trying many times, I found that solving the problem is as follows: In config/filesystems.php: 'disks' => [
'local' => [
'driver' => 'local',
'root' => public_path(), //The change has been made from storage_path('app') to public_path()
'throw' => false,
],
Please or to participate in this conversation.