1

I'm trying to generate a query that returns a list of flight requests along with the user who requested them but I'm trying to omit the email attribute from the user, I know I can add email to the hidden array in the User model but I only want to hide it from this one query so it would not make sense to have to add the makeVisible method to every other query I'm running on my app.

The query I've got is this:

FlightRequest::get()
->where('public', 1)
->whereNull('acceptee_id')
->sortBy('id')
->load('aircraft', 'requestee');

I've tried adding ->makeHidden('requestee.email') to the end of the query but that doesnt work

hcphoon
  • 538
  • 5
  • 24
  • 1
    This is very poorly optimized... Your initial query should be `FlightRequest::with(['aircraft', 'requestee'])->where('public', 1)->whereNull('acceptee_id')->orderBy('id')->get()`. When you call `::get()`, you're loading every record from the DB into memory, then performing a bunch of additional filtering/sorting/loading, which is incredibly inefficient. – Tim Lewis Oct 09 '20 at 21:09

1 Answers1

1

Fixed by specifying the requestee 'columns' manually, ->load('aircraft', 'requestee:id,username,name')

hcphoon
  • 538
  • 5
  • 24
  • 1
    why are you not having the database do all the filtering and sorting instead of getting all the records then filtering them? and you can use `with` to eager load those relationships – lagbox Oct 09 '20 at 20:48