Browse Source

getMetadata will return collections of user meta if key is null

pull/68/head
Nafies Luthfi 5 years ago
parent
commit
9f1fd6f9f2
  1. 11
      app/User.php
  2. 18
      tests/Unit/UserTest.php

11
app/User.php

@ -320,10 +320,19 @@ class User extends Authenticatable
return $this->hasMany(UserMetadata::class, 'user_id', 'id'); return $this->hasMany(UserMetadata::class, 'user_id', 'id');
} }
public function getMetadata($key)
public function getMetadata($key = null)
{ {
$metadata = $this->metadata; $metadata = $this->metadata;
if (is_null($key)) {
$metadataCollection = [];
foreach ($metadata as $metaKey => $metaValue) {
$metadataCollection[$metaKey] = $metaValue;
}
return collect($metadataCollection);
}
$meta = $metadata->filter(function ($meta) use ($key) { $meta = $metadata->filter(function ($meta) use ($key) {
return $meta->key == $key; return $meta->key == $key;
})->first(); })->first();

18
tests/Unit/UserTest.php

@ -141,6 +141,24 @@ class UserTest extends TestCase
} }
/** @test */ /** @test */
public function user_model_get_metadata_method_returns_all_metadata_if_key_is_null()
{
$user = factory(User::class)->create();
$this->assertEmpty($user->getMetadata());
DB::table('user_metadata')->insert([
'id' => Uuid::uuid4()->toString(),
'user_id' => $user->id,
'key' => 'cemetery_location_address',
'value' => 'Some address',
]);
$user = $user->fresh();
$this->assertCount(1, $user->getMetadata());
}
/** @test */
public function user_have_mother_link_method() public function user_have_mother_link_method()
{ {
$mother = factory(User::class)->create(); $mother = factory(User::class)->create();

Loading…
Cancel
Save