Charles
Charles

Reputation: 31

Laravel Undefined variable: Name

hello I'm trying to pass the $Name to the view and i cant seem to get it to work and the video i followed line for line didn't seem to work any help would be great appreciated.

Edit. I know this is Novice level but its giving the most trouble in my project right now.

Controller

    <?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

abstract class RequestController extends BaseController
{
    public function GetName(){
        $name = ['one','two','three'];
        return view::make('home', ['name' => $name]);
    }
} 

routes.php

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('home', 'RequestController@GetName');

View

@foreach( $name as $names)
<tr>
<th>{{ $names }}</th>
</tr>
@endForeach

Error

ErrorException in d223bf579fc7c74c7e9ee61a6d17702e line 399:
Undefined variable: name (View: D:\Xampp\htdocs\blog\resources\views\home.blade.php)
in d223bf579fc7c74c7e9ee61a6d17702e line 399
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44
at PhpEngine->evaluatePath('D:\Xampp\htdocs\blog\storage\framework\views/d223bf579fc7c74c7e9ee61a6d17702e', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag))) in CompilerEngine.php line 58
at CompilerEngine->get('D:\Xampp\htdocs\blog\resources\views/home.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag))) in View.php line 135
at View->getContents() in View.php line 106
at View->renderContents() in View.php line 80
at View->render() in Response.php line 51
at Response->setContent(object(View)) in Response.php line 198
at Response->__construct(object(View)) in Router.php line 1229
at Router->prepareResponse(object(Request), object(View)) in ControllerDispatcher.php line 113
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Authenticate.php line 45
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 124
at Pipeline->Illuminate\Pipeline\{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 114
at ControllerDispatcher->callWithinStack(object(HomeController), object(Route), object(Request), 'index') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch(object(Route), object(Request), '\Bestmomo\Scafold\Http\Controllers\HomeController', 'index') in Route.php line 203
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 708
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{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 710
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 675
at Router->dispatchToRoute(object(Request)) in Router.php line 635
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
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 124
at Pipeline->Illuminate\Pipeline\{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 124
at Pipeline->Illuminate\Pipeline\{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 124
at Pipeline->Illuminate\Pipeline\{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 124
at Pipeline->Illuminate\Pipeline\{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 124
at Pipeline->Illuminate\Pipeline\{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 124
at Pipeline->Illuminate\Pipeline\{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 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54

Upvotes: 1

Views: 4859

Answers (3)

Yusup Bugacheev
Yusup Bugacheev

Reputation: 511

view('home', compact('name'));

Upvotes: 0

wui
wui

Reputation: 410

you can try:

        return View::make( 'home' ,compact('name')); 

or

        return View::make('home', ['name' => $name]);

also add at the top:

        use \View as View; 

Upvotes: 0

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

In your Controller

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

And in your view do the foreach that you have already and it should work as expected.

Tip :

To debug this you shall Just do isset print_r etc.,

Also Make sure that you're calling the proper function in the controller from your route

Upvotes: 1

Related Questions