Why are images not loading

5 days ago 9
ARTICLE AD BOX

Im using laravel and my images wont load.
I don't know if it has something to do with my PostController:

<?php namespace App\Http\Controllers; use App\Models\Post; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; class PostController extends Controller { /** * Display a listing of the resource. */ public function index() { $posts = Post::all(); return view('posts.gallery', compact('posts')); } /** * Show the form for creating a new resource. */ // public function create() //{ // return view('posts.create'); // } /** * Store a newly created resource in storage. */ public function store(Request $request) { $validated = $request->validate([ 'title' => 'required', 'description' => 'required', 'image' => 'nullable|image|max:2048' ]); if ($request->hasFile('image')) { $validated['image'] = $request->file('image')->store('images', 'public'); } Post::create($validated); return redirect()->route('posts.index')->with('success','Post created successfully!'); } /** * get the latest post */ public function home() { $latestpost = Post::latest()->first(); return view('home', ['latestpost' => $latestpost]); } /** * Display the specified resource. */ public function show(Post $post) { // } /** * Show the form for editing the specified resource. */ public function edit($id) { $post = Post::findOrFail($id); return view('posts.edit', compact('post')); } /** * Update the specified resource in storage. */ public function update(Request $request, $id) { $validated = $request->validate([ 'title' => 'required', 'description' => 'required', ]); $post = Post::findOrFail($id); // find the post $post->update($validated); //update post w new data return redirect()->route('posts.gallery')->with('success','Post updated successfully!'); } /** * Remove the specified resource from storage. */ public function destroy($id) { $post = Post::findOrFail($id); // find the post $post->delete(); //update post w new data return redirect()->route('posts.gallery')->with('success','Post deleted successfully!'); } }

I want to display the posts here:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h1> all posts</h1> <a href="{{ route('posts.create') }}"> create new post</a> @foreach($posts as $post) <h2>{{ $post->title }}</h2> <p>{{ $post->description }}</p> @if($post->image) <img src="{{ asset($post->image) }}" alt="{{ ($post->title) }}"> @endif <p>{{ $post->created_at }}</p> @endforeach <a href="{{ route('home') }}">gohome</a> </body> </html>

And the code works for everything but the images. The images don't show up no matter what, it just shows the alt text. When I click on the image icon and hit inspect though, it shows the correct URL.
Ive already tried deleting the storage folder, and remaking it with php artisan storage:lin. When i run php artisan storage:link it says "link already exists", but if the link is good why dont any of the pictures show up?

heres my web.php file if there is some kind of route issue:

<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\LoginController; use App\Http\Controllers\PostController; Route::get('/welcome', function () { return view('welcome'); })->name('login'); Route::get('/', [PostController::class, 'home'])->name('home'); Route::post('login', LoginController::class)->name('login.attempt'); Route::view('dashboard', 'dashboard')->name('dashboard'); Route::resource('posts', PostController::class);
Read Entire Article