0

I am trying to submit an image into the database but I keep getting this error: Type error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model, null given, called in C:\xampp\htdocs\Evaluation\app\Http\Controllers\ImageController.php on line 24.

I checked with other questions in StackOverflow and mostly they said it was the fault of the saving part where they put something like this, $post but I have checked and there is nothing wrong with it. The relationship doesn't seem to have any problem as well but why is it still not working? The error also return me a null when I upload image. The null is returning at the part here, $UserImage = $request->input('UserImage'); So could my problem be in image1.blade.php?

ImageController:

public function test(personal_info $user){ 
    return view('image1',compact('user'));
}

public function test1(Request $request){
    $UserImage = new Image;
    $personal_info = new personal_info;
    $UserImage = $request->input('UserImage');
    $id = $request->user_id;
    $id = personal_info::find($id);
    $id->Images()->save($UserImage);
       return redirect('/summary');
    }

image1.blade.php (where i submit the form)

 <form class="form-horizontal" method="post" action="{{ url('/Upload')}}" enctype="multipart/form-data">

                        {{  csrf_field()  }}
<input type="hidden" name="user_id" value="{{$user->id}}">

<div class="form-group">
    <label for="imageInput" class="control-label col-sm-3">Upload Image</label>
            <div class="col-sm-9">
                <input type="file" name="UserImage">

        </div>
    </div>

 <div class="form-group">
            <div class="col-md-6-offset-2" style="padding-left: 30px">
              <input type="submit" class="btn btn-primary" value="Save">
            </div>
          </div>
          </form>

Image.php:

public function personal_infos() {
    return $this->belongsTo('App\personal_info', 'user_id', 'id');
}

personal_info.php:

public function Images() {
    return $this->hasOne('App\Image','user_id');
}
9
  • why do you think an uploaded file is an Eloquent Model? Commented Dec 5, 2017 at 8:09
  • I need the image to have an id (something like the user profile picture) that why i use eloquent, I have already defined the relationship in the model as you can see in the question I just updated Commented Dec 5, 2017 at 8:11
  • but why do you think an input from the request that is supposed to be a file is an Eloquent Model? you are treating it as one, when you pass it to save because save takes a Model. Commented Dec 5, 2017 at 8:12
  • Then what am I suppose to do? I have been doing something like this for some time already but never got this kind of error. Sorry my eloquent isn't that good since I hardly use it Commented Dec 5, 2017 at 8:14
  • 1
    first of all, saving images to the DB is silly ... you know what is meant for serving files? your webserver not your database ... you need to stop what you are doing and start figuring out what all these variables actually are Commented Dec 5, 2017 at 8:35

1 Answer 1

2
public function test1(Request $request)
{
    // make new instance of Image Model
    $imageModel = new Image;
    // find personal_info Model by id
    $personal_info = personal_info::findOrFail($request->input('user_id'));

    // UploadedFile
    $image = $request->file('UserImage');

    // get the file contents?
    $imageModel->content = ...

    // save the relationships, pass a model instance to `save`
    $personal_info->Images()->save($imageModel);

    return redirect('/summary');
}
Sign up to request clarification or add additional context in comments.

5 Comments

So I am guessing this is not the preferable method? Since database is not for storing images
i would prefer to see these written to disk, but it kinda depends what your needs are my friend :)
Ohh I see actually I can already store inside disk where the image are inside the disk while inside the database it is the name and size of the file but because I am using another software which takes data from database I can't do that method. But anyways thank you so much for your :)
yes... since you have a need to put the actual content in the db, then you should do that :)
But anyways thank you for your help, have been trying to figure this out for 2 days :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.