我想用 Eloquent 多态关系做评论系统?
I want to do comments system with Eloquent Polymorphic Relations?
你好,我想在我的 projectDetail 组件中构建评论系统
但是当我尝试将评论存储在 DB[ 1048 Column 'commentable_id' cannot be null ] 时出现错误
这是我的 ProjectDeatil.vue 脚本:
<script>
export default {
data(){
return{
key: this.$route.params.id,
projets:[],
projet:{
id:'',
name:'',
durre:'',
description:'',
budget:'',
owner:'',
},
membres:[],
membre:{
id :'',
membre:'',
projet_id:'',
},
form : new Form({
id:'',
body:''
})
}
},
methods:{
afficherProjets(){
axios.get('/api/getProjects')
.then(({data}) => {this.projets=data.data});
},
afficherMembre(){
axios.get('/api/membreid').then(({data})=> {this.membres =data.data});
},
ajouterCommentaire(){
this.form.post('/api/comments/'+this.key).then(()=>{
this.form.reset()})
}
},
mounted() {
console.log('Component mounted.')
this.afficherProjets();
this.afficherMembre();
}
}
</script>
这是我的 CommentController 函数:
public function store($key){
//$data =$request->all();
$projet=new Projet;
$commentaire =new Commentaire;
$commentaire->user_id= auth()->user()->id;
$commentaire->body= request('body');
$commentaire->commentable_id = $key;
$projet->comments()->save($commentaire);
}
这是我在模型评论中的功能:
public function commentable(){
return $this->morphTo();
}
这是我在模型项目中的功能:
public function comments(){
return $this->morphMany('App\Commentaire','commentable')->latest();
}
这是我的路线:
Route::post('/comments/{key}', 'API\CommentController@store');
当您在 Laravel 中的多态关系上调用 ->save()
时,Eloquent 会神奇地为您添加正确的字段,在本例中为 commentable_id
,因此如果你正在做 ->comments()->save()
,你不需要设置它。但是,您应该做的是从数据库中获取 Projet
,然后 运行 代码。
当你新建一个 Projet
实例时,Eloquent 不知道这与数据库中的什么有关,所以它试图用 commentable_id
[=18] 来保存它=].替换这一行
$projet=new Projet;
有一行可以为您从数据库中获取 Projet
的正确实例,例如:
$projet = Projet::find($key); // Assuming $key is the primary key of your Projet model.
(为免生疑问,我还建议删除行 $commentaire->commentable_id = $key;
。)
你好,我想在我的 projectDetail 组件中构建评论系统 但是当我尝试将评论存储在 DB[ 1048 Column 'commentable_id' cannot be null ] 时出现错误 这是我的 ProjectDeatil.vue 脚本:
<script>
export default {
data(){
return{
key: this.$route.params.id,
projets:[],
projet:{
id:'',
name:'',
durre:'',
description:'',
budget:'',
owner:'',
},
membres:[],
membre:{
id :'',
membre:'',
projet_id:'',
},
form : new Form({
id:'',
body:''
})
}
},
methods:{
afficherProjets(){
axios.get('/api/getProjects')
.then(({data}) => {this.projets=data.data});
},
afficherMembre(){
axios.get('/api/membreid').then(({data})=> {this.membres =data.data});
},
ajouterCommentaire(){
this.form.post('/api/comments/'+this.key).then(()=>{
this.form.reset()})
}
},
mounted() {
console.log('Component mounted.')
this.afficherProjets();
this.afficherMembre();
}
}
</script>
这是我的 CommentController 函数:
public function store($key){
//$data =$request->all();
$projet=new Projet;
$commentaire =new Commentaire;
$commentaire->user_id= auth()->user()->id;
$commentaire->body= request('body');
$commentaire->commentable_id = $key;
$projet->comments()->save($commentaire);
}
这是我在模型评论中的功能:
public function commentable(){
return $this->morphTo();
}
这是我在模型项目中的功能:
public function comments(){
return $this->morphMany('App\Commentaire','commentable')->latest();
}
这是我的路线:
Route::post('/comments/{key}', 'API\CommentController@store');
当您在 Laravel 中的多态关系上调用 ->save()
时,Eloquent 会神奇地为您添加正确的字段,在本例中为 commentable_id
,因此如果你正在做 ->comments()->save()
,你不需要设置它。但是,您应该做的是从数据库中获取 Projet
,然后 运行 代码。
当你新建一个 Projet
实例时,Eloquent 不知道这与数据库中的什么有关,所以它试图用 commentable_id
[=18] 来保存它=].替换这一行
$projet=new Projet;
有一行可以为您从数据库中获取 Projet
的正确实例,例如:
$projet = Projet::find($key); // Assuming $key is the primary key of your Projet model.
(为免生疑问,我还建议删除行 $commentaire->commentable_id = $key;
。)