What I am trying to do is be able to post comments from a form to the post. I know the names for my models are bad. It should have been called post instead of dummy. I got this error when I called my new controller I made for the comments table. Also, I created a new function in the dummy model so my code can be more clean and organized. I was able to post comments before I made the new function in the dummy model and before I called a different controller. Here are the files that I think the errors is in:
Here is the route:
Route::post('post/{post}/comments', 'commentController@store');
Here is the dummy model which should have been called post and has the new function i created for it to make the code shorter in the commentController file:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class dummy extends Model
{
protected $guarded = [];
public function comments(){
return $this->hasMany(Comments::class, 'post_id');
}
public function addComment($body){
$this->comments()->create(compact('body'));
}
}
Lastly here is the commentController file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; //this one is for sql builders
use App\Dummy;
use App\Comments;
use Illuminate\Http\RedirectResponse;
use DateTime; //to create a new date object yo need to include this namespace
class commentController extends Controller
{
public function store(Dummy $post){
$date = new DateTime();
$timestamp = $date->getTimestamp();
$id = $post->id;
$post->addComment(request('body'));
return view('post', compact('post', 'timestamp', 'id'));
}
}
via Lami