-
database/migrations/2018_12_20_142237_create_comments_table.php
Open in GitHubuse Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateCommentsTable extends Migration { public function up() { Schema::create('comments', function (Blueprint $table) { $table->increments('id'); $table->integer('commentable_id')->unsigned(); $table->string('commentable_type'); $table->text('body'); $table->boolean('action')->default(false)->nullable(); $table->integer('author_id')->unsigned()->nullable(); $table->timestamps(); //$table->softDeletes(); }); Schema::table('comments', function (Blueprint $table) { $table->foreign('author_id') ->references('id')->on('users') ->onDelete('restrict'); }); } // }
-
app/Models/Comment.php
Open in GitHubuse Illuminate\Database\Eloquent\Model; class Comment extends Model { // public function commentable() { return $this->morphTo(); } // }
-
app/Http/Controllers/CommentController.php
Open in GitHubuse App\Http\Requests\CommentRequest as StoreRequest; use App\Models\Comment; use Illuminate\Support\Facades\Log; class CommentController extends Controller { // public function store(StoreRequest $request) { Log::info('Comment created by '.backpack_user()->firstname); return Comment::create([ 'commentable_id' => $request->input('commentable_id'), 'commentable_type' => $request->input('commentable_type'), 'action' => $request->input('action') ?? 0, 'body' => $request->input('body'), 'author_id' => backpack_user()->id, ]); } // }
-
resources/views/students/show.blade.php
Open in GitHub// <div class="row"> <div class="col-md-12"> <student-comments :comments="{{ json_encode($comments) }}" :id="{{ json_encode($student->id) }}" :type="'App\\Models\\Student'" route="{{ route('storeComment') }}"> </student-comments> </div> </div> //
-
resources/js/components/StudentCommentComponent.vue
Open in GitHub<template> // </template> <script> export default { props: ["comments", "id", "type", "route"], data() { return { comment_body: null, action: false, showEditField: false, errors: null, commentlist: this.comments, isValidated: false, selectedComment: null, }; }, mounted() {}, methods: { // addComment() { axios .post(this.route, { body: this.comment_body, commentable_id: this.id, commentable_type: this.type, action: this.action, }) .then(response => { this.commentlist.push(response.data); this.comment_body = null; this.showEditField = false; this.errors = null; this.isValidated = true; setTimeout(() => { this.isValidated = false; }, 3000) }) .catch(e => { this.errors = e.response.data.errors.body[0]; }); }, // }, }; </script>
-
app/Models/Student.php
Open in GitHubuse Illuminate\Database\Eloquent\Model; class Student extends Model { // public function comments() { return $this->morphMany(Comment::class, 'commentable'); } // }