2017-07-17 5 views
-2

データベースからエンティティを取得していますが、ユーザーとエンティティをリンクするピボットテーブルがあり、エンティティごとにユーザーを取得したい場合、問題はラーバブコントローラ機能内でコレクションを使用する方法がわかりません:Laravelは同じ機能のコントローラを使用していますか?

public function handle() 
    { 
     $dir = "public/ical/*"; 
     foreach(glob($dir) as $file) 
     { 
     $entity_id = preg_match("/\d/", $file); 
     $entity = Entity::where('id', $entity_id) 
      ->with('users') 
      ->first(); 
     dd($entity->users->id); //I want to retrieve that. 
     $icals = new ICal($file, array(
      'defaultSpan' => 2, 
      'defaultTimeZone' => 'UTC', 
      'defaultWeekStart' => 'MO', 
      'skipRecurrence' => false, 
      'useTimeZoneWithRRules' => false, 
      )); 
     $time_period = array(
      'interval' => true, 
      ); 
     if ($time_period['interval']) { 
      $events = $icals->eventsFromInterval('2 week'); 
      $count = 1; 
     } 
     foreach ($events as $event) { 
         $dtstart = $icals->iCalDateToDateTime($event->dtstart); 
         $dtend = $icals->iCalDateToDateTime($event->dtend); 
         $dtstart_tz = $icals->iCalDateToDateTime($event->dtstart_tz); 
         $dtend_tz = $icals->iCalDateToDateTime($event->dtend_tz); 
         $database_event = new Event; 
         $database_event->type = "External Event"; 
         $database_event->startdate = $dtstart->format('d-m-Y'); 
         $database_event->endate = $dtend->format('d-m-Y'); 
         $database_event->startime = $dtstart_tz->format('H:i'); 
         $database_event->endtime = $dtend_tz->format('H:i'); 
         $database_event->title = $event->summary; 
         $database_event->description = $event->description ; 
         $database_event->location = $event->location; 
         $database_event->entity_id = $entity_id; 
         $database_event->save(); 
         $this->info("Event Retrieved"); 
         $count++; 
     } 

データダンプDD($エンティティ)が得られます。

App\Entity {#620 
    #table: "entities" 
    +timestamps: true 
    #connection: "mysql" 
    #primaryKey: "id" 
    #keyType: "int" 
    +incrementing: true 
    #with: [] 
    #withCount: [] 
    #perPage: 15 
    +exists: true 
    +wasRecentlyCreated: false 
    #attributes: array:16 [ 
    "id" => 1 
    "name" => "dasd" 
    "type" => "dsa" 
    "email" => "adas" 
    "logo" => null 
    "twitter_business" => null 
    "facebook_business" => null 
    "instagram_business" => null 
    "google_places" => null 
    "ical" => null 
    "slug" => null 
    "api_key" => null 
    "geoloc_id" => null 
    "deleted" => 0 
    "created_at" => null 
    "updated_at" => null 
    ] 
    #original: array:16 [ 
    "id" => 1 
    "name" => "dasd" 
    "type" => "dsa" 
    "email" => "adas" 
    "logo" => null 
    "twitter_business" => null 
    "facebook_business" => null 
    "instagram_business" => null 
    "google_places" => null 
    "ical" => null 
    "slug" => null 
    "api_key" => null 
    "geoloc_id" => null 
    "deleted" => 0 
    "created_at" => null 
    "updated_at" => null 
    ] 
    #casts: [] 
    #dates: [] 
    #dateFormat: null 
    #appends: [] 
    #events: [] 
    #observables: [] 
    #relations: array:1 [ 
    "users" => Illuminate\Database\Eloquent\Collection {#622 
     #items: array:2 [ 
     0 => App\Entity {#630 
      #table: "entities" 
      +timestamps: true 
      #connection: "mysql" 
      #primaryKey: "id" 
      #keyType: "int" 
      +incrementing: true 
      #with: [] 
      #withCount: [] 
      #perPage: 15 
      +exists: true 
      +wasRecentlyCreated: false 
      #attributes: array:16 [ 
      "id" => 1 
      "name" => "dasd" 
      "type" => "dsa" 
      "email" => "adas" 
      "logo" => null 
      "twitter_business" => null 
      "facebook_business" => null 
      "instagram_business" => null 
      "google_places" => null 
      "ical" => null 
      "slug" => null 
      "api_key" => null 
      "geoloc_id" => null 
      "deleted" => 0 
      "created_at" => null 
      "updated_at" => null 
      ] 
      #original: array:18 [ 
      "id" => 1 
      "name" => "dasd" 
      "type" => "dsa" 
      "email" => "adas" 
      "logo" => null 
      "twitter_business" => null 
      "facebook_business" => null 
      "instagram_business" => null 
      "google_places" => null 
      "ical" => null 
      "slug" => null 
      "api_key" => null 
      "geoloc_id" => null 
      "deleted" => 0 
      "created_at" => null 
      "updated_at" => null 
      "pivot_user_id" => 1 
      "pivot_entity_id" => 1 
      ] 
      #casts: [] 
      #dates: [] 
      #dateFormat: null 
      #appends: [] 
      #events: [] 
      #observables: [] 
      #relations: array:1 [ 
      "pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#629 
       +parent: App\Entity {#597 
       #table: "entities" 
       +timestamps: true 
       #connection: null 
       #primaryKey: "id" 
       #keyType: "int" 
       +incrementing: true 
       #with: [] 
       #withCount: [] 
       #perPage: 15 
       +exists: false 
       +wasRecentlyCreated: false 
       #attributes: [] 
       #original: [] 
       #casts: [] 
       #dates: [] 
       #dateFormat: null 
       #appends: [] 
       #events: [] 
       #observables: [] 
       #relations: [] 
       #touches: [] 
       #hidden: [] 
       #visible: [] 
       #fillable: [] 
       #guarded: array:1 [ 
        0 => "*" 
       ] 
       } 
       #foreignKey: "user_id" 
       #relatedKey: "entity_id" 
       #guarded: [] 
       #connection: null 
       #table: "entity_user" 
       #primaryKey: "id" 
       #keyType: "int" 
       +incrementing: true 
       #with: [] 
       #withCount: [] 
       #perPage: 15 
       +exists: true 
       +wasRecentlyCreated: false 
       #attributes: array:2 [ 
       "user_id" => 1 
       "entity_id" => 1 
       ] 
       #original: array:2 [ 
       "user_id" => 1 
       "entity_id" => 1 
       ] 
       #casts: [] 
       #dates: [] 
       #dateFormat: null 
       #appends: [] 
       #events: [] 
       #observables: [] 
       #relations: [] 
       #touches: [] 
       +timestamps: false 
       #hidden: [] 
       #visible: [] 
       #fillable: [] 
      } 
      ] 
      #touches: [] 
      #hidden: [] 
      #visible: [] 
      #fillable: [] 
      #guarded: array:1 [ 
      0 => "*" 
      ] 
     } 
     1 => App\Entity {#633 
      #table: "entities" 
      +timestamps: true 
      #connection: "mysql" 
      #primaryKey: "id" 
      #keyType: "int" 
      +incrementing: true 
      #with: [] 
      #withCount: [] 
      #perPage: 15 
      +exists: true 
      +wasRecentlyCreated: false 
      #attributes: array:16 [ 
      "id" => 2 
      "name" => "ddasd" 
      "type" => "fsdf" 
      "email" => "fsdf" 
      "logo" => null 
      "twitter_business" => null 
      "facebook_business" => null 
      "instagram_business" => null 
      "google_places" => null 
      "ical" => null 
      "slug" => null 
      "api_key" => null 
      "geoloc_id" => null 
      "deleted" => 0 
      "created_at" => null 
      "updated_at" => null 
      ] 
      #original: array:18 [ 
      "id" => 2 
      "name" => "ddasd" 
      "type" => "fsdf" 
      "email" => "fsdf" 
      "logo" => null 
      "twitter_business" => null 
      "facebook_business" => null 
      "instagram_business" => null 
      "google_places" => null 
      "ical" => null 
      "slug" => null 
      "api_key" => null 
      "geoloc_id" => null 
      "deleted" => 0 
      "created_at" => null 
      "updated_at" => null 
      "pivot_user_id" => 1 
      "pivot_entity_id" => 2 
      ] 
      #casts: [] 
      #dates: [] 
      #dateFormat: null 
      #appends: [] 
      #events: [] 
      #observables: [] 
      #relations: array:1 [ 
      "pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#625 
       +parent: App\Entity {#597} 
       #foreignKey: "user_id" 
       #relatedKey: "entity_id" 
       #guarded: [] 
       #connection: null 
       #table: "entity_user" 
       #primaryKey: "id" 
       #keyType: "int" 
       +incrementing: true 
       #with: [] 
       #withCount: [] 
       #perPage: 15 
       +exists: true 
       +wasRecentlyCreated: false 
       #attributes: array:2 [ 
       "user_id" => 1 
       "entity_id" => 2 
       ] 
       #original: array:2 [ 
       "user_id" => 1 
       "entity_id" => 2 
       ] 
       #casts: [] 
       #dates: [] 
       #dateFormat: null 
       #appends: [] 
       #events: [] 
       #observables: [] 
       #relations: [] 
       #touches: [] 
       +timestamps: false 
       #hidden: [] 
       #visible: [] 
       #fillable: [] 
      } 
      ] 
      #touches: [] 
      #hidden: [] 
      #visible: [] 
      #fillable: [] 
      #guarded: array:1 [ 
      0 => "*" 
      ] 
     } 
     ] 
    } 
    ] 
    #touches: [] 
    #hidden: [] 
    #visible: [] 
    #fillable: [] 
    #guarded: array:1 [ 
    0 => "*" 
    ] 
} 

答えて

0

エンティティオブジェクトとユーザオブジェクト間の関係は、多くの多くのです。あなたはできません

$entity->users->id 

これは、ユーザーの集まりでidプロパティを試行しているためです。

このお試しください:

foreach ($entity->users as $user) 
{ 

    echo($user->id); 

} 
:これを行う、ユーザーをループする

$entity->users->first()->id 

関連する問題