0

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', 'CustomerController@list');    
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:

This is error page please check it

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Muhammad Amir
  • 79
  • 2
  • 4
  • 14
  • 1
    From your question I cannot the see error you are etting. Please expand your question with the error message. – Paul Kertscher Feb 06 '17 at 11:22
  • In the code you posted, are those different files you just posted together, or is your controller and view in the same file? – MikeBird Feb 09 '17 at 17:07

3 Answers3

0

pass it as

return View('new')->with(['fullName' => $fullName]);
Mr Singh
  • 114
  • 9
  • 1
    `::with()` method is intended to get a key value pair as separate parameters. It's more like `return view('new', ['fullName' => $fullName]);` but it doesn't change a thing comparing to Amir's example code. – Chris Cynarski Feb 06 '17 at 11:28
  • @PaulKertscher , i have attached image please check it. – Muhammad Amir Feb 06 '17 at 12:05
  • I have used, return view('new')->with(["fullName" => $fullName]); but same error Undefined variable fullName: – Muhammad Amir Feb 06 '17 at 12:06
  • Laravel Framework 5.4.6, this is my laravel application version which i have checked from , php artisan --version command. Can anyone tell me, should i changed the Kernel.php file because http://stackoverflow.com/questions/34474224/undefined-variable-errors-laravel-5-2 , here is telling that this is problem in version of laravel – Muhammad Amir Feb 06 '17 at 12:21
  • Can anyone help me in this error because i have tried all options from online resources but no solution, i am sucked – Muhammad Amir Feb 06 '17 at 13:25
  • dd value for fullname in controller do you get it as needed? – Mr Singh Feb 06 '17 at 13:36
  • even the code works at my end if a pass the data the way u passed for the same – Mr Singh Feb 06 '17 at 13:55
  • 1
    Thanks all of you for helping, i have add into \Routes\Web.php this code and everything is working good, Route::group(['middleware' => ['web']], function () { Route::get('/list', 'CustomerController@list'); }); – Muhammad Amir Feb 06 '17 at 14:01
0

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 .

Atiqur
  • 3,802
  • 1
  • 25
  • 26
0

There are multiple and flexible possibilities in laravel

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

OR

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

OR

    return view('new',compact ('fullName')) 

source

FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66