با سلام و احترام
من در قسمت comment ها قبلا به راحتی کامنت را ثبت می کردم ولی الان که سایت تمام شده و دارم تست می کنم متاسفانه با پیغام بالا روبرو می شم .خودم فکرمی کنم بعد از دوزبانه کردن سایت این مشکل ایجاد شده ولی نمی تونم رفعش کنم. همه جاهم نوشته مربوط به متد post است ولی من چون قبلا می تونستم به راحتی نظرات را ثبت کنم و تغییری در این قسمت ندادم ، نمی دونم به چی ربط داره. لطفا راهنمایی فرمایید.
سلام
@vajihebromand
کد مربوط به روت و مربوط به فرمتون رو قرار بدید تا راهنمایی بشه
شاید پاسخ شما همینجا باشد
با سلام و احترام
من فقط یک روت از جنس post در route.php دارم به شرح زیر
Route::post('/comment' , 'HomeController@comment');
وفرم comment.blade.php به شرح زیر است
<!-- Comments Form -->
@if(auth()->check())
<div class="well">
<h4>ثبت نظر :</h4>
@include('Home.layouts.errors')
<form role="form" action="/comment" method="post">
{{ csrf_field() }}
<input type="hidden" name="parent_id" value="0">
<input type="hidden" name="commentable_id" value="{{ $subject->id }}">
<input type="hidden" name="commentable_type" value="{{ get_class($subject) }}">
<div class="form-group">
<textarea name="comment" class="form-control" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">ارسال</button>
</form>
</div>
@else
<div class="alert alert-danger">شما برای ارسال نظر باید وارد سایت شوید</div>
@endif
<hr>
<!-- Posted Comments -->
@foreach($comments as $comment)
<div class="media">
<a class="pull-right" href="#">
<img class="media-object" src="http://placehold.it/64x64" alt="">
</a>
<div class="media-body">
<h4 class="media-heading">{{ $comment->user->name }}
<small>{{ jdate($comment->created_at)->ago() }}</small>
<button class="pull-left btn btn-xs btn-success" data-toggle="modal" data-target="#sendCommentModal" data-parent="{{ $comment->id }}">پاسخ</button>
</h4>
{!! $comment->comment !!}
<!-- Nested Comment -->
@if(count($comment->comments))
@foreach($comment->comments as $childComment)
<div class="media">
<a class="pull-right" href="#">
<img class="media-object" src="http://placehold.it/64x64" alt="">
</a>
<div class="media-body">
<h4 class="media-heading">{{ $childComment->user->name }}
<small>{{ jdate($childComment->created_at)->ago() }}</small>
</h4>
{!! $childComment->comment !!}
</div>
</div>
@endforeach
@endif
<!-- End Nested Comment -->
</div>
</div>
@endforeach
<!-- Comment -->
<div class="modal fade" id="sendCommentModal" tabindex="-1" role="dialog" aria-labelledby="sendCommentModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel">ارسال پاسخ</h4>
</div>
<div class="modal-body">
<form action="/comment" method="post">
{{ csrf_field() }}
<input type="hidden" name="parent_id" value="0">
<input type="hidden" name="commentable_id" value="{{ $subject->id }}">
<input type="hidden" name="commentable_type" value="{{ get_class($subject) }}">
<div class="form-group">
<label for="message-text" class="control-label">متن پاسخ:</label>
<textarea class="form-control" id="message-text" name="comment"></textarea>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">ارسال</button>
<button type="button" class="btn btn-default" data-dismiss="modal">انصراف</button>
</div>
</form>
</div>
</div>
</div>
</div>
وکد مربوط به کلید ثبت نظر در کنترلر home controller به شرح زیر است
public function comment()
{
$this->validate(request(),[
'comment' => 'required|min:5'
]);
// return dd('test');
// Comment::create(array_merge([
// 'user_id' => auth()->user()->id
// ], \request()->all()));
auth()->user()->comments()->create(request()->all());
//return redirect(route('articles.index'))
return back();
}
, و در مدل comment داریم
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $fillable = [
'user_id',
'parent_id',
'comment',
'approved',
'commentable_id',
'commentable_type'
];
/**
* Get all of the owning commentable models.
*/
public function commentable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->hasMany(Comment::class , 'parent_id' , 'id');
}
public function setCommentAttribute($value)
{
$this->attributes['comment'] = str_replace(PHP_EOL , "<br>" , $value);
}
}
ممنون از پاسخ شما
سلام
@vajihebromand
حس میکنم توی کنترلر یه ایرادی هست
public function comment(Request $request)
{
$this->validate($request, [
'comment' => 'required|min:5'
], [
'comment.required' => 'نظر را وارد نمایید'
]);
}
این مورد رو چک کنید
توی قسمت namespace ها هم چک کنید این مورد یوز شده باشه
use Illuminate\Http\Request;
اگر بازم ارور داشتید
خط به خط dd() بگیرید ، بالاخره معلوم میشه مشکل از کجاست .
متاسفانه اصلا خروجی ddرا در کدها اصلا خروجی نشان نمی دهد
Laravel Framework 5.4.23
با تشکر
خودم فکر می کنم از وقتی دو زبانه کردم ارور می ده.کدcomment\ را به /{{app()->getLocale()}}/comment تغییر دادم ولی جواب نداد
.فکر کنم return back() را شاید باید redirect کنم ولی نمی دونم به چه صفحه ای rediect کنم
سلام
@vajihebromand
از کنترلر برید جلو
توی کنترلر خروجی هارو خط به خط دیباگ کنید
dd() بگیرید از اول
ببینید کجا ها بهتون خروجی نمیده و ارورش چی هست
از همونجا دیباگ کنید
با سلام و احترام
با حذف قابلیت دو زبانه فعلا مشکلم حل شد.
ممنون از همراهیتون