2016-03-31 3 views
1

私は(例えば)/project/2に行くとビューを返そうとしていますが、次のエラーが表示されます:エラー:未定義の変数:projects(表示:../resources/views/project.blade.php)

Undefined variable: projects (View: ../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('/', '[email protected]'); 

Route::get('/test', function() { 
    return view('testPage'); 
} 
); 

Route::post('/', '[email protected]'); 

Route::post('/project', '[email protected]'); 
Route::get('/project/{id}', '[email protected]'); 

/* 
|-------------------------------------------------------------------------- 
| 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() { 
    // 
}); 
+0

'project.blade.php'の完全な内容であなたの投稿を更新してください –

答えて

0

あなたによると、ルート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); 

} 

を、あなたはとても変数$projects$projectを使用する代わりに、使用できなくなりますprojectないprojectsを使用しました。より明確にするために、)->with('project', $project);と書くと、withメソッドの最初の引数は2番目のパラメータで渡されたデータにアクセスするための変数名になり、その場合は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 
); 
関連する問題