php - Laravel Where Issue, Maybe with Pagination?
Get the solution ↓↓↓Solution:
You're searching for the parent Doctor record within the original Collection of$doctors
returned by the pagination query. If the parent Doctor isn't in the current page of records, your "query" result will be null.
The problem: You have 100 Doctors, with IDs #1-100. You're fetching the first page, so$doctors
will contain Doctors #1-10. (Simple example with no searching or anything, just standard pagination.)
Doctor #2 has anffs_id
value of39
. Because you're searching the$doctors
collection for a Doctor with ID #39, it won't be found, because$doctors
only contains #1-10.
So, you're 100% correct in that the issue is with the contents of$doctors
.
Solution: Create an Eloquent relationship method for this Doctor-to-Doctor relationship. Something like this (adjusted to your app's needs):
public function ffs()
{
return $this->belongsTo(\App\Doctor::class);
}
Use this relationship to retrieve and reference the parent Doctor within your results (bonus sugar here using to remove the
@if
check from the view):
<td>
{{ optional($doctor->ffs)->full_name }}
</td>
This will directly query the database for the related record, instead of using the$doctors
collection.
At this point, we'll run into something called the N+1 problem, where every parent Doctor request will be an individual database query. You can fix this using one of the Eager Loading methods.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: can't write image data to path
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.