【问题标题】:What is the reason: Undefined variable in laravel 5原因是什么:laravel 5中未定义的变量
【发布时间】:2017-02-27 03:15:16
【问题描述】:

您好,我是 Laravel 新手,正在使用 Laravel 5 进行我的第一个项目。当我将数据从控制器传递到视图时,我收到此错误。我被困在最后三天。我已经检查了来自在线博客的所有问题,甚至是 stackoverflow,但找不到解决方案,请帮忙。这是我的代码:

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

我也试过这些:

//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'));

这是错误页面:

【问题讨论】:

  • 从您的问题中我看不到您正在设置的错误。请使用错误消息扩展您的问题。
  • 在你发布的代码中,是你刚刚发布的那些不同的文件在一起,还是你的控制器和视图在同一个文件中?

标签: php laravel-5


【解决方案1】:

把它当作

return View('new')->with(['fullName' => $fullName]);

【讨论】:

  • ::with() 方法旨在获取键值对作为单独的参数。它更像return view('new', ['fullName' =&gt; $fullName]);,但与 Amir 的示例代码相比并没有改变。
  • @PaulKertscher,我附上了图片,请检查一下。
  • 我用过,return view('new')->with(["fullName" => $fullName]);但同样的错误 Undefined variable fullName:
  • Laravel Framework 5.4.6,这是我从 php artisan --version 命令检查的 laravel 应用程序版本。谁能告诉我,我是否应该更改 Kernel.php 文件,因为 stackoverflow.com/questions/34474224/… ,这说明这是 laravel 版本中的问题
  • 感谢大家的帮助,我已将此代码添加到 \Routes\Web.php 中,一切正常,Route::group(['middleware' => ['web']] , function () { Route::get('/list', 'CustomerController@list'); });
【解决方案2】:

您可以通过多种方式将变量传递到视图中

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

    }

希望这些对新手有所帮助。

【讨论】:

    【解决方案3】:

    在 laravel 中有多种灵活的可能性

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

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

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

    source

    【讨论】:

    • 感谢从控制器传递变量到视图的多个有趣的选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    相关资源
    最近更新 更多