Home > Back-end >  Undefined variable: photos (View: C:\wamp64\www\couture\resources\views\upload.blade.php)
Undefined variable: photos (View: C:\wamp64\www\couture\resources\views\upload.blade.php)

Time:04-04

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

CodePudding user response:

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.

CodePudding user response:

Change create function code.

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

Check documentation.

  • Related