Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maatwebsite multiple sheets mapping data show wrong[BUG] #2855

Closed
5 tasks
navaneetharajan opened this issue Sep 24, 2020 · 6 comments
Closed
5 tasks

maatwebsite multiple sheets mapping data show wrong[BUG] #2855

navaneetharajan opened this issue Sep 24, 2020 · 6 comments

Comments

@navaneetharajan
Copy link

navaneetharajan commented Sep 24, 2020

Prerequisites

Versions

  • PHP version: 7,2
  • Laravel version: 7
  • Package version: 3.1

Description

I am creating multiple sheet. First sheet data mapping is working good and next page data full wrong data mapped
Controller Code:

 $id=57;
   return (new BugExport($id))->download('test.xlsx');

Export Code:

<?php

namespace App\Exports;

use App\Model\bug;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class BugExport implements WithMultipleSheets
{
    use Exportable;
    /**
    * @return \Illuminate\Support\Collection
    */
    protected $id;
    public function __construct(int $id)
    {
        $this->id = $id;
    }
    public function sheets(): array
    {
        $sheets = [];
        $array_value=['Bugs','Bug Activities','Bug Comments'];
        $sheets[] = new Bugs($this->id, $array_value[0]);
         $sheets[] = new Bug_Activitie($this->id, $array_value[1]);

        return $sheets;
    }
  
}

Bug Activities Code:

<?php

namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromQuery;
use App\Model\bug;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;

class Bug_Activitie implements FromCollection,WithTitle,
ShouldAutoSize,WithHeadings,WithEvents
{

    private $id;
    private $sheet_name;
    private $i = 1;
    public function __construct(int $id, string $sheet_name)
    {
        $this->sheet_name = $sheet_name;
        $this->id  = $id;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
       return bug::query()->
        select('bugs.bug_id','users.user_name','bug_activities.activity','bug_activities.created_at')
        //->join('users', 'users.id', '=', 'bugs.assign_to')
        ->join('bug_activities', 'bug_activities.bug_id', '=', 'bugs.id')
        ->join('users', 'users.id', '=', 'bug_activities.user_id')
        ->where('bugs.project_id', $this->id);
    }

     /**
     * @return Builder
     */
    public function query()
    {
       
      
     //   return bug::query()->with('bug_activities')
  //     ->where('project_id',$this->id)->get();
  return bug::query()->
        select('bugs.bug_id','users.user_name','bug_activities.activity','bug_activities.created_at')
        //->join('users', 'users.id', '=', 'bugs.assign_to')
        ->join('bug_activities', 'bug_activities.bug_id', '=', 'bugs.id')
        ->join('users', 'users.id', '=', 'bug_activities.user_id')
        ->where('bugs.project_id', $this->id);
       //echo "<pre>1";print_r($test);die;
            
    }

    public function title(): string
    {
        return  $this->sheet_name;
    }

    public function map($bug): array
    {
       // echo 1;die;

        echo "<pre>Navin";print_r($bug);die;
        return [

            !empty($bug->bug_activities[$this->i]->id)?$bug->bug_activities[$this->i]->id:'',
            !empty($bug->bug_id)?$bug->bug_id:'',
            !empty($bug->bug_activities[$this->i]->activity)?$bug->bug_activities[$this->i]->activity:'',
           !empty($bug->bug_activities[$this->i]->created_at)?date('M-d-Y',strtotime($bug->bug_activities[$this->i]->created_at)):'',
           // date('M-d-Y',strt otime($bug->created_at)),
           
        ];
        $this->i++;
    }


    public function headings(): array
    {
        return [
            //'#',
            'Bug Id',
            'User Name',
            'Activities',
            'Created Date'

        ];
    }

    
}

Steps to Reproduce

Expected behavior:

Array
(
[0] => Array
(
[id] => 8
[bug_id] => BK-931062
[user_id] => 37
[activity] => The Bug is Open on BK-931062
[created_at] => 2020-07-17T17:38:06.000000Z
[updated_at] => 2020-07-17T17:38:06.000000Z
[status] => 1
[user_name] => Naveen
)

[1] => Array
    (
        [id] => 9
        [bug_id] => BK-397174
        [user_id] => 39
        [activity] => The Bug is Open on BK-397174
        [created_at] => 2020-07-17T17:46:45.000000Z
        [updated_at] => 2020-07-17T17:46:45.000000Z
        [status] => 1
        [user_name] => Demo Naveen
    )

[2] => Array
    (
        [id] => 12
        [bug_id] => BK-556095
        [user_id] => 37
        [activity] => The Bug is Open on BK-556095
        [created_at] => 2020-07-17T18:06:46.000000Z
        [updated_at] => 2020-07-17T18:06:46.000000Z
        [status] => 1
        [user_name] => Naveen
    )

[3] => Array
    (
        [id] => 13
        [bug_id] => BK-556095
        [user_id] => 37
        [activity] => The Bug is Reassign to Naveen
        [created_at] => 2020-07-17T18:07:50.000000Z
        [updated_at] => 2020-07-17T18:07:50.000000Z
        [status] => 1
        [user_name] => Naveen
    )

[4] => Array
    (
        [id] => 16
        [bug_id] => BK-731281
        [user_id] => 39
        [activity] => The Bug is Open on BK-731281
        [created_at] => 2020-07-17T18:13:51.000000Z
        [updated_at] => 2020-07-17T18:13:51.000000Z
        [status] => 1
        [user_name] => Demo Naveen
    )

[5] => Array
    (
        [id] => 17
        [bug_id] => BK-731281
        [user_id] => 37
        [activity] => The Bug Status Changed into In Progress
        [created_at] => 2020-07-17T18:20:29.000000Z
        [updated_at] => 2020-07-17T18:20:29.000000Z
        [status] => 1
        [user_name] => Naveen
    )

[6] => Array
    (
        [id] => 18
        [bug_id] => BK-731281
        [user_id] => 37
        [activity] => The Bug is Reassign to Demo Naveen
        [created_at] => 2020-07-17T18:21:04.000000Z
        [updated_at] => 2020-07-17T18:21:04.000000Z
        [status] => 1
        [user_name] => Naveen
    )

[7] => Array
    (
        [id] => 19
        [bug_id] => BK-731281
        [user_id] => 39
        [activity] => The Bug is Reassign to Naveen
        [created_at] => 2020-07-17T18:32:26.000000Z
        [updated_at] => 2020-07-17T18:32:26.000000Z
        [status] => 1
        [user_name] => Demo Naveen
    )

[8] => Array
    (
        [id] => 20
        [bug_id] => BK-731281
        [user_id] => 39
        [activity] => The Bug is Reassign to Demo Naveen
        [created_at] => 2020-07-17T18:33:53.000000Z
        [updated_at] => 2020-07-17T18:33:53.000000Z
        [status] => 1
        [user_name] => Demo Naveen
    )

)
This data need to mapping in bug_activities

Actual behavior:

nApp\Model\bug Object
(
[table:protected] => bugs
[dates:protected] => Array
(
[0] => created_at
[1] => updated_at
)

[connection:protected] => mysql
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
    (
    )

[withCount:protected] => Array
    (
    )

[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] => 
[attributes:protected] => Array
    (
        [bug_id] => BK-931062
        [user_name] => Naveen
        [activity] => The Bug is Open on BK-931062
        [created_at] => 2020-07-17 17:38:06
    )

[original:protected] => Array
    (
        [bug_id] => BK-931062
        [user_name] => Naveen
        [activity] => The Bug is Open on BK-931062
        [created_at] => 2020-07-17 17:38:06
    )

[changes:protected] => Array
    (
    )

[casts:protected] => Array
    (
    )

[classCastCache:protected] => Array
    (
    )

[dateFormat:protected] => 
[appends:protected] => Array
    (
    )

[dispatchesEvents:protected] => Array
    (
    )

[observables:protected] => Array
    (
    )

[relations:protected] => Array
    (
    )

[touches:protected] => Array
    (
    )

[timestamps] => 1
[hidden:protected] => Array
    (
    )

[visible:protected] => Array
    (
    )

[fillable:protected] => Array
    (
    )

[guarded:protected] => Array
    (
        [0] => *
    )

)

This export page query return correct values but mapping show irrelevant data

Additional Information

NA

@patrickbrouwers
Copy link
Member

I don't understand the issue. You are getting an Eloquent Bug model in the map method, that's expected behaviour... ?

@navaneetharajan
Copy link
Author

navaneetharajan commented Sep 24, 2020

@patrickbrouwers. The issue is The query is returning correct in FromCollection method and mapping also came same value right?
expected behavior is my query return value. Actual behavior is mapping function return value

@patrickbrouwers
Copy link
Member

patrickbrouwers commented Sep 24, 2020

in collection() you are selecting columns which means it won't return an eloquent model, if you use query() it will return an eloquent model. Choose one or the two that fits your needs, but I don't see any bug.
Also in collection() you should call ->get() yourself.

@navaneetharajan
Copy link
Author

@patrickbrouwers collection() i call get() function still mapping irreverent record.

@patrickbrouwers
Copy link
Member

I don't know then, if you think it really is a bug in our package, please provide a minimal reproduction situation in a failing unit test or example repository that I can run on my computer.

@stale stale bot added the stale label May 30, 2021
@stale
Copy link

stale bot commented May 31, 2021

This bug report has been automatically closed because it has not had recent activity. If this is still an active bug, please comment to reopen. Thank you for your contributions.

@stale stale bot closed this as completed May 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants