jess8bit's avatar

Can't solve the error "Creating default object from empty value"

Hi guys, got stuck from quite a while on this.

I read many posts about this error but still cannot see what's wrong with this simple code.

The error is raised on the last line, where I'm setting an object attribute.

Spent hours on this... I need help :)

use App\Http\Controllers\Controller;
use App\CmsComponentSlot;
class EventHubManagerController extends Controller
{
    public function storeNewSlot(Request $request, EventHub $eventhub)
    {
            $ccs = new CmsComponentSlot;
            $css->event_hub_id=1;
    }
}

thanks!!

0 likes
8 replies
ejdelmonico's avatar

Where is the $request and the $eventhub being used? Does CmsComponentSlot have an event_hub_id property? You should really provide adequate code to determine the context of your code. You inject Request and EventHub but do include the appropriate use statements for starters.

jess8bit's avatar

Thanks ejdelmoci for taking the time to answer. I deliberately simplified the code here by assuming the rest of the context was not necessary (I narrowed the problem and concluded that the rest of the context was not necessary). But I'm probably wrong :) so let me add more verbose here:

routing

Route::post('/eventhubmanager/{eventhub}/cmsslot', EventHub\EventHubManagerController@storeNewSlot')

controller

use Illuminate\Http\Request;
use App\EventHub;
use App\Http\Controllers\Controller;
use App\CmsComponentSlot;
class EventHubManagerController extends Controller
{
    public function storeNewSlot(Request $request, EventHub $eventhub)
    {
            $ccs = new CmsComponentSlot;
            $css->event_hub_id=$eventhub->id;
    }
}

So,

  • $eventhub is injected via route auto-binding, but doesn't look like a culprit here, does it? (I check the value of $eventhub->id. Whatever the value is, I got the error.)
  • Seems like the error occurs during the mutation. If I dump the $ccs object right after the instanciation, I have an App/EventHub model. But as soon as I try to assign a value to an attribute, it breaks with this error.
  • The attribute event_hub_id exists on the model.
  • The error occurs on any other attributes I try.

that really confuses me. any clue ?

let me know if you need more code :) thanks!

ejdelmonico's avatar

When you hit the route, what is the exact error given to you? The errors are pretty concise in Laravel. Most tell you exactly what is wrong without snipe (fake animal) hunting. Also, are you trying to create the CmsComponentSlot with $eventhub->id? If so, I would think that your class should accept it in the constructor.

jess8bit's avatar

The route comes from an Ajax request btw; The only error I got is an error 500, at the first line (729) where I try to set a property of this model

    "message": "Creating default object from empty value",
    "exception": "ErrorException",
    "file": "\app\Http\Controllers\EventHub\EventHubManagerController.php",
    "line": 729,

I can try with any other property from that model, instead of event_hub_id, for example

$css->rank=2;

...same result, the mutation raises this error.

Another experimentation makes me think something's wrong with the model (but still cannot see what) : When I instantiate and mutate another model in the same method, it passes for that model, and stil not for CmsComponentSlot:

use Illuminate\Http\Request;
use App\EventHub;
use App\Http\Controllers\Controller;
use App\CmsComponentSlot;
use App\CmsComponentSlotDraft;
class EventHubManagerController extends Controller
{
    public function storeNewSlot(Request $request, EventHub $eventhub)
        {
                $ccsd = new CmsComponentSlotDraft;
                $ccsd->foo = 'bar' // this is ok
                $ccs = new CmsComponentSlot;
                $css->foo2 = 'bar2'; // error 500 "Create etc..."
        }
}

Crazy huh ? ^^

(no animal has been harmed)

ejdelmonico's avatar

It is a bit strange. Is there a constructor value for CmsComponentSlot? Have you tried dd() for both to see what is different?

jess8bit's avatar

Hey ejdelmonico, thanks again for your time.

There is no constructor for those models. Very basic classes.

Being in ajax context, dd() is not really my friend here, but I did die(var_dump($instance)); on each model to get a more readable response in the browser,

$instance = new Class();
die(var_dump($instance));

They both returns the exact same array:

object(App\CmsComponentSlot)#275 (26) {  // or CmsComponentSlotDraft
  ["connection":protected]=>
  NULL
  ["table":protected]=>
  NULL
  ["primaryKey":protected]=>
  string(2) "id"
  ["keyType":protected]=>
  string(3) "int"
  ["incrementing"]=>
  bool(true)
  ["with":protected]=>
  array(0) {
  }
  ["withCount":protected]=>
  array(0) {
  }
  ["perPage":protected]=>
  int(15)
  ["exists"]=>
  bool(false)
  ["wasRecentlyCreated"]=>
  bool(false)
  ["attributes":protected]=>
  array(0) {
  }
  ["original":protected]=>
  array(0) {
  }
  ["changes":protected]=>
  array(0) {
  }
  ["casts":protected]=>
  array(0) {
  }
  ["dates":protected]=>
  array(0) {
  }
  ["dateFormat":protected]=>
  NULL
  ["appends":protected]=>
  array(0) {
  }
  ["dispatchesEvents":protected]=>
  array(0) {
  }
  ["observables":protected]=>
  array(0) {
  }
  ["relations":protected]=>
  array(0) {
  }
  ["touches":protected]=>
  array(0) {
  }
  ["timestamps"]=>
  bool(true)
  ["hidden":protected]=>
  array(0) {
  }
  ["visible":protected]=>
  array(0) {
  }
  ["fillable":protected]=>
  array(0) {
  }
  ["guarded":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
}

Insane! It must something so big that I can't see it, as usual ...

jess8bit's avatar

FOUND IT!!! a f*****g syntax error on the instance name as I said, right here in front of my eyes

$ccs = new CmsComponentSlot; // CCS
$css->event_hub_id=1; // vs CSS

maaan, feel so sorry about that, taking your time on this one.

Thanks a lot for your help ejdelmonico, much appreciated anyways!

Please or to participate in this conversation.