2016-09-24 46 views
-1

我在儀表板視圖中顯示laravel 5.3.9中的錯誤,並顯示未定義的變量帖子。我試圖列出用戶在數據庫中的所有帖子。這是我到目前爲止的代碼:Laravel在視圖文件中定義的變量

UserController.php

<?php 

namespace App\Http\Controllers; 

use App\User; 
use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Auth; 


class UserController extends Controller 
{ 
    public function postSignUp(Request $request) 
    { 
     // Validate the entry of the sign up form 
     $this->validate($request, [ 
      'email'  => 'email | required | unique:users', 
      'first_name' => 'required | max:120', 
      'password' => 'required | min:6' 
     ]); 

     $email = $request['email']; 
     $first_name = $request['first_name']; 
     $password = bcrypt($request['password']); 

     $user = new User(); 
     $user->email = $email; 
     $user->first_name = $first_name; 
     $user->password = $password; 

     $user->save(); 

     Auth::attempt($user); 

     return redirect()->route('dashboard'); 

    } 

    public function postSignIn(Request $request) 
    { 
     // Validate the sign in form 
     $this->validate($request, [ 
      'email'  => 'email | required', 
      'password' => 'required | min:6' 
     ]); 

     if(Auth::attempt(['email' => $request['email'], 'password' => $request['password']])) { 
      return view('dashboard'); 
     } 

     return redirect()->back(); 
    } 
} 

?> 

PostController.php

<?php 

namespace App\Http\Controllers; 

use App\Post; 
use Illuminate\Http\Request; 

class PostController extends Controller 
{ 
    public function getDashboard() 
    { 
     $posts = Post::all(); 
     return view('dashboard', [ 'posts' => $posts ]); 
    } 

    public function postCreatePost(Request $request) 
    { 
     // Validate the post text field 
     $this->validate($request, [ 
      'body' => 'required | max:1000' 
     ]); 

     $post = new Post(); 
     $post->body = $request['body']; 
     $message = "There was an error"; 

     if($request->user()->posts()->save($post)) { 
      $message = "Post succesfully created"; 
     } 
     return redirect()->route('dashboard')->with([ 'message' => $message ])->compact($posts); 
    } 

    public function getDeletePost($post_id) 
    { 
     $post = Post::where('id', $post_id)->first(); 
     $post->delete(); 

     return redirect()->route('dashboard')->with(['message' => 'Post succesfully deleted']); 
    } 
} 

dashboard.blade.php

@foreach($posts as $post) 
     <article class="post"> 
      <p>{{ $post->body }}</p> 
      <div class="info"> 
       Posted by {{ $post->user->first_name }} on {{ $post->user->created_at }} 
      </div> 
      <div class="interaction"> 
       <a href="#">Like</a> | 
       <a href="#">Dislike</a> | 
       <a href="#">Edit</a> | 
       <a href="{{ route('post.delete', [ 'post_id' => $post->id ]) }}">Delete</a> 
      </div> 
     </article> 
     @endforeach 

web.app路線

<?php 

use App\Task; 
use Illuminate\Http\Request; 

/* 
|-------------------------------------------------------------------------- 
| Web Routes 
|-------------------------------------------------------------------------- 
| 
| This file is where you may define all of the routes that are handled 
| by your application. Just tell Laravel the URIs it should respond 
| to using a Closure or controller method. Build something great! 
| 
*/ 

Route::group(['middleware' => ['web']], function() 
{ 
    Route::get('/', function() { 
     return view('welcome'); 
    })->name('home'); 

    // Route for signed up users 
    Route::post('/signup', [ 
     'uses' => '[email protected]', 
     'as' => 'signup' 
    ]); 

    // Redirect to signin 
    Route::post('/signin', [ 
     'uses' => '[email protected]', 
     'as' => 'signin' 
    ]); 

    //Redirect to the dashboard 
    Route::get('/dashboard', [ 
     'uses'  => '[email protected]', 
     'as'   => 'dashboard', 
     'middleware' => 'auth' 
    ]); 

    // Create a new post and route 
    Route::post('/createpost', [ 
     'uses' => '[email protected]', 
     'as' => 'post.create' 
    ]); 

    // Create a new post and route 
    Route::get('/delete-post/{post_id}', [ 
     'uses'  => '[email protected]', 
     'as'   => 'post.delete', 
     'middleware' => 'auth' 
    ]); 

}); 

,而且我得到

ErrorException in 3bda00b66abf627abd64dcb541751a9b753d4384.php line 33: 
Undefined variable: posts (View: C:\wamp\www\laravel\resources\views\dashboard.blade.php) 
    in 3bda00b66abf627abd64dcb541751a9b753d4384.php line 33 
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44 
at PhpEngine->evaluatePath('C:\wamp\www\laravel\storage\framework\views/3bda00b66abf627abd64dcb541751a9b753d4384.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag))) in CompilerEngine.php line 59 
at CompilerEngine->get('C:\wamp\www\laravel\resources\views/dashboard.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag))) in View.php line 150 
at View->getContents() in View.php line 121 
at View->renderContents() in View.php line 86 
at View->render() in Response.php line 45 
at Response->setContent(object(View)) in Response.php line 201 
at Response->__construct(object(View)) in Router.php line 1042 
at Router->prepareResponse(object(Request), object(View)) in Router.php line 642 
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 53 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41 
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 137 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64 
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 137 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49 
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 137 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64 
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 137 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37 
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 137 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59 
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 137 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104 
at Pipeline->then(object(Closure)) in Router.php line 644 
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 618 
at Router->dispatchToRoute(object(Request)) in Router.php line 596 
at Router->dispatch(object(Request)) in Kernel.php line 267 
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46 
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104 
at Pipeline->then(object(Closure)) in Kernel.php line 149 
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116 
at Kernel->handle(object(Request)) in index.php line 53 

錯誤我不知道怎麼打發$帖子變量視圖文件。

回答

0

如果$posts爲空,則可能會遇到該錯誤。你可以嘗試封裝foreach

@if (isset($posts)) 
    @foreach ($posts as $post) 
    ... 
    @endforeach 
@endif 
+0

這是工作,並從頁面中的錯誤被刪除,但不是它不顯示任何用戶在數據庫中的帖子。 –

+0

然後可能你的Post :: all()返回null。你可以檢查你的Post模型和數據庫記錄。 – berkayk

+0

我發現問題是UserController.php 返回視圖('儀表板'); 應該是\t \t return redirect() - > route('dashboard');否則它會進入/登錄路線。 –

0

你首先要檢查控制器是否$ post正在返回任何數據。所以就在這條線後面。

$ posts = Post :: all();

做一個

DD($個);

如果此操作返回任何記錄,則可以檢查您的視圖。正如@ berkayk的回答所述。您應該在視圖上進行一些檢查,以確保您的視圖不會拋出錯誤,或者通過檢查控制器上的記錄計數來完全返回不同的視圖。