0

my post is too long but pls help me !

I tried to export data from database to CSV file, but I got 2 problems:

1. Update: I solved it by changing CSV to XLSX.

2. How can I get title_name in eloquent ? I tried but it did not work.

$title_japan = TitleJapan::select('id','name',$this->title->name)->get();

Here is my TitleJapan model:

    class TitleJapan extends Model
{
    public function title()
    {
        return $this->belongsTo('App\Title');
    }
}

Here is my Title model:

    class Title extends Model
{
    public function titleJapan()
    {
        return $this->hasMany('App\TitleJapan');
    }

}

And my controller:

    class ExportController extends Controller
{
    public function index()
    {
        return view('Export.index');
    }

    public function exportTitleCSV()
    {
        $title = Title::all();
        $title_japan = TitleJapan::select('id','name',$this->title->name)->get();

        return Excel::create('Filename', function($excel) use ($title,$title_japan,$title_oversea) {

            $excel->setTitle('TitleBandai');
            $excel->sheet('FirstSheet', function($sheet) use($title_japan) {
                $sheet->fromArray($title_japan);
            });
            $excel->sheet('SecondSheet', function($sheet) use($title_oversea) {
                $sheet->fromArray($title_oversea);
            });

        })->export('csv');
    }
}

File CSV I got

enter image description here

File CSV I want

enter image description here

3
  • First I would try exporting as a xls or xlsx A csv is just a text file and will not understand multiple sheets Commented Jul 28, 2017 at 8:32
  • csv files can't have sheets. That's excel only Commented Jul 28, 2017 at 8:33
  • Thanks a lot you guys, i solved it ! Commented Jul 28, 2017 at 8:37

1 Answer 1

1

A .csv file is very simple. It's just some data separated by commas or some other delimiter. Therefore, it cannot have multiple sheets. If you export as .xlsx it should work.

For the name of the title, i suggest you eager load the title and then process the result into an array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.