0

I have three tables : "Users", "tickets" and "events". I want to get tickets of the users and the details of tickets is in events. I want to get data from all table using relationships.

i need user and ticket of user and event of that ticket

Also am i doing it properly?

I have made relations with all the tables.

table structure is:

users

-username, user_id

ticket_orders

-user_id , ticket_id, event_id

events

-event_id, event_name

For Users

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

For tickets

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

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

for events

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

I tried this in controller

here CreateEvent and Event is same. On my code i am using "CreateEvent" instead of "Event"

public function showMyTickets()
    {

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

this is what i got in dd($ticket) the data are in relations. now i dont know how to retrive it and am i doing it correctly.

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"
    }
  ]
}
TeachMe
  • 535
  • 1
  • 5
  • 16
  • you should use belongsToMany and you can access pivot https://stackoverflow.com/questions/27434338/laravel-get-pivot-data-for-specific-many-to-many-relation – Ahmed Aboud May 25 '19 at 05:20
  • If you want the tikets. Why not?: `$tickets = TicketOrder::where('user_id', $user->id)->with('events')->get();` – porloscerros Ψ May 25 '19 at 05:38
  • @porloscerrosΨ i want to get from users table not from tickets table – TeachMe May 25 '19 at 05:41
  • It seems odd that user_id , ticket_id, and event_id would be in one table ; this implies that tickets are unique to an event only. That's not necessarily wrong, but I don't think it's how I'd do it. – Strawberry May 25 '19 at 07:05
  • @Strawberry an ticket is unique to an event. a single ticket is linked to only a single event. so thats why. user_id has who ordered the ticket. ticket_id has its own unique id for ticket. event_id says ticket is of which event. – TeachMe May 25 '19 at 07:08
  • But you have no way of keeping track of ' orders ' - unless an order can only comprise: 1 ticket for 1 event OR multiple tickets for the same event OR all tickets purchased by a given user. – Strawberry May 25 '19 at 07:12
  • There will be many unique users and many unique events. What should i do then ? if i want to track of 'orders'. – TeachMe May 25 '19 at 07:16

0 Answers0