错误:未定义的变量:projects(查看:../ resources / views / project.blade.php)

时间:2016-03-31 12:11:52

标签: php laravel

当我去(例如)/project/2时,我试图返回一个视图但是我收到以下错误:

  

未定义的变量:projects(查看:   ../资源/视图/ 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 () {
    //
});

1 个答案:

答案 0 :(得分:1)

根据您的路由URI project/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。更清楚的是,当您编写)->with('project', $project);时,您意味着,with方法中的第一个参数将是用于访问第二个参数中传递的数据的变量名称,在您的情况下,您是&#39 ;使用了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
);
相关问题