【问题标题】:Error: Undefined variable: projects (View: ../resources/views/project.blade.php)错误:未定义变量:项目(查看:../resources/views/project.blade.php)
【发布时间】: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的全部内容更新您的帖子

标签: php laravel


【解决方案1】:

根据你的路由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。更清楚地说,当您编写 )-&gt;with('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
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-07
    • 2019-11-25
    • 2019-11-07
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多