错误:未定义变量:projects(视图:../resources/views/project.blade.php)
我试图在转到(例如)错误:未定义变量:projects(视图:../resources/views/project.blade.php),php,laravel,Php,Laravel,我试图在转到(例如)/project/2时返回视图,但出现以下错误: 未定义变量:项目(视图: ../resources/views/project.blade.php) 请记住,我对开发和Laravel相当陌生。我使用的是Laravel5.2 这是我的控制器: <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Project; use App\Http\Controllers\Cont
/project/2
时返回视图,但出现以下错误:
未定义变量:项目(视图:
../resources/views/project.blade.php)
请记住,我对开发和Laravel
相当陌生。我使用的是Laravel5.2
这是我的控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Project;
use App\Http\Controllers\Controller;
class pagesController extends Controller {
public function index() {
$projects = \App\Project::orderBy('created_at', 'desc')->get();
return view('index')->with('projects', $projects);
}
public function storeProject(Request $request) {
$project = new Project;
$project->name = $request->newProject;
$project->save();
}
public function getProject($id) {
$project = \App\Project::findorfail($id); //findorfail 404 teruggeven wanneer id null is.
return view('project')->with('project', $project);
}
}
<!-- Begin nieuw project formulier-->
<div class="dropdown-container">
<div class="text-center dropdownwrap">
<form role="form" method="POST" action="/project/">
<div class="form-group">
<input name="newProject" type="text" class="form-control" id="newProjectField" placeholder="Voeg project toe..."/>
</div>
<div class="form-group text-center">
<button type="submit" class="btn btn-primary">
Bewaar
</button>
</div>
</form>
</div>
<a href="#" class="btn btn-default btn-block dropdownButton"><span class="glyphicon glyphicon-plus"></span></a>
</div>
<!-- Einde Nieuw project formulier-->
<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in 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('/', 'pagesController@index');
Route::get('/test', function() {
return view('testPage');
}
);
Route::post('/', 'pagesController@storeProject');
Route::post('/project', 'pagesController@storeProject');
Route::get('/project/{id}', 'pagesController@getProject');
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function () {
//
});
根据您的路由URIproject/2
触发以下方法:
public function getProject($id)
{
$project = \App\Project::findorfail($id); //findorfail 404 teruggeven wanneer id null is.
return view('project')->with('project', $project);
}
在这种情况下,您使用了project
而不是projects
,因此变量$projects
将不可用,请改用$project
。更清楚地说,当您使用('project',$project)编写)->时
您的意思是,with
方法中的第一个参数将是访问在第二个参数中传递的数据的变量名,在您的例子中,您使用了with('project',$project)
。所以它的工作原理是这样的:
return view('project')->with(
'project', // <-- This will be available in the view as $project
$project // <-- This will be contained in the $project variable in the view
);
返回视图(“项目”)->(
'项目',//请用project.blade.php