DjangoとLaravel 比較（CSV）


Laravel

PostController.php 
```sh
    public function csvDownload() {
        $posts = Post::search()->get();

        $headers = [
            "Content-type" => "text/csv",
            "Content-Disposition" => "attachment; filename=file.csv"
        ];

        $callback = function() use($posts) {
            $handle = fopen('php://output', 'w');

          $columns = [
            'id',
            'name',
            'gender',
            'prefecture',
            'company',
            'jyob',
            'employmentstatus'
            ];

            mb_convert_variables('SJIS-win', 'UTF-8', $columns);

            fputcsv($handle, $columns);

            foreach($posts as $post) {
                $csv = [
                    $post->id,
                    $post->name,
                    $post->gender,
                    $post->prefecture,
                    $post->company,
                    $post->jyob,
                    $post->employmentstatus
                ];

                mb_convert_variables('SJIS-win', 'UTF-8', $csv);

                fputcsv($handle, $csv);
            }

            fclose($handle);
        };

        return response()->stream($callback, 200, $headers);

    }
```


app/Models/Post.php
```sh
    public function scopeSearch($query) {
    $request = request();
    
    $query->when($request->name, function($q, $name) {
        $q->where('name', 'LIKE', '%' . $name . '%');
    })
    ->when($request->gender, function($q, $gender) {
        $q->where('gender', 'LIKE', '%' . $gender . '%');
    })
    ->when($request->prefecture, function($q, $prefecture) {
        $q->where('prefecture', 'LIKE', '%' . $prefecture . '%');
    })
    ->when($request->company, function($q, $company) {
        $q->where('company', 'LIKE', '%' . $company . '%');
    })
    ->when($request->jyob, function($q, $jyob) {
        $q->where('jyob', 'LIKE', '%' . $jyob . '%');
    })
    ->when($request->employmentstatus, function($q, $employmentstatus) {
        $q->where('employmentstatus', 'LIKE', '%' . $employmentstatus . '%');
    });
```

以下django

views.py

```sh

from .models import Member
import csv

def csv_export(request):
	filename='export.csv'
	response = HttpResponse(content_type='text/csv;charset=utf_8_sig')
	response['Content-Disposition'] = "attachment;  filename='{}'; filename*=UTF-8''{}".format(filename, filename)

	w = csv.writer(response)
	w.writerow(['id','name','prefecture','gender','company','jyob','employmentstatus','stay','annual','affiliation','potion'])
	    
	data = Member.objects.all()
	for item in data:
		w.writerow([item.id, item.name, item.prefecture, item.gender, item.company, item.jyob, item.employmentstatus, item.stay, item.annual, item.affiliation,item.postion ])
	return response

```

models.py
models.pyはsetting.pyの内容と統一しました
※多分統一しなくても実装はできる模様

```sh

class Plofile(models.Model):
    name = models.CharField(verbose_name="氏名",max_length=100)
    prefecture  = models.CharField(verbose_name="出身",choices=settings.PREFECTURES,max_length=100)
    gender  = models.CharField(verbose_name="性別",choices=settings.GENDERS,max_length=100)
    company= models.CharField(verbose_name="業種",choices=settings.COMPANYS,max_length=100)
    jyob  = models.CharField(verbose_name="部署",choices=settings.JYOBS,max_length=100) 
    employmentstatus  = models.CharField(verbose_name="雇用",choices=settings.EMPLOYMENTSTATUSS,max_length=100)
    stay= models.CharField(verbose_name="住所",choices=settings.STAYS,max_length=100)
    annual  = models.CharField(verbose_name="年収",choices=settings.ANNUALS,max_length=100) 
    affiliation  = models.CharField(verbose_name="所属",choices=settings.AFFILIATONS,max_length=100)
    postion  = models.CharField(verbose_name="役職",choices=settings.POSITIONS,max_length=100)

```