FEATURE: add creating comments
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
<?php namespace Modules\Blog\Article\Entities;
|
||||||
|
|
||||||
|
use App\Models\Content;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Comment extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'content_id',
|
||||||
|
'content',
|
||||||
|
'email',
|
||||||
|
'name'
|
||||||
|
];
|
||||||
|
|
||||||
|
public function author()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function article()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Content::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
namespace Modules\Blog\Article\Http\Controllers\Admin;
|
||||||
|
|
||||||
|
use Modules\Blog\Article\Entities\Comment;
|
||||||
|
use Pingpong\Modules\Routing\Controller;
|
||||||
|
|
||||||
|
class CommentsOverview extends Controller
|
||||||
|
{
|
||||||
|
public function getOverview()
|
||||||
|
{
|
||||||
|
$comments = Comment::paginate(20);
|
||||||
|
|
||||||
|
dd($comments);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
namespace Modules\Blog\Article\Http\Controllers\Visitor;
|
||||||
|
|
||||||
|
use App\Models\Content;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Modules\Blog\Article\Entities\Comment;
|
||||||
|
use Modules\Blog\Article\Handlers\Article;
|
||||||
|
use Pingpong\Modules\Routing\Controller;
|
||||||
|
|
||||||
|
class CreateNewComment extends Controller
|
||||||
|
{
|
||||||
|
public function postNew(\Modules\Blog\Article\Http\Requests\CreateNewComment $request, $articleId)
|
||||||
|
{
|
||||||
|
$article = Content::where('id', $articleId)
|
||||||
|
->where('module_handler', Article::class)
|
||||||
|
->firstOrFail();
|
||||||
|
|
||||||
|
$article->module_settings = json_decode($article->module_settings);
|
||||||
|
|
||||||
|
if (!isset($article->module_settings->allowComments) || !$article->module_settings->allowComments) {
|
||||||
|
return redirect()
|
||||||
|
->route('page.content', ['url' => $article->url])
|
||||||
|
->with('error', trans('article::comments.disabledComments'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = $request->message;
|
||||||
|
$user = Auth::user();
|
||||||
|
if ($user) {
|
||||||
|
$name = $user->name;
|
||||||
|
$email = $user->email;
|
||||||
|
} else {
|
||||||
|
$name = $request->name;
|
||||||
|
$email = $request->email;
|
||||||
|
}
|
||||||
|
|
||||||
|
Comment::create([
|
||||||
|
'user_id' => $user ? $user->id : null,
|
||||||
|
'content_id' => $article->id,
|
||||||
|
'name' => $name,
|
||||||
|
'email' => $email,
|
||||||
|
'content' => $content
|
||||||
|
]);
|
||||||
|
|
||||||
|
return redirect()
|
||||||
|
->route('page.content', ['url' => $article->url])
|
||||||
|
->with('success', trans('article::comments.saved'));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Blog\Article\Http\Requests;
|
||||||
|
|
||||||
|
use App\Http\Requests\Request;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
|
class CreateNewComment extends Request
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
$user = Auth::user();
|
||||||
|
|
||||||
|
$required = ['message' => 'required'];
|
||||||
|
|
||||||
|
if (!$user) {
|
||||||
|
$required['name'] = 'required';
|
||||||
|
$required['email'] = 'required|email';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $required;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,36 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
Route::group([
|
|
||||||
'middleware' => 'web',
|
|
||||||
'prefix' => 'article',
|
|
||||||
'namespace' => 'Modules\Blog\Article\Http\Controllers'
|
|
||||||
], function () {
|
|
||||||
Route::get('/', 'CategoriesOverview@getOverview')
|
|
||||||
->name('article.overview');
|
|
||||||
|
|
||||||
Route::get('/new', 'CreateNewCategory@getNew')
|
|
||||||
->name('article.new');
|
|
||||||
|
|
||||||
Route::post('/new', 'CreateNewCategory@postNew')
|
|
||||||
->name('article.postNew');
|
|
||||||
|
|
||||||
Route::get('/edit/{id}', 'EditCategory@getEdit')
|
|
||||||
->name('article.edit');
|
|
||||||
|
|
||||||
Route::post('/edit/{id}', 'EditCategory@postEdit')
|
|
||||||
->name('article.postEdit');
|
|
||||||
|
|
||||||
Route::get('/delete/{id}', 'EditCategory@delete')
|
|
||||||
->name('article.delete');
|
|
||||||
});
|
|
||||||
|
|
||||||
// routes for comments
|
// routes for comments
|
||||||
|
|
||||||
Route::group([
|
Route::group([
|
||||||
'middleware' => 'web',
|
'middleware' => ['web', 'auth'],
|
||||||
'prefix' => 'comments',
|
'prefix' => 'comments',
|
||||||
'namespace' => 'Modules\Blog\Article\Http\Controllers'
|
'namespace' => 'Modules\Blog\Article\Http\Controllers\Admin'
|
||||||
], function () {
|
], function () {
|
||||||
|
|
||||||
Route::get('/', 'CommentsOverview@getOverview')
|
Route::get('/', 'CommentsOverview@getOverview')
|
||||||
->name('comments.overview');
|
->name('comments.overview');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Route::group([
|
||||||
|
'middleware' => ['web'],
|
||||||
|
'prefix' => 'comments',
|
||||||
|
'namespace' => 'Modules\Blog\Article\Http\Controllers\Visitor'
|
||||||
|
], function () {
|
||||||
|
|
||||||
|
Route::post('/new/{id}', 'CreateNewComment@postNew')
|
||||||
|
->name('comments.new');
|
||||||
|
});
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
|
||||||
|
class CreateComments extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('comments', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
$table->timestamps();
|
||||||
|
$table->integer('user_id')
|
||||||
|
->nullable(true)
|
||||||
|
->default(null)
|
||||||
|
->comment('When comment was inserted by logged user, save his ID here');
|
||||||
|
$table->integer('content_id');
|
||||||
|
$table->string('email');
|
||||||
|
$table->string('name');
|
||||||
|
$table->text('content');
|
||||||
|
$table->boolean('approved');
|
||||||
|
|
||||||
|
$table->foreign('content_id')
|
||||||
|
->references('id')
|
||||||
|
->on('contents')
|
||||||
|
->onDelete('cascade')
|
||||||
|
->onUpdate('cascade');
|
||||||
|
|
||||||
|
$table->foreign('user_id')
|
||||||
|
->references('id')
|
||||||
|
->on('users')
|
||||||
|
->onDelete('set null')
|
||||||
|
->onUpdate('cascade');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::drop('comments');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,4 +6,16 @@
|
|||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
{!! $page->content !!}
|
{!! $page->content !!}
|
||||||
|
|
||||||
|
<form action="{{route('comments.new', ['id' => $page->id])}}" method="post">
|
||||||
|
{!! csrf_field() !!}
|
||||||
|
<input type="text" name="name">
|
||||||
|
<input type="email" name="email">
|
||||||
|
|
||||||
|
<textarea name="message" id="" cols="30" rows="10">
|
||||||
|
|
||||||
|
</textarea>
|
||||||
|
|
||||||
|
<input type="submit">
|
||||||
|
</form>
|
||||||
@endsection
|
@endsection
|
||||||
Reference in New Issue
Block a user