I am beginner in laravel and i want to make image uploading and saving app.
Everything is going cool but as i try to upload images it isnot saved to database.
But in public/gallery/images folder images are present.How this is possible without saving in database.
当我尝试上传以下错误时显示: GalleryController.php第71行中的FatalErrorException: 在null
上调用成员函数images()My controller is:
public function doImageUpload(Request $request){
//get the file from the post request
$file = $request->file('file');
//set my file name
$filename = uniqid() . $file->getClientOriginalName();
//move the file to correct location
$file->move('gallery/images',$filename);
//save image details into the database
$gallery = Gallery::find($request->input('gallery_id'));//get the gallery_id
$image = $gallery->images()->create([
'gallery_id'=>$request->input('gallery_id'),
'file_name'=>$filename,
'file_size'=>$file->getClientSize(),
'file_mime'=>$file->getClientMimeType(),
'file_path'=>'gallery/images/' . $filename,
'created_by'=>1,
]);
My view is:
<div class="row">
<div class="col-md-12">
<form action="{{url('image/do-upload')}}"
method="POST" enctype="multipart/form-data">
<label>Select image to upload:</label >
<input type="file" name="file" id="file">
<input type="submit" value="Upload" name="submit">
<input type="hidden" name="_token" value={{ csrf_token() }}>
</form>
</div>
and my image model is:
class Image extends Model
{
protected $fillable = [
'gallery_id','file_name','file_size','file_mime','file-path','created_by'
];
public function gallery(){
return $this->belongsTo('App\Gallery');
}
} 作为laravel的新手,我没有得到实际的错误含义调用成员函数images()上的null? 如何解决这个问题?
答案 0 :(得分:0)
在使用find方法后对pronunciations
进行日志调试,很有可能它没有初始化,如果$gallery
方法找不到你给它的id,它会返回{ {1}}
一个好的做法是验证你得到一个对象,并验证你的输入包含gallery_id,并且它是一个数字&gt; 0
find
您也可以通过Image create方法创建图像,而不是使用图库关系,如果galley_id输入不正确,可能会创建一个图像,如果gallery_id为0,则可能会创建图像。
null
答案 1 :(得分:0)
我不知道你是否解决了这个问题,但我遇到了同样的问题,我找到了解决方案。
发送表单时,需要输入隐藏的输入。
<input type"hidden" name="gallery_id" value="{{ $gallery->id }}">
我确定你和我一样工作,DropZone Gallery,如果是的话,我认为你有解决方案