'Undefined variable: photos (View: C:\wamp64\www\couture\resources\views\upload.blade.php)

i want to fix the undifined variable $photos upload.blade.php

@extends('layouts.app')
@section('content')
        <h1>voir les modeles</h1>
            <form action="/upload" method="POST" enctype="multipart/form-data">
                @csrf
                <input type="file" name="image">
                <input type="submit" name="Upload">
            </form>
        </hr>
        <ul>
            @foreach ($photos as $photo)
            <li> {{ $photo->name }}<img src="{{ asset('storage/images/', $photo->name) }}"></li>

            @endforeach
        </ul>
@endsection

here is the controler photoenter code here

PhotoController

 <?php

namespace App\Http\Controllers;

use App\Models\Photo;
use Illuminate\Http\Request;

class PhotoController extends Controller
{
    public function create()
    {
        $photos = Photo::all();
        return view('upload', compact('photos'));
    }
    public function store(Request $request)
    {

        // dd($request->file());
        $size = $request->file('image')->getSize();
        $name = $request->file('image')->getClientOriginalName();

        $request->file('image')->storeAs('public/images/', $name);
        $photo = new Photo();
        $photo->name = $name;
        $photo->size = $size;
        $photo->save();

        return redirect()->back();
     }
}






enter code here


Solution 1:[1]

public function create()
{
    $photos = Photo::all();
    return view('upload', ['photo =>$photos'] );

}

should be,

return view('upload', ['photos' => $photos]);

Notice the incorrect placement of the single quote and the singular use of photo.

Also, please ask an actual question and don't just dump the error into the title.

Solution 2:[2]

Change create function code.

public function create()
{
    $photos = Photo::all();
    return view('upload', ['photos' => $photos] );
}

Check documentation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 waterloomatt
Solution 2 OSahin