laravel-SQLSTATE [HY000]:一般错误:1364 字段“agent_id”没有默认值
发布时间:2022-05-25 01:33:00 494
相关标签: # php# 数据
由于外键字段我有两个模型(Agent,Post),我无法将用户的主键保存为posts表中的外键。我可以手动插入数据,并与父表链接,但当用户从视图中的表单插入时,它会给我该错误的帮助吗。。提前感谢
Agent
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Agent extends Model
{
use HasFactory;
public function posts(){
return $this->hasMany(Post::class);
}
}
Post
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
public function user(){
return $this->belongsTo(Agent::class);
}
}
这是我在控制器中保存数据的方法
public function savepost(Request $request){
$post = new Post ;
$post->title = $request->title ;
$post->description = $request->description ;
$post->type = $request->type;
$saved = $post->save();
if($saved){
return back()->with('success','New post has been added successfuly');
}else{
return back()->with('fail' , 'Something went wrong , try again later');
}
}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报