2016-06-17 46 views
0

我有一個非常簡單的控制器和視圖。但由於某種原因,我無法將一個變量傳遞給我的觀點。請注意,路由器內部的echo $project->title工作。laravel 5.2無法通過變量來查看

控制器:

<?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> 

但由於某些原因,我不斷收到此錯誤:

ErrorException in b605b028d5285b79f4e0043cf14415de5dddcae6.php line 10: Undefined variable: project (View: C:\xampp\htdocs\webcast_module\resources\views\home.blade.php)

in b605b028d5285b79f4e0043cf14415de5dddcae6.php line 10 at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44 at PhpEngine->evaluatePath('C:\xampp\htdocs\webcast_module\storage\framework\views/b605b028d5285b79f4e0043cf14415de5dddcae6.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'id' => '1', 'title' => 'eerste project', 'description' => 'merijn')) in CompilerEngine.php line 59 at CompilerEngine->get('C:\xampp\htdocs\webcast_module\resources\views/home.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'id' => '1', 'title' => 'eerste project', 'description' => 'merijn')) in View.php line 149 at View->getContents() in View.php line 120 at View->renderContents() in View.php line 85 at View->render() in Response.php line 53 at Response->setContent(object(View)) in Response.php line 201 at Response->__construct(object(View)) in Router.php line 1085 at Router->prepareResponse(object(Request), object(View)) in ControllerDispatcher.php line 95 at ControllerDispatcher->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 at Pipeline->Illuminate\Routing{closure}(object(Request)) in Authenticate.php line 28 at Authenticate->handle(object(Request), object(Closure)) at call_user_func_array(array(object(Authenticate), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96 at ControllerDispatcher->callWithinStack(object(HomeController), object(Route), object(Request), 'index') in ControllerDispatcher.php line 54 at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\HomeController', 'index') in Route.php line 174 at Route->runController(object(Request)) in Route.php line 140 at Route->run(object(Request)) in Router.php line 724 at Router->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 at Pipeline->Illuminate\Routing{closure}(object(Request)) in VerifyCsrfToken.php line 64 at VerifyCsrfToken->handle(object(Request), object(Closure)) at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) in ShareErrorsFromSession.php line 49 at ShareErrorsFromSession->handle(object(Request), object(Closure)) at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) in StartSession.php line 62 at StartSession->handle(object(Request), object(Closure)) at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37 at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) in EncryptCookies.php line 59 at EncryptCookies->handle(object(Request), object(Closure)) at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 at Pipeline->then(object(Closure)) in Router.php line 726 at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699 at Router->dispatchToRoute(object(Request)) in Router.php line 675 at Router->dispatch(object(Request)) in Kernel.php line 246 at Kernel->Illuminate\Foundation\Http{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 at Pipeline->Illuminate\Routing{closure}(object(Request)) in CheckForMaintenanceMode.php line 44 at CheckForMaintenanceMode->handle(object(Request), object(Closure)) at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 at Pipeline->then(object(Closure)) in Kernel.php line 132 at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99 at Kernel->handle(object(Request)) in index.php line 54

+1

可你試圖返回查看( '家',[ '項目'= > $ project]); https://laravel.com/docs/5.2/views#passing-data-to-views –

+2

你可以使用** compact()**。返回視圖('家',緊湊('項目'));希望它會有所幫助。 :) :) – Sakil

+0

什麼會更好? –

回答

6

發送到視圖中的數據需要成爲一個聯合陣列。用這個代替:

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

噢男人在啞巴。非常感謝大聲笑! –

5

取代:

return view('home', $project); 

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

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