Home > Mobile >  Storing and display image in blade file
Storing and display image in blade file

Time:04-19

I am trying to send photos and store them from PhotoController as below:

public function create()
{
    //
    $photos = Photo::all();
    return view('upload', compact('photos'));
}
public function store(Request $request)
{
    //
    $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();
}

And I try to display them in an upload blade file under view like this:

    <form method="POST" action="upload" enctype="multipart/form-data">
        @csrf
        <input type="file" name="image">
        <input type="submit" name="Upload">
    </form>
    </hr>
    <ul>
        @foreach (photos as photo)
            <li>
                <p>
                    {{ $photo->name }}
                </p>
                <img src="{{ asset('storage/images/'. $photo->name) }}">
            </li>
        @endforeach
    </ul>

Laravel shows me the following error at the foreach line:

syntax error, unexpected token ")", expecting "->" or "?->" or "{" or "["

CodePudding user response:

was just looking at your view on the foreach part

try changing this line

@foreach (photos as photo)

to this

@foreach ($photos as $photo)

CodePudding user response:

I think you miss the $ in the blade foreach directive.

    @foreach ($photos as $photo)
        <li>
            <p>
                {{ $photo->name }}
            </p>
            <img src="{{ asset('storage/images/'. $photo->name) }}">
        </li>
    @endforeach
  • Related