Skip to content
Advertisement

laravel 5.2 cant pass variable to view

I have a very simple controller and view. But for some reason I am unable to pass a variable to my view. Note that echo $project->title inside the router works.

Controller:

<?php

namespace AppHttpControllers;

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

    /**
     * Show the application dashboard.
     *
     * @return IlluminateHttpResponse
     */
    public function index()
    {
        $project = AppProjects::find(1);
        return view('home', $project);
    }

}

View:

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

But for some reason I keep getting this error:

ErrorException in b605b028d5285b79f4e0043cf14415de5dddcae6.php line 10: Undefined variable: project (View: C:xampphtdocswebcast_moduleresourcesviewshome.blade.php)

in b605b028d5285b79f4e0043cf14415de5dddcae6.php line 10 at CompilerEngine->handleViewException(object(ErrorException), ‘1’) in PhpEngine.php line 44 at PhpEngine->evaluatePath(‘C:xampphtdocswebcast_modulestorageframeworkviews/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:xampphtdocswebcast_moduleresourcesviews/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->IlluminateRouting{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 at Pipeline->IlluminateRouting{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->IlluminatePipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->IlluminateRouting{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), ‘AppHttpControllersHomeController’, ‘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->IlluminateRouting{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 at Pipeline->IlluminateRouting{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->IlluminatePipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->IlluminateRouting{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->IlluminatePipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->IlluminateRouting{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->IlluminatePipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->IlluminateRouting{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->IlluminatePipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->IlluminateRouting{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->IlluminatePipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->IlluminateRouting{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->IlluminateFoundationHttp{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 at Pipeline->IlluminateRouting{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->IlluminatePipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->IlluminateRouting{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

Advertisement

Answer

The data sent to the view needs to be an associative array. Use this instead:

return view('home', ['project' => $project]);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement