2017-07-17 100 views
-2

我正在从数据库检索实体,但是我确实有数据透视表链接用户和实体,并且对于每个实体我想要检索用户,问题是我不知道如何使用laravel控制器函数中的集合: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属性。

试试这个:通过您的用户

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

要循环,这样做:

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

    echo($user->id); 

} 
相关问题