0

So I have a query that I only want to get specific columns in a relation but is not working. I'm using Laravel 5.2 by the way. Here's what I have:

$job = Job::query()->whereId($job_id)
        ->with([
            'jobType' => function (Relation $query) {
                $query->select(['name']);
            },

        ])
    ->first();

If I do that, the jobType relationship returns null as seen below:

job type relation

And if I'll remove the $query->select(['name']);, it has the data from job_type table. How can I just successfully get specific column from a table?

Eem Jee
  • 1,239
  • 5
  • 30
  • 64

1 Answers1

1

Maybe This can.. To get the specific column you need a specific jobtype

$job = Job::query()->whereId($job_id)
        ->with(['jobType' => function ($q) use($jobType) {
                $q->where(// check the condition on jobtype table);
                $q->select(['name']);
            },
        ])
    ->first();
void
  • 915
  • 8
  • 20
  • I think where clause is unnecessary, I have let it worked using the inclusion of the `id` column in the select clause. – Eem Jee May 19 '20 at 11:35