当前位置: 动力学知识库 > 问答 > 编程问答 >

php - What is the reason: Undefined variable in laravel 5

问题描述:

Hello I am new to Laravel and working on my first project using Laravel 5. I am getting this error when i pass the data from controller to view. I am stuck last three days. I have checked all questions from online blogs, even stackoverflow but couldn't find the solution please help. Here is my code:

App\Routes\web.php

Route::get('/list', '[email protected]');

App\Http\Controllers\CustomerController.php

public function list(){

$first = 'Mian';

$last = 'Amir';

$fullName = $first . " " . $last;

//dd($fullName);

return view('new')->with("fullName", $fullName);

//return view('new')->withFullName($fullName);

}

Resources\Views\new.blade.php

@extends('master')

@section('content')

<h1>New Content Will Goes Here: {{$fullName}}</h1>

@endsection

I have also tried these also:

//return view('new')->with('customer', $customers);

//return View::make('new')->with(array('customers' => $customers));

// return View::make('new', compact('customers'));

/$customers = DB::table('customers')->get();

//return view('new', ['customers' => $customers]);

//return View::make('new', compact('customers'));

//return View::make('new')->with('customers', $customers);

//dd($customers);

//return View::make('new')->with(array('customers' => , $customers));

//$customers = Customer::all();

//dd($customers);

//return View::make('view')->with('customers', $customers);

//return View::make('view', compact('customers'));

//return $view->with('customers', $customers)->with('q', $q);

//return view('view', ['key' => 'The big brown fox jumped over the lazdog']);

//$key = 'If a would chuck can chuck wood,';

//return view('view', compact('key'));

This is the error page:

网友答案:

pass it as

return View('new')->with(['fullName' => $fullName]);
网友答案:

you can pass a variable into view in multiple way

    public function list(){
        $first = 'Mian';
        $last = 'Amir';
        $fullName = $first . " " . $last;

        return view('new')->with(["fullName"=>$fullName]);  //Or
        return view('new', ["fullName"=>$fullName]);     //Or
        return view('new', compact("fullName"));       //Or

        Session::put('fullName', $fullName); 
        return view('new');  //then in view- Session::get('fullName');

        //Or using dynamic method which I personally dislike without any reason! :P
        return view('new')->withFullName($fullName); 
       //But i this case in view you have to use underscored variable $full_name !!!

    }

Hope these will help newbies .

分享给朋友:
您可能感兴趣的文章:
随机阅读: