PHP 5.5 : how to convert array with 1 element to property of parent -


i have this:

$foo = foo::getfoobyid(100);  public static function getfoobyid($id) {     return foo::where('id', $id)->with('locations')->firstorfail(); } 

this returns eloquent collection.

$foo gives following properties:

$foo->name $foo->locations 

$foo->locations array 1 element

just want:

$foo->location = $foo->locations[0]; unset($foo->locations); 

this code works, terrible, because happens on different places. how can in clear way?

you haven't shown it, assume you've defined foo having one-to-many relationship locations.

if there single location given foo, should define one-to-one relationship rather one-to-many, you'll able $foo->location single location object rather $foo->locations array of them.

in other words, guess have code in foo model looks this:

public function locations() {     return $this->hasmany('locations'); } 

you need replace more this:

public function location() {     return $this->hasone('locations'); } 

then change with('locations') in query function with('location').


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -