Laravel未定义视图文件中的变量

时间:2016-09-24 23:22:09

标签: php laravel-blade laravel-5.3

我在仪表板视图中的laravel 5.3.9中收到错误,其中包含未定义的变量帖子。我试图列出他在数据库中的所有帖子。到目前为止,这是我的代码:

UserController.php

shouldComponentUpdate

PostController.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();
    }
}

?>

dashboard.blade.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']);
    }
}

web.app路线

@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

我得到的错误

    <?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' => 'UserController@postSignUp',
        'as'   => 'signup'
    ]);

    // Redirect to signin
    Route::post( '/signin', [
        'uses' => 'UserController@postSignIn',
        'as'   => 'signin'
    ]);

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

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

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

});

我不知道如何将$ posts变量传递给视图文件。

2 个答案:

答案 0 :(得分:0)

如果$posts为空,则可能会遇到该错误。您可以尝试使用

封装foreach
@if (isset($posts))
    @foreach ($posts as $post)
    ...
    @endforeach
@endif

答案 1 :(得分:0)

如果$ post返回任何数据,您首先要检查控制器。就在这条线之后;

  

$ posts = Post :: all();

做一个

  

DD($帖);

如果这返回任何记录,那么您可以检查您的视图。正如@ berkayk的回答所提到的那样。您应该在视图中进行一些检查,以确保您的视图不会抛出错误或通过检查控制器上的记录计数完全返回不同的视图。