Laravel在同一功能中使用控制器?

时间:2017-07-17 14:01:04

标签: laravel laravel-5 eloquent

我正在从数据库中检索实体,但是我确实有用于链接用户和实体的数据透视表以及我想要检索用户的每个实体,问题是我不知道如何在一个数据库中使用一个集合。 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++;
        }

data dump dd($ entity)给出:

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 => "*"
  ]
}

1 个答案:

答案 0 :(得分:0)

实体对象和用户对象之间的关系是多对多的。你做不到

$entity->users->id

这是因为您正在尝试用户集合上的id属性。

试试这个:

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

要遍历您的用户,请执行以下操作:

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

    echo($user->id);

}