this is a post.php model
public function comments()
{
return $this->hasMany(Comment::class); //aquivalente a App\Comment
}
public function user()
{
return $this->belongsTo(User::class);
}
Public function addComment($body) //nombre de la variable post Task $task el segundo es la variable o los datos a que se va a pasasr
{
$user_id = \Auth::user()->id;
$this->comments()->create(compact(['body', 'user_id']));
}
Public function addreply($body)
{
dd($body);
$this->comments()->create(compact(['body', 'parent_id']));
}
this is commentController
class CommentsController extends Controller
{
public function store(Post $post)
{
$this->validate(request(), [
'body'=> 'required|min:5'
]);
$post->addComment(request('body'));
return back();
}
} and this is for migration table
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id')->unsigned();
$table->integer('parent_id')->default(0);
$table->integer('user_id')->unsigned();
$table->string('body');
$table->timestamps();
});
Schema::table('comments', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropForeign('post_id');
Schema::dropForeign('user_id');
Schema::dropIfExists('comments');
}
so the problem is here when i send a comment or add comment it show user_id not default. first i added like a default but it has problems
so can someone help with it? i want to make like send a message and other user can respond a message i think i have to add some code but i do?
via vlass