您好我是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'));
这是错误页面:
答案 0 :(得分:0)
将其作为
传递return View('new')->with(['fullName' => $fullName]);
答案 1 :(得分:0)
您可以以多种方式将变量传递到视图中
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 !!!
}
希望这些能帮助新手。
答案 2 :(得分:0)
laravel中有多种灵活的可能性
return view('new')->withFullName($fullName);
或强>
return view('new')->with('fullName' , $fullName);
或强>
return view('new',compact ('fullName'))