lemiwinks's avatar

"Creating default object from empty value" error. Image upload

When I try to edit an image in the database by uploading a new image, I get the following error message: "Creating default object from empty value"

Controller functions


public function change($id, $journeyID)
    {
        $image = Image::find($id);
        return View::make('images.change')
            ->with(array(
                'image' => $image,
                'id' => $id,
                'journeyID' => $journeyID
            ));

    }

    public function save($id)
    {

        $rules = array(
            'image' => 'image'
        );
        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails()) {
            return Redirect::to('{journeyID}/images/{id}/change')
                ->withErrors($validator);

        } else {
            $image = Image::find($id);
            $image->imagesArray = $this->uploadImage();
            $image->save();

            return Redirect::to($image->journeyID . '/images');
        }

    }

 public function uploadImage()
    {
        // getting all of the post data
        $file = array('image' => Input::file('image'));
        // setting up rules
        $rules = array('image' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000
        // doing the validation, passing post data, rules and the messages
        $validator = Validator::make($file, $rules);
        if ($validator->fails()) {
            // send back to the page with the input data and errors
            Session::flash('message', 'Uploaded image was not valid. Stock image used instead.');
            return "abl/public_html/imagesArrayFolder/noImage.jpg";
        } else {
            // checking file is valid.
            if (Input::file('image')->isValid()) {
                $publicPath = public_path();  //this is a path shortcut for laravel 4 to get to the app/storage folder
                $destinationPath = "abl/public_html/imagesArrayFolder"; // upload path
                $originalName = Input::file('image')->getClientOriginalName(); //getting original name for easy access of file
                $fileName = date('Y_m_d-H_i_s') . '--' . $originalName; // renaming image
                Input::file('image')->move($destinationPath, $fileName); // uploading file to given path


                // return the image path to the store function
                return ("abl/public_html/imagesArrayFolder/" . $fileName);
            } else {
                // sending back with error message.
                Session::flash('message', 'Uploaded image was not valid. Stock image used instead.');
                return "abl/public_html/imagesArrayFolder/noImage.jpg";
            }
        }
    }

    public function saveImage($image)
    {
        $pictureArray = array();

        array_push($pictureArray, $image);
    }

Routes

Route::get('{journeyID}/images/{id}/change', 'ImagesController@change');
Route::post('{journeyID}/save/{id}', 'ImagesController@save');

View

    {{--{{ Form::model($image, array('url' => array($image->journeyID, '/images/', $image->id, 'save'), 'method' =>
    'PUT', 'files'=>true)) }}--}}
    {{--{{ Form::open(array('url' => 'images','files'=>true)) }}--}}
    {{Form::open(array('action' => array('ImagesController@save', $id, $journeyID),
     'method' => 'post', 'files'=>true )) }}

    {{ Form::hidden('journeyID', $journeyID)}}

<td> {{ Form::label('image', 'image') }} </td>
<td> {{ Form::file('image') }}

    {{ Form::submit('Change', array('class' => 'btn btn-primary')) }}
    {{ Form::close() }}

0 likes
5 replies
ritey's avatar

Do you get a file name & line number for the error?

bobbybouwmann's avatar

This will not work..

return Redirect::to('{journeyID}/images/{id}/change')
JarekTkaczyk's avatar

@lemiwinks Apparently this find doesn't find anything

            $image = Image::find($id);
            $image->imagesArray = $this->uploadImage();

so use findOrFail or manually check that you found the model.

JarekTkaczyk's avatar
Level 53

@lemiwinks I suppose you have wrong order of the ids there, change it to:

{{Form::open(array('action' => array('ImagesController@save', $journeyID, $id),
     'method' => 'post', 'files'=>true )) }}

Please or to participate in this conversation.