0

I'm kind of new to laravel and trying to pull data from a database but keep hitting an error page saying there's an undefined variable within the view.

I'm supposing there's something I'm doing wrong (or not doing) within the controller. So I have a create.blade.php that posts data from a form to the database and everything works fine, however I want it such that after posting to the database to redirect to the show.blade.php showing the uploaded data and here is where I hit a brick wall with the error messages.

This is my store function within the controller -

public function store(Request $request)
    {
     $data4page = new Data4page;

     $data4page->h1Ttle = $request->h1Ttle;
     $data4page->h4Ttle = $request->h4Ttle;
     $data4page->description = $request->description;
     $data4page->save();

     /*redirecting to the content's show page*/
     return redirect()->route('content.show', $data4page->id);
}

And this is the show function within the same controller -

public function show($id)
    {
        //
        $data4page = Data4page::find($id);
        return view('digital.show')->with(compact('data4page'));
    }

The error message I'm getting is -

Undefined variable: data4page (View: /var/www/html/theproject/resources/views/content/show.blade.php)

And in the show.blade.php this is a sample of what I have -

<h1>{{ $data4page->h4Ttle }}</h1>
<p>{{ $data4page->description }}</p>
<h4>{{ $data4page->h4Ttle }}</h4>

Having said all that, I have also tried various means of returning the view within the show function of the controller but all these result in the same error message above. These variants include -

return view('content.show')->with('data4page', $data4page);
return view('content.show',['data4page'=>$data4page]);

Update: I earlier had typos but now the error I get is -

undefined variable: {"id":9,"h1Ttle":"h1 sample text","h4Ttle":"h4 sample text","description":"body text goes here"} (View: /var/www/html/theproject/resources/views/content/show.blade.‌​‌​php)

This is the show.blade.php -

@extends('main')
@section('title')
@section('content')
      <div class="app-template">
         <main class="wrapperMain">
            <div class="">
               <aside class="navSidebar">
                  <nav class="navProperty">
                     <h1 class="navProperty-title">{{ $data4page->h1Ttle }}</h1>
                  </nav>
                  <footer class="footerProperty">
                     <div class="upcomingSchedule">
                        <h4 class="upcomingSchedule-heading">{{ $data4page->h4Ttle }}</h4>
                     </div>
                  </footer>
               </aside>
            </div>
            <div class="content content--withSidebar">
               <div class="lead--Home">
                  <p>{{ $data4page->description }}</p>
               </div>
            </div>
         </main>
@endsection
Clint_A
  • 518
  • 2
  • 11
  • 35

4 Answers4

3

You have a name mismatch.

return view('content.show')->with(['name' => $data4page]);

Here you should use:

<h1>{{ $name->h4Ttle }}</h1>

As you called it name

In your other options you defined Data4page and in your view you use data4page (lower case d).

Robert
  • 5,703
  • 2
  • 31
  • 32
  • I just replaced the $data4page with $name as you've said within the view and now the error message says undefined variable: {"id":9,"h1Ttle":"sample text","h4Ttle":"h4 sample text","description":"body text goes here"} (View: /var/www/html/theproject/resources/views/content/show.blade.php) – Clint_A Jun 06 '17 at 11:57
  • Can you update your question with the code you have now? The error looks like a typo, perhaps a double `$` in your view? – Robert Jun 06 '17 at 12:18
  • @Clint_A Can you check the laravel log? It will show the cached view and the line in that file it fails on. Check that and you'll find exactly where the error occurs in the view. – Robert Jun 06 '17 at 12:36
  • I've updated the code. You were right, the error was due to a typo. But now the returned view is blank! – Clint_A Jun 06 '17 at 12:36
  • Blank as in nothing? Or blank as in some html in the source but no data?Check debug settings and laravel.log – Robert Jun 06 '17 at 12:37
  • Sorry, I'm back to this error - undefined variable: {"id":9,"h1Ttle":"sample text","h4Ttle":"h4 sample text","description":"body text goes here"} (View: /var/www/html/theproject/resources/views/content/show.blade.‌​php) – Clint_A Jun 06 '17 at 12:42
  • Again, check laravel.log and the cached template file it references to. Which line is in the view is responsible for this? Also you do a {{ dd($data4page); }} in the view to check the object there. – Robert Jun 06 '17 at 13:20
  • your previous suggestion was actually the solution, there was a double $ that had evaded sight all along. You could post it as an answer so I can accept it. Thanks. – Clint_A Jun 07 '17 at 04:31
3

Changed this

return view('content.show')->with(['name' => $data4page]);

To:-

return view('content.show')->with(compact('data4page'));

Hope it hepls!

kunal
  • 4,122
  • 12
  • 40
  • 75
  • It still gives the same error - undefined variable: {"id":9,"h1Ttle":"sample text","h4Ttle":"h4 sample text","description":"body text goes here"} (View: /var/www/html/theproject/resources/views/content/show.blade.‌​php) – Clint_A Jun 06 '17 at 12:07
  • print this line in blade echo "
    "; print_r($data4page); die;
    – kunal Jun 06 '17 at 12:11
  • It exists. It gives the same error for every other id I try. – Clint_A Jun 06 '17 at 12:15
  • "; print_r($data4page); die; ?> returns the specific id's (9) database information. The data exists. – Clint_A Jun 06 '17 at 12:20
0

Why have you resorted to typing the variable name with the wrong case when the answers given below are absolutely fine? PHP variables are case sensitive. You're passing $Data4page to your view in the code you've shown and then trying to use the access $data4page. Any of the below will work.

return view('content.show', ['data4page' => $data4page]);
return view('content.show', compact('data4page'));
return view('content.show')->with('data4page', $data4page);
return view('content.show')->with(['data4page' => $data4page]);
return view('content.show')->with(compact('data4page'));
Sandeesh
  • 11,486
  • 3
  • 31
  • 42
  • At first it was a typing error, but having updated the code it now returns a blank page but without errors! – Clint_A Jun 06 '17 at 12:34
  • @Clint_A post your full view code. Check if the redirecting route `content.show` is valid and if it's being called by putting a `dd` in there. Also run `php artisan route:clear` and `php artisan view:clear` to clear the cached routes and views. – Sandeesh Jun 06 '17 at 12:39
0

You need to loop through the collection to get the value

@section('content')
     <div class="app-template">
        <main class="wrapperMain">
           @foreach ($data4page as $d4p)
           <div class="">
              <aside class="navSidebar">
                 <nav class="navProperty">
                    <h1 class="navProperty-title">{{ $d4p->h1Ttle }}</h1>
                 </nav>
                 <footer class="footerProperty">
                    <div class="upcomingSchedule">
                       <h4 class="upcomingSchedule-heading">{{ $d4p->h4Ttle }}</h4>
                    </div>
                 </footer>
              </aside>
           </div>
           <div class="content content--withSidebar">
              <div class="lead--Home">
                 <p>{{ $d4p->description }}</p>
              </div>
           </div>
           @endforeach
        </main>
@endsection
linktoahref
  • 7,812
  • 3
  • 29
  • 51
  • That's exactly what he's doing in the original code. You don't really need to specify the route parameter name when you have just one parameter. – Sandeesh Jun 06 '17 at 12:46
  • Oh, okay I wasn't aware! I thought the second parameter expects an array – linktoahref Jun 06 '17 at 12:47