Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SupunSam's avatar

Laravel Excel Export with HasOne Relationship

Hi All,

I am trying to export database data to excel using the following code in my export file:

return JobPreflight::wherePreflight('1')
                ->whereIn('status', ['Awaiting Comp', 'Cancelled'])
                ->wheremfg_site($usersite)
                ->with('job:id,job_key,type,job_no,due_out')
                ->get();

However, I only get the data from the Preflight table. Surprisingly this exact same code is working for my view when I create my data table.

Why I am not getting data from my hasone relationship.

0 likes
10 replies
bobbybouwmann's avatar

Because wherePreflight performs the query direct on the relationship. It's the same as this

JobPreflight::preflight()->where('id', 1)->get();

Well you get the idea. You don't want that. Instead, you want to do something like this

JobPreflight::with('preflight', function ($query) {
    return $query->where('id', 1);
})->get();

Anyway, your query seems to be doing a lot of things going from JobPreflight to preflight and also including job. It's a but a mess in my opinion.

1 like
SupunSam's avatar

Hi @bobbybouwmann..

Thank you very much for your valuable suggestion. I have one problem. I use this same code to get the data for my data table. That's why I used it for export as well.

bobbybouwmann's avatar

I understand that it works, but it will never return a JobPreflight model class because of the relationship. Because you use this code, doesn't mean it's correct ;)

Anyway, what are you trying to achieve? Maybe we can come up with the correct query.

1 like
SupunSam's avatar

I want to export all data from the JobPreflight model alongside with some data from the Job Model. JobPreflight model has a column called preflight and I'm trying to get where the boolean value is 1.

I found something that relates to Laravel Excel. But unable to get it right. I am using the "collection" method here. but in order to get data from relationships, I might have to use a "query" instead of a collection.

public function collection()
    {
        $usersite = Auth::user()->site;

            return JobPreflight::wherePreflight('1')
                ->whereIn('status', ['Awaiting Comp', 'In Progress', 'On Hold', 'Cancelled'])
                ->wheremfg_site($usersite)
                ->with('job:id,job_key,type,job_no,')
                ->get();
        
    }

I tried changing from collection to query. but it did not work. I am really bad with DB queries. maybe that's why.

SupunSam's avatar

I understand what you are saying @bobbybouwmann :-). I intended something to happen but this is not the way. Kindly help me to make this work.

SupunSam's avatar

These are my relationships:

Job

public function job()
    {
        return $this->hasOne(Job::class, 'id', 'job_id');
    }

Preflight

public function jobPreflight()
    {
        return $this->hasOne(JobPreflight::class);
    }
bobbybouwmann's avatar

I think you just need to get rid of wherePreflight('1')

1 like
SupunSam's avatar

Hi, I reviewed your previous answer. I think there is a misunderstanding. My JobPreflight Modal contains a column called preflgiht. It has a binary value. So I'm running this query to retrieve all the jobs where in preflight column value is "1".

As per your previous answer, it searches for id value of "1".

SupunSam's avatar
SupunSam
OP
Best Answer
Level 1

I was able to find the issue by myself. I am getting all the data I needed with my query. However, Laravel Excel doesn't know what to do with this retrieved relational data. Hence it only returns the data from the Preflight table.

To select and map the retrieved data as you need, you have to use "WithMapping" concern. then you can define the columns from the data retrieved from DB.

use Maatwebsite\Excel\Concerns\WithMapping;
public function map($preflight): array
    {
        return [
            $preflight->id,
            $preflight->job->job_key,
            $preflight->job->type,
            $preflight->job->job_no,
            $preflight->job->due_out,
            $preflight->cyc_time,
            $preflight->recieved_at,
            $preflight->completed_at,
            $preflight->fixed_at,
            $preflight->spt_user1,
            $preflight->spt_user2,
            $preflight->pending,
            $preflight->spt_time,
            $preflight->handover,
            $preflight->pf_issues,
            $preflight->lowres_dtp,
         ]; 
     }

I hope this helps anyone who is going through the same trouble.

1 like

Please or to participate in this conversation.