samedi 26 décembre 2015

I want to upload and save image in database but when i try to do it Call to a member function images() on null error shown up

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.

When i try to upload following error shown up: FatalErrorException in GalleryController.php line 71: Call to a member function images() on null

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');
    }

} Being new to laravel i didnt get the actual error meaning Call to a member function images() on null?? How to fix this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire