这是控制器
public function store(Request $request) {
$post = new Post;
$photo = new Photo;
$user = Auth::user();
$input = $request->except('file');
$input['user_id'] =$user->id;
$post->create($input);
if ($files = $request->file('file')) {
foreach($files as $file) {
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = $photo->create(['file'=>$name, $photo->post_id => $post->id]);
}
}
return 'Upload successful!';
这是模型
protected $fillable = ['file', 'post_id', 'user_id'];
protected $uploads = '/images/';
public function post() {
return $this->belongsTo('App\Post');
}
public function getFileAttribute($photo) {
return $this->uploads . $photo;
}
我收到此错误 完整性约束违规:1048列' post_id'不能为空(SQL:插入照片(文件,post_id,updated_at,created_at
)答案 0 :(得分:0)
你写了一个错字:
$photo = $photo->create(['file'=>$name, 'post_id' => $post->id]);