I have several models set up in a Grandparent -> Parent -> Child
relationship, where each has a one-to-many relationship with the one beneath it. The backend is set up through migrations using the default created_at
and modified_at
dates, relevant as that is what I'm trying to grab records by.
I currently have (names replaced):
$parent->child->where('created_at', '=', $date);
Through echo'ing it out I can verify that $date
is correct when the where function is run, and matches records in the database. My expectation based on documentation is that this should return a Collection. When I attempt to iterate through it to get each instance of the model inside, however, I get errors saying I'm attempting to access properties of a non-object. (Just trying to echo out the created_at
date).
I also tried putting ->first()
after the original, with the assumption that it would spit out the first child as an object, that matched those pre-requisites, however it comes out null despite the collection not showing as null when similarly tested.
My question is, is there a way to get the first()
approach to work, returning a single instance of the model? Similar to:
$parent->child->where('created_at', '=', $date)->first();
This is my first time posting a question so if you need more info just ask.