简体   繁体   English

错误:未定义变量:项目(视图:../resources/views/project.blade.php)

[英]Error: Undefined variable: projects (View: ../resources/views/project.blade.php)

I am trying to return a view when i go to (for instance) /project/2 but i get the following error:当我转到(例如) /project/2时,我试图返回一个视图,但出现以下错误:

Undefined variable: projects (View: ../resources/views/project.blade.php)未定义变量:项目(查看:../resources/views/project.blade.php)

Keep in mind that i'm pretty new to development and Laravel .请记住,我对开发和Laravel I'm using Laravel 5.2 .我正在使用Laravel 5.2

This is my controller:这是我的控制器:

<?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);
  }
}

My form:我的表格:

<!-- 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-->

My routes:我的路线:

<?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 () {
    //
});

According to your route URI project/2 you trigger the following method:根据您的路由 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);

}

In this case, you've used project not projects so the variable $projects will not be available, instead use $project .在这种情况下,您使用了project而不是projects因此变量$projects将不可用,而是使用$project To be more clear, when you write )->with('project', $project);更清楚地说,当你写)->with('project', $project); you meant that, the first argument in the with method would be the variable name to access the data passed in the second parameter and in your case, you've used with('project', $project) .您的意思是, with方法中的第一个参数将是访问第二个参数中传递的数据的变量名称,在您的情况下,您使用了with('project', $project) So it works just like this:所以它的工作原理是这样的:

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
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义变量:products(查看:E:\\Laravel Projects\\laravelstore\\resources\\views\\pages\\productsdash.blade.php) - Undefined variable: products (View: E:\Laravel Projects\laravelstore\resources\views\pages\productsdash.blade.php) 为什么会出现此错误:未定义变量:产品(视图:C:\\xampp\\htdocs\\basic\\resources\\views\\mycart.blade.php)? - Why do I get this error : Undefined variable: product (View: C:\xampp\htdocs\basic\resources\views\mycart.blade.php)? 未定义的变量:transaction_id(视图:resources / views / view_all_transaction.blade.php) - Undefined variable: transaction_id(View: resources/views/view_all_transaction.blade.php) 未定义变量:worklogs_yesterday(查看:/home/riwaj/Desktop/projects/dmt-intern-manager/InternManager/resources/views/home.blade.php) - Undefined variable: worklogs_yesterday (View: /home/riwaj/Desktop/projects/dmt-intern-manager/InternManager/resources/views/home.blade.php) 未定义变量:错误(视图:C:\\ wamp \\ www \\ laravel \\ myproject \\ resources \\ views \\ songs \\ create.blade.php) - Undefined variable: errors (View: C:\wamp\www\laravel\myproject\resources\views\songs\create.blade.php) 未定义变量:页面(视图:D:\\wamp\\www\\laraangular\\resources\\views\\site\\about.blade.php) - Undefined variable: page (View: D:\wamp\www\laraangular\resources\views\site\about.blade.php) “未定义的变量:类别(视图:C:\\ xampp \\ htdocs \\ blog \\ resources \\ views \\ partials \\ _popular.blade.php)” - “Undefined variable: categories (View: C:\xampp\htdocs\blog\resources\views\partials\_popular.blade.php) ” 未定义的变量:帖子(查看:C:\\ xampp \\ htdocs \\ practiseapp \\ resources \\ views \\ dashboard.blade.php) - Undefined variable: posts (View: C:\xampp\htdocs\practiseapp\resources\views\dashboard.blade.php) 未定义变量:artikel(视图:C:\\xampp\\htdocs\\uts\\resources\\views\\layouts\\home.blade.php) - Undefined variable: artikel (View: C:\xampp\htdocs\uts\resources\views\layouts\home.blade.php) 未定义变量:posts(查看:C:\Users\BWB\Documents\Laravel\restravels\resources\views\profile.blade.php) - Undefined variable: posts (View: C:\Users\BWB\Documents\Laravel\restravels\resources\views\profile.blade.php)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM