I am trying to create a comment system in which users can post comments on user posts.
I can save and display projects on a specific project page (/ projects / {id}). I have a form where users can leave comments behind. I can save comments in the database, but when I try to show comments, I get this error Undefined variable: comments (View: /var/www/resources/views/projects/show.blade.php).
My files: Comment model:
class Comment extends Model
{
protected $guarded = [];
public function user()
{
return $this->belongsTo('App\User');
}
public function post()
{
return $this->belongsTo('App\Project','project_id');
}
public $timestamps = false;
}
Project Model:
class Project extends Model
{
protected $fillable = [
'user_id',
'title',
'tags',
'summary',
'file_name',
'published_at'
];
public function User()
{
return $this->belongsTo('App\User');
}
}
My user model
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
public function Projects()
{
return $this->hasMany('App\Project');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
}
My 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']);
}
My opinion
@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>
<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
My routes:
Route::resource('student', 'StudentController');
Route::get('student/profile', 'StudentController@getProfile');
Route::post('projects/{id}','CommentController@store');
Route::get('projects/{id}','CommentController@index');
Route::post('projects/store', 'ProjectsController@store');
Route::resource('projects', 'ProjectsController');
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
Route::get('password/email', 'Auth\PasswordController@getEmail');
Route::post('password/email', 'Auth\PasswordController@postEmail');
Route::get('password/reset/{token}', 'Auth\PasswordController@getReset');
I get this error now: Undefined variable: project (View: /var/www/resources/views/projects/show.blade.php)