如何从第一张表中获取第三张表和第二张表的数据?

时间:2019-05-25 05:09:59

标签: php mysql laravel

我有三个表:“用户”,“门票”和“事件”。我想获取用户的票证,而票证的详细信息正在发生。我想使用关系从所有表中获取数据。

我需要用户和用户票证以及该票证的事件

我也做得正确吗?

我已经与所有桌子建立了联系。

表结构为:

用户

-用户名,用户名

机票订单

-user_id,ticket_id,event_id

事件

-event_id,event_name

  

对于用户

    public function ticketOrder(){
        return $this->hasMany('App\TicketOrder', 'user_id', 'user_id');
    }
  

对于门票

public function user(){
       return $this->belongsTo('App\User', 'user_id', 'user_id');
   }

   public function events(){
       return $this->belongsTo('App\Event', 'event_id', 'event_id');
   }
  

事件

 public function ticketOrder(){
        return $this->hasMany('App\TicketOrder', 'event_id', 'event_id');
    }
  

我在控制器中尝试过

     

此处CreateEvent和Event是相同的。在我的代码中,我使用的是“ CreateEvent”而不是“ Event”

public function showMyTickets()
    {

        $user = Auth::user();
        $ticket= $user->with('TicketOrder','TicketOrder.CreateEvents')->where('user_id',$user->user_id)->get();
        dd($ticket);
    }
  

这就是我在dd($ ticket)中得到的   数据是相关的。现在我不知道该如何找回并且我做对了。

Collection {#1164 ▼
  #items: array:1 [▼
    0 => User {#970 ▼
      #fillable: array:6 [▶]
      #hidden: array:3 [▶]
      #casts: array:1 [▶]
      #connection: "mysql"
      #table: "users"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:11 [▶]
      #original: array:11 [▶]
      #changes: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "TicketOrder" => Collection {#1163 ▼
          #items: array:1 [▼
            0 => TicketOrder {#1068 ▼
              #fillable: array:8 [▶]
              #hidden: array:8 [▶]
              #connection: "mysql"
              #table: "ticket_orders"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:11 [▶]
              #original: array:11 [▶]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: array:1 [▼
                "CreateEvents" => CreateEvent {#1155 ▼
                  #fillable: array:22 [▶]
                  #hidden: array:1 [▶]
                  #connection: "mysql"
                  #table: "create_events"
                  #primaryKey: "id"
                  #keyType: "int"
                  +incrementing: true
                  #with: []
                  #withCount: []
                  #perPage: 15
                  +exists: true
                  +wasRecentlyCreated: false
                  #attributes: array:26 [▶]
                  #original: array:26 [▶]
                  #changes: []
                  #casts: []
                  #dates: []
                  #dateFormat: null
                  #appends: []
                  #dispatchesEvents: []
                  #observables: []
                  #relations: []
                  #touches: []
                  +timestamps: true
                  #visible: []
                  #guarded: array:1 [▶]
                }
              ]
              #touches: []
              +timestamps: true
              #visible: []
              #guarded: array:1 [▶]
            }
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [▶]
      #rememberTokenName: "remember_token"
    }
  ]
}

0 个答案:

没有答案
相关问题