Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

academico-sis/academico

338 stars
6 code files
View academico-sis/academico on GitHub

database/migrations/2018_12_20_142237_create_comments_table.php

Open in GitHub
use 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 GitHub
use Illuminate\Database\Eloquent\Model;
 
class Comment extends Model
{
//
public function commentable()
{
return $this->morphTo();
}
//
}

app/Http/Controllers/CommentController.php

Open in GitHub
use 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 GitHub
use Illuminate\Database\Eloquent\Model;
 
class Student extends Model
{
//
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
//
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.