【问题标题】:Laravel 5.1 Undefined variable: commentsLaravel 5.1 未定义变量:注释
【发布时间】:2016-01-21 12:02:41
【问题描述】:

我正在尝试构建一个评论系统,用户可以在其中将 cmets 留在用户项目帖子中。

我可以保存和显示项目,并且在特定的项目页面(/projects/{id})上我有一个表单,用户可以在其中留下 cmets。我能够将 cmets 保存在数据库中,但是当我尝试显示 cmets 时出现此错误 Undefined variable: cmets (View: /var/www/resources/views/projects/show.blade.php)。

我的文件: 评论型号:

class Comment extends Model
{
//comments table in database
protected $guarded = [];

public function user()
{
    return $this->belongsTo('App\User');
}

// returns post of any comment
public function post()
{
    return $this->belongsTo('App\Project','project_id');
}


public $timestamps = false;
}

项目模型:

class Project extends Model
{
protected $fillable = [
'user_id',
'title',
'tags',
'summary',
'file_name',
'published_at'
];
public function User()
{
    return $this->belongsTo('App\User');
}
}

我的用户模型

class User extends Model implements AuthenticatableContract,
                                AuthorizableContract,
                                CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = ['name', 'email', 'password'];

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = ['password', 'remember_token'];

public function Projects()
{
    return $this->hasMany('App\Project');
}

public function comments()
{
    return $this->hasMany('App\Comment');
}

}

我的评论控制器

 public function index()
{
    $comments = Comment::all();

    return view('projects.show', compact('comments'));
}

public function store()
{
    $input = Request::all();
    $comment = new Comment;
    $comment->body = $input['body'];
    $comment->on_projects = $input['project_id'];
    $comment->from_user = Auth::user()->id;
    $comment->save();
    return redirect('projects/'.$input['project_id']);

}

我的看法

@section('content')
<a href="/projects">Terug naar alle projecten</a>
<h1>Werkje: {{ $project->title }}</h1>
<h3>Gemaakt door: <a href='/student/{{ $project->User->id }}'>{{ $project->User->name }}</a></h3>
<img src="{{URL::to('/')}}/uploads/projects/{{ $project->file_name }}">
<h5>Tags: {{$project->tags}}</h5>
<hr />
<h1>Reageer</h1>

@if (Auth::check())

    <article> <!--Add comment -->

        <br/>
        {!! Form::open() !!}
        {!! form::text('body', null, ['class' => 'form-control']) !!}

        <br/>

        {!! Form::Submit('Reageer', ['class' => 'btn btn-primary form-control']) !!}
        {!! Form::hidden('project_id', $project->id) !!}


        {!! Form::close() !!}
        <br/>

    </article>


    <article>

        @foreach ($comments as $comment)

            <article>

                <p>Body: {{ $comment->body }}</p>
                <p>{{ $comment->user->name }}</p>

            </article>


        @endforeach

    </article>
@else
    <p>Gelieve in te loggen om te kunnen reageren.</p>
@endif

我的路线:

// Student routes REST methode
Route::resource('student', 'StudentController');
Route::get('student/profile', 'StudentController@getProfile');

// add comment
Route::post('projects/{id}','CommentController@store');
// show comments
Route::get('projects/{id}','CommentController@index');

//Project routes REST methode
Route::post('projects/store', 'ProjectsController@store');
Route::resource('projects', 'ProjectsController');

// Authentication routes
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Registration routes
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

// Password reset link request routes
Route::get('password/email', 'Auth\PasswordController@getEmail');
Route::post('password/email', 'Auth\PasswordController@postEmail');

// Password reset routes
Route::get('password/reset/{token}', 'Auth\PasswordController@getReset');

我现在收到此错误: 未定义变量:项目(查看:/var/www/resources/views/projects/show.blade.php)

【问题讨论】:

  • 你使用什么版本的 Laravel?
  • Laravel 5.1,谢谢。
  • 如果您在$comments = Comment::all(); 正下方放置dd($comments) 会发生什么?
  • 很遗憾,什么都没发生
  • 这很奇怪...你确定public function index() 正在被执行吗?如果您将//return view('projects.show', compact('comments')); 注释掉,您还能看到视图吗?编辑:如果您也发布路线会很有帮助。

标签: php mysql laravel


【解决方案1】:

好的,根据我们的聊天记录,您的路线文件中存在冲突,因为您同时使用项目的资源和项目的路线。我们整理了为特定项目加载 cmets 的最佳方法,现在一切正常,希望对您有所帮助:)

【讨论】:

  • 这可能对 OP 有帮助,但对其他搜索类似问题的用户没有帮助。请发布您的解决方案。
  • 有什么方法可以找到附加到此帖子的聊天内容吗?这至少是一个半小时的讨论。
【解决方案2】:

我的 CommentController 中的 index() 函数现在是空的。

我在我的 show{id} 函数中查询我的 ProjectsController 中的 cmets

public function show($id)
{
    $input = Request::all();
    $project = Project::all()->load("User");

    $project_comments = DB::table('comments')
        ->select('body', 'name')
        ->where('on_projects', '=', $id)
        ->join('users', 'users.id', '=', 'comments.from_user')
        ->get();

    return view('projects.show', ['project' => Project::findOrFail($id), 'comments' => $project_comments]);

}

这就是我解决路线冲突的方法

// add comment
Route::post('projects/{id}','CommentController@store');
// add project
Route::post('projects/store', 'ProjectsController@store');
//Project routes REST methode
Route::resource('projects', 'ProjectsController');

【讨论】:

    猜你喜欢
    • 2019-02-18
    • 2015-12-17
    • 2018-10-09
    • 2016-01-24
    • 2019-04-29
    • 1970-01-01
    • 2015-05-15
    • 2020-02-28
    • 1970-01-01
    相关资源
    最近更新 更多