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

mstdmstd's avatar

How in export with maatwebsite excel add data from related table?

In Laravel 8 with "maatwebsite/excel": "^3.1" I make export of table row with features that some enum columns must have label instead of enum values like :

public function collection()
{
    $adsIdList = [7,1,5];
    return Ad
        ::getById($adsIdList)
        ->get()
        ->map(function ($adItem)  {
            $a = Carbon::createFromFormat('Y-m-d', $adItem->expire_date);
            $is_past = $a->isPast();
            $retAdItem = [
                'id'     => $adItem->id,                                // A
                'title'     => $adItem->title,                          // B
                'slug'    => $adItem->slug,                             // C
                'phone_display' => $adItem->phone_display ? 'Y' : 'N',  // D
                'has_locations' => $adItem->has_locations  ? 'Y' : 'N', // E
                'status' => Ad::getStatusLabel($adItem->status),        // F
                'price' => $adItem->price,                              // G
                'ad_type' => Ad::getAdTypeLabel($adItem->ad_type),      // H
                'expire_date' => $adItem->expire_date,                  // I
                'is_past' => $is_past,                                  // ?

also I add some calculative fields in my app/Exports/AdsExport.php file above.

But if there is a way to add data from related table? Any Ad can have several ad_locations with several fields... Later I will need to write import functionality to generated in export files...

Thanks!

0 likes
3 replies
mstdmstd's avatar

Thanks! With Using custom structures I got some packed array in excel cell? Could you please point 1) to methods I need to import these data into my db in several joined tables? 2) If there is a way to export/import image in this way?

mstdmstd's avatar

I try to read file content and keep it serialized in model attribute as :

                $adImages = AdImage
                    ::getByAdId($adItem->id)
                    ->get();
                foreach( $adImages as $next_key=>$nextAdImage ) {
                
                    $nextAdImageImgProps = AdImage::readAdImageProps($nextAdImage->ad_id, $nextAdImage->image, true);
                        $imagePath= storage_path() . '/app/public/' . $nextAdImageImgProps['image_path'];
                        $imageContent =   file($imagePath);
                        $adImages[$next_key]->setImageContentAttribute( serialize($imageContent) );
                    }


                }
                \Log::info(  varDump($adImages, ' -1 $adImages::') );

But looking at resulting $adImages I see that its rows has no imageContent field, which I defined in in app/Models/AdImage.php :

class AdImage extends Model
{

    protected $fillable = [ 'ad_id', 'image', 'main', 'info', 'imageContent'];

    protected $table = 'ad_images';
    protected $primaryKey = 'id';
    public $timestamps = false;

    protected $imageContent = '';

    protected $image_filename_max_length = 100;
//    protected $appends = ['imageContent']; // If to uncomment this line - the same result
    protected $attributes = array(
//        'imageContent' => '',   // If to uncomment this line - the same result
    );

    ...

    public function setImageContentAttribute($imageContent)
    {
        \Log::info(  varDump($imageContent, ' -1 setImageContentAttribute $imageContent::') );
         $this->imageContent= $imageContent;
    }

    public function getImageContentAttribute()
    {
        return $this->imageContent;
    }

Checking logs I see that method setImageContentAttribute is called with valid parameter ...

Did I define imageContent attribute in wrong way?

Please or to participate in this conversation.