laravel 5.2无法传递变量来查看

时间:2016-06-17 13:52:36

标签: php laravel laravel-5.2

我有一个非常简单的控制器和视图。但由于某种原因,我无法将变量传递给我的观点。请注意,路由器内的echo $project->title可以正常工作。

控制器:

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use Auth;
class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $project = \App\Projects::find(1);
        return view('home', $project);
    }

}

查看:

        <div class="panel-body">
            You are logged in!
            {{ $project->title }}
        </div>

但由于某种原因,我不断收到此错误:

  

b605b028d5285b79f4e0043cf14415de5dddcae6.php行中的ErrorException   10:未定义的变量:project(查看:   C:\ XAMPP \ htdocs中\ webcast_module \资源\视图\ home.blade.php)

     

在b605b028d5285b79f4e0043cf14415de5dddcae6.php第10行   CompilerEngine-&gt; handleViewException(object(ErrorException),&#39; 1&#39;)in   PhpEngine.php第44行   PhpEngine-&GT; evaluatePath(&#39; C:\ XAMPP \ htdocs中\ webcast_module \存储\框架\视图/ b605b028d5285b79f4e0043cf14415de5dddcae6.php&#39 ;,   数组(&#39; __ env&#39; =&gt;对象(工厂),&#39; app&#39; =&gt;对象(应用程序),   &#39;错误&#39; =&GT;对象(ViewErrorBag),&#39; id&#39; =&GT; &#39; 1&#39;,&#39; title&#39; =&GT; &#39; eerste   项目&#39;,&#39;描述&#39; =&GT; &#39; merijn&#39;))在CompilerEngine.php第59行at   CompilerEngine-&gt; get(&#39; C:\ xampp \ htdocs \ webcast_module \ resources \ views / home.blade.php&#39;,array(&#39; __ env&#39; =&gt; object(Factory), &#39; app&#39; =&gt;对象(应用),   &#39;错误&#39; =&GT;对象(ViewErrorBag),&#39; id&#39; =&GT; &#39; 1&#39;,&#39; title&#39; =&GT; &#39; eerste   项目&#39;,&#39;描述&#39; =&GT; &#39; merijn&#39;))在View.php第149行中   View-&gt; getContents()在View.php第120行的View-&gt; renderContents()中   View.php第85行在View-&gt; render()中的Response.php第53行at   响应 - &gt; setContent(object(View))在Response.php第201行中   响应 - &gt; __构造(对象(视图))在Router.php第1085行中   Router-&gt; prepareResponse(object(Request),object(View))in   ControllerDispatcher.php第95行   ControllerDispatcher-&gt; Illuminate \ Routing {closure}(object(Request))at   Pipeline.php行中的call_user_func(object(Closure),object(Request))   52在Pipeline-&gt; Illuminate \ Routing {closure}(object(Request))in   在Authenticate-&gt;句柄中验证.php第28行(对象(请求),   对象(闭包))在call_user_func_array(array(object(Authenticate),   在Pipeline.php中处理&#39;),数组(对象(Request),对象(Closure)))   Pipeline的第136行 - &gt; Illuminate \ Pipeline {closure}(object(Request))   在Pipeline.php中的call_user_func(object(Closure),object(Request))   管道上的第32行 - &gt; Illuminate \ Routing {closure}(object(Request))at   Pipeline.php行中的call_user_func(object(Closure),object(Request))   103在Pipeline-&gt;然后(对象(Closure))在ControllerDispatcher.php中   96号线   ControllerDispatcher-&GT; callWithinStack(对象(HomeController中),   ControllerDispatcher.php中的object(Route),object(Request)和&#39; index&#39;)   在ControllerDispatcher-> dispatch(对象(路线),第54行,   对象(请求),&#39; App \ Http \ Controllers \ HomeController&#39;,&#39; index&#39;)in   在Route-&gt; runController(对象(请求))中的Route.php第174行   Route.php第140行在Route-&gt;在Router.php行中运行(对象(请求))   724在Router-&gt; Illuminate \ Routing {closure}(object(Request))at   Pipeline.php行中的call_user_func(object(Closure),object(Request))   52在Pipeline-&gt; Illuminate \ Routing {closure}(object(Request))in   VerifyCsrfToken.php第64行at   VerifyCsrfToken-&gt;句柄(对象(Request),对象(Closure))at   call_user_func_array(array(object(VerifyCsrfToken),&#39; handle&#39;),   在Pipeline.php第136行中的array(object(Request),object(Closure)))   Pipeline-&gt; Illuminate \ Pipeline {closure}(object(Request))at   Pipeline.php行中的call_user_func(object(Closure),object(Request))   32在管道 - &gt;照亮\路由{闭合}(对象(请求))in   ShareErrorsFromSession.php第49行at   ShareErrorsFromSession-&gt; handle(object(Request),object(Closure))at   call_user_func_array(array(object(ShareErrorsFromSession),&#39; handle&#39;),   在Pipeline.php第136行中的array(object(Request),object(Closure)))   Pipeline-&gt; Illuminate \ Pipeline {closure}(object(Request))at   Pipeline.php行中的call_user_func(object(Closure),object(Request))   32在管道 - &gt;照亮\路由{闭合}(对象(请求))in   在StartSession-&gt;句柄的StartSession.php第62行(对象(请求),   对象(闭包))在call_user_func_array(array(object(StartSession),   在Pipeline.php中处理&#39;),数组(对象(Request),对象(Closure)))   Pipeline的第136行 - &gt; Illuminate \ Pipeline {closure}(object(Request))   在Pipeline.php中的call_user_func(object(Closure),object(Request))   管道上的第32行 - &gt; Illuminate \ Routing {closure}(object(Request))in   AddQueuedCookiesToResponse.php第37行   AddQueuedCookiesToResponse-&gt;句柄(对象(Request),对象(Closure))   at call_user_func_array(array(object(AddQueuedCookiesToResponse),   在Pipeline.php中处理&#39;),数组(对象(Request),对象(Closure)))   Pipeline的第136行 - &gt; Illuminate \ Pipeline {closure}(object(Request))   在Pipeline.php中的call_user_func(object(Closure),object(Request))   管道上的第32行 - &gt; Illuminate \ Routing {closure}(object(Request))in   EncryptCookies.php第59行在EncryptCookies-&gt;句柄(对象(请求),   对象(闭包))在call_user_func_array(array(object(EncryptCookies),   在Pipeline.php中处理&#39;),数组(对象(Request),对象(Closure)))   Pipeline的第136行 - &gt; Illuminate \ Pipeline {closure}(object(Request))   在Pipeline.php中的call_user_func(object(Closure),object(Request))   管道上的第32行 - &gt; Illuminate \ Routing {closure}(object(Request))at   Pipeline.php行中的call_user_func(object(Closure),object(Request))   103在Pipeline-&gt;然后(对象(Closure))在Router.php第726行中   Router-&gt; runRouteWithinStack(object(Route),object(Request))in   Router.php第699行在Router-&gt; dispatchToRoute(object(Request))中   Router.php第675行在Router-&gt;在Kernel.php中的dispatch(对象(Request))   第246行   Kernel-&gt; Illuminate \ Foundation \ Http {closure}(object(Request))at   Pipeline.php行中的call_user_func(object(Closure),object(Request))   52在Pipeline-&gt; Illuminate \ Routing {closure}(object(Request))in   CheckForMaintenanceMode.php第44行   CheckForMaintenanceMode-&gt; handle(object(Request),object(Closure))at   call_user_func_array(array(object(CheckForMaintenanceMode),&#39; handle&#39;),   在Pipeline.php第136行中的array(object(Request),object(Closure)))   Pipeline-&gt; Illuminate \ Pipeline {closure}(object(Request))at   Pipeline.php行中的call_user_func(object(Closure),object(Request))   32在管道 - &gt; Illuminate \ Routing {closure}(对象(请求))at   Pipeline.php行中的call_user_func(object(Closure),object(Request))   103在管道 - >然后(对象(Closure))在Kernel.php第132行at   Kernel.php行中的Kernel-&gt; sendRequestThroughRouter(object(Request))   在index.php第54行中的Kernel-&gt;句柄(对象(请求))中的99

2 个答案:

答案 0 :(得分:6)

发送到视图的数据需要是关联数组。请改用:

return view('home', ['project' => $project]);

答案 1 :(得分:5)

取代:

return view('home', $project);

return view('home', compact($project));

return view('home')->with('project', $project);