我做了评论功能,但它不起作用。它使POST数据包很好,但它不会对db进行任何查询。因此表格是空的。
以下是我的源代码
CommentController.php
<?php
namespace App\Http\Controllers; //171029
use Illuminate\Http\Request;
use App;
class CommentsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function store(\App\Http\Requests\CommentsRequest $request, \App\Article $article)
{
$comment = $article->comments()->create(array_merge(
$request->all(), ['user_id'=> $request->user()->id]
));
return redirect(route('articles.show', $article->id) .'#comment_'.$comment->id);
}
}
CommentModel
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $fillable = ['commentable_type', 'commentable_id', 'user_id', 'parent_id', 'content',];
protected $with = ['user',];
public function user()
{
return $this->belongsTo(User::class);
}
public function commentable()
{
return $this->morphTo();
}
public function replies()
{
return $this->hasMany(Comment::class, 'parent_id')->lastest();
}
public function parent()
{
return $this->belongsTo(Comment::class, 'parent_id', 'id');
}
DataBase(评论表中的数据)
+------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| user_id | int(10) unsigned | NO | MUL | NULL | |
| parent_id | int(10) unsigned | YES | MUL | NULL | |
| commentable_type | varchar(255) | NO | | NULL | |
| commentable_id | int(10) unsigned | NO | | NULL | |
| content | text | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------------+------------------+------+-----+---------+----------------+
它没有任何错误..但不发送查询...请帮助我。
答案 0 :(得分:0)
质量分配
对于质量分配,您需要指定fillable
或guarded
。
You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.