【发布时间】:2016-07-19 21:21:34
【问题描述】:
当我转到(例如)/project/2 时,我试图返回一个视图,但我收到以下错误:
未定义变量:项目(查看: ../resources/views/project.blade.php)
请记住,我对开发和 Laravel 还很陌生。我正在使用Laravel 5.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 () {
//
});
【问题讨论】:
-
请用
project.blade.php的全部内容更新您的帖子