Back

Undefined variable: entry - Passing data from Controller to View


Luke Frost posted 4 months ago

At the moment I am trying to pass a value of a variable that is $entry from my EntryController to the view that allows me to view the entry depending on the person who is logged in. The database should only retrieve the entry of the user who is logged in. When I run the page, this error appears:

Undefined variable: entry

Here is part of the entry blade file (viewentry.blade.php):


      <div class="row">
          @foreach ($entry as $entries)

      <div class="col-xs-12 col-sm-12 col-md-12">
          <div class="form-group">
              <strong>Image:</strong>
              {{ $entries->image }}
          </div>
      </div>

      <div class="col-xs-12 col-sm-12 col-md-12">
          <div class="form-group">
              <strong>Title:</strong>
              {{ $entries->title }}
          </div>
        </div>

        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Description:</strong>
                {{ $entries->description }}
            </div>
          </div>
        @endforeach
```

The method to display the entry in the controller (EntryController.php):

```
  public function showUpload(Entries $entry)
  {
    $entry = Entries::with('entrys')->where('user_id', '=', Auth::user()->id)->get();
    return view('viewentry', compact('entry'));
  }
```

I'm not sure why this error is coming up as I can't see anything wrong with the code above. Has anyone had this problem before?, If so how can it be solved?
Naveedanwar123 replied 4 months ago

Why are you passing Entries object to this method?

Hisham Haniffa replied 4 months ago

In ur controller ur passing entry and in the blade file ur trying to loop through variable called entries hence it fails to identify the variable entry. Below is the updated controller method.

public function showUpload(Entries $entry)
{
  $entries = Entries::with('entrys')->where('user_id', '=', Auth::user()->id)->get();
  return view('viewentry', compact('entries'));
}
Luke Frost replied 4 months ago

I updated the controller code to what you suggested but it still isn't working as it should.


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us