laravel 5.4视图中未定义的变量

时间:2017-06-06 11:40:06

标签: laravel controller laravel-5.4 laravel-views laravel-controller

我对laravel很新,并试图从数据库中提取数据但是仍然遇到一个错误页面,说明视图中有一个未定义的变量。

我想在控制器中有一些我做错了(或没有做错)。所以我有一个 create.blade.php ,它将表单中的数据发布到数据库中,一切正常,但是我希望它能在发布到数据库后重定向到 show。 blade.php 显示上传的数据,这里是我用错误消息打砖墙的地方。

这是我在控制器中的商店功能 -

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);
}

这是同一个控制器中的show函数 -

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

我得到的错误信息是 -

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

在show.blade.php中,这是我所拥有的样本 -

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

说了这么多,我还尝试了在控制器的show函数中返回视图的各种方法,但所有这些都导致上面的相同错误消息。这些变体包括 -

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

更新:我之前有错别字,但现在我得到的错误是 -

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)

这是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

4 个答案:

答案 0 :(得分:3)

您的姓名不匹配。

import threading

class CalculationThread(threading Tread):
    def __init__(self, range):
        self.range = range
        # use this copy instead for the thread
        self.objects_dict = dict(objects_dict)
    def run(self):
        for i in self.range:
            # your code

threads = []
threadCount = 8

lastThreadStart = num_objects
tLen = num_objects // threadCount
for i in reversed(range(threadCount)):
    threadStart = i*tLen
    t = CalculationThread(range(threadStart, lastThreadStart))
    lastThreadStart = threadStart

# wait for all to finish
for t in threads: t.join()

# code to union all objects_dicts comes here

在这里你应该使用:

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

正如你所说的那样<h1>{{ $name->h4Ttle }}</h1>

在您定义name的其他选项中,您在视图中使用Data4page(小写d)。

答案 1 :(得分:3)

更改了此

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

要: -

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

希望它能够结束!

答案 2 :(得分:0)

如果下面给出的答案绝对正确,为什么你要使用错误的案例输入变量名? PHP变量区分大小写。您已将$Data4page传递给您所显示的代码中的视图,然后尝试使用访问$data4page。以下任何一个都可以。

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'));

答案 3 :(得分:0)

您需要遍历集合以获取值

@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