Hi, i have an endpoint that will receive the id of a database model in the body of the request. I am using the findOrFail method like this
$employment =Employment::findOrFail($attributes['employment_id']);
but i only need this model to access a related model of a related model so i do not need all the fields, what query should i do to make it all more eficient?
What have you tried already? If we know the relationships you're trying to improve upon then we might be able to give you a better answet than your vague question warrants.
The employment model is related to another model called service, the service is related to the service_timetable and the information i need is a field called weekly_hours which is in the service timetable table.
i have done this:
Regarding query optimization, while Laravel offers a fluent API for accessing relationships, it's not mandatory to always use it. Query builders exist for a reason; you can craft your query in SQL and then translate it into a query builder. In this case, I would imaging querying the timetable table directly by providing the related model's ID, selecting only what you need and limiting the result to 1 seems like the way to go approach.