我想上传图像并将其保存在数据库中,但是当我尝试这样做时,在出现空错误时调用成员函数images()


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中的FatalErrorException.php第71行:在 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('AppGallery');
    }

}作为 laravel 的新手,我没有收到实际的错误,这意味着在 null 上调用成员函数 images(( ??如何解决这个问题?

$gallery 进行日志调试 使用 find 方法后,很有可能它没有初始化,如果 find 方法找不到你给它的 id,它会返回null一个好的做法是验证你得到了一个对象,并验证你的输入是否包含gallery_id,并且它是一个数字> 0

if ($request->has('gallery_id') && intval($request->input('gallery_id'))>0){      
  $gallery = Gallery::find($request->input('gallery_id'));//get the gallery_id
  if ($gallery){
     $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,
           ]);
  } else {
    return Response::make('no gallery found with this $request->input('gallery_id') gallery_id',404);
  }
} else {
  return Response::make('invalid gallery_id as input',400);
}

您还可以按原样通过 Image Create 方法创建图像,而不是使用库关系,如果galley_id输入不正确,这将创建一个可能gallery_id为 0 的图像。

$image = Image::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,
           ]);
我不知道

你是否解决了这个问题,但我遇到了同样的问题,我找到了解决方案。

发送表单时,您需要放置一个隐藏的输入。

<input type"hidden" name="gallery_id" value="{{ $gallery->id }}">

我相信你和我一样做同一个项目,DropZone Gallery,如果是这样,我认为你有解决方案

相关内容

最新更新