Php 我无法在Laravel 7的数据库中存储信息
我在将信息存储到MySQL数据库中时遇到了一个问题,我尝试更改将信息存储到DB中的方法,但仍然存在相同的问题。此外,我需要你的帮助来找出问题并解决它 后置控制器Php 我无法在Laravel 7的数据库中存储信息,php,laravel,Php,Laravel,我在将信息存储到MySQL数据库中时遇到了一个问题,我尝试更改将信息存储到DB中的方法,但仍然存在相同的问题。此外,我需要你的帮助来找出问题并解决它 后置控制器 <?php public function store(){ $inputs= request()->validate([ 'title'=>'required|min:8|max:100', 'post_image'=>'file', 'body'=&g
<?php
public function store(){
$inputs= request()->validate([
'title'=>'required|min:8|max:100',
'post_image'=>'file',
'body'=>'required'
]);
if (request('post_image')){
$inputs['post_image']=request('post_image')->store('images');
}
auth()->user()->posts()->create($inputs);
return back();
}
要获取请求对象的文件,可以通过file()方法访问它。为什么不使用方法注入来处理请求
//使用illumb\Http\Request;-导入顶部的use语句
公共函数存储(请求$Request){
$inputs=$request->validate([
“标题”=>“必需”|最小值:8 |最大值:100”,
//“post_image”=>“file”,
“正文”=>“必需”
]);
如果($request->hasFile('post_image')&&&$request->file('post_image')->isValid()){
$inputs['post_image']=$request->file('post_image')->store('images');
}
auth()->user()->posts()->create($inputs);
返回();
}
欢迎来到SO。。。你面临什么问题?有错误吗?没有,我没有遇到任何问题,但是当我提交表单时,DB中没有存储任何内容。提交表单时会发生什么?若验证失败,你们将被重定向回,若你们通过验证,你们将被重定向回,验证也会失败吗?请你们的问题澄清问题的标题。看@Gander好的,我会更新的是的,现在就可以了;此外,我忘了将请求参数传递到存储函数中。不过,谢谢你的回答。谢谢
<?php
class Post extends Model
{
//
protected $guarded =[];
public function user(){
return $this->belongsTo(User::class);
}
public function getPostImageAttribute($value){
return asset($value);
}
}
<form action="{{route('post.store')}}" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<lable for="title">Title</lable>
<input type="text" name="title" class="form-control" placeholder="Enter title">
</div>
<div class="form-group">
<lable for="file">File</lable>
<input type="file" name="post_image" class="form-control-file" id="post_image" placeholder="Upload your image">
</div>
<div class="form-group">
<lable for="exampleInputEmail"></lable>
<textarea name="body" cols="30" rows="10" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary"> Submit</button>
</form>