PHP code example of hulkur / laravel-hasmany-keyby

1. Go to this page and download the library: Download hulkur/laravel-hasmany-keyby library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hulkur / laravel-hasmany-keyby example snippets


$users = User::with('groups')->all()

$user->groups = [0 => $group];

class User extends Model
{
  use HasManyKeyByRelationship;

  public function groups()
  {
    return $this->hasMany(Group::class)->keyBy('id'); // can be string or callable
  }
}

$user->groups = [$group->id => $group];

@foreach($users as $user)
  @foreach($groups as $group)
    {{optional($user->accesslevels[$group->id])->level}}
  @endforeach
@endforeach