簡體   English   中英

Laravel 5.1未定義變量:注釋

[英]Laravel 5.1 Undefined variable: comments

我正在嘗試構建一個評論系統,用戶可以在用戶項目帖子上留下評論。

我可以保存並顯示項目,並在特定的項目頁面上(/ projects / {id})我有一個表單,用戶可以在其中留下評論。 我能夠將注釋保存在數據庫中,但是當我嘗試顯示注釋時,我得到此錯誤未定義變量:comments(查看:/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');

我現在得到這個錯誤:未定義的變量:project(查看:/var/www/resources/views/projects/show.blade.php)

我的CommentController中的index()函數現在為空。

我在show {id}函數中查詢了ProjectsController中的注釋

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

好的,根據我們的聊天,您在路徑文件中存在沖突,因為您在同一時間使用項目資源和項目路徑。 我們整理出了為特定項目加載評論的最佳方式,現在一切正常,希望有幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM