Thursday, March 30, 2017

Laravel - Unable to override the create method for a model

When overriding the base Laravel model create method, the application fails. No errors are sent back to the browser, and the server logs are empty. The strange thing, it works just fine without the override. Not sure what I'm doing wrong.

Simplified controller function:

public function save(Request $request, $id = 0) {
    $myModel = MyModel::find($id);
    $data = $request->all();

    if (!$myModel) // Create a new row
    {
        $myModel = MyModel::create($data);
    }

    // ...
}

This works fine until I add this function to the model:

class MyModel extends Model
{
    // ...

    public static function create($attributes = [])
    {
        return parent::create($attributes);
    }

    // ...
}

Just to clarify, I'm not looking for a different way to implement this. I just need to know why the parent call is failing for me.

Here's some server info...

  • Server OS: Windows 7 (personal PC with XAMPP)
  • Laravel version 5.4.15
  • PHP version 5.6.30


via Siphon

Advertisement