Laravel-excel: ํด๋กœ์ € ์ง๋ ฌํ™” ์‹คํŒจ: 'Illuminate\Http\UploadedFile' ์ง๋ ฌํ™”๊ฐ€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2017๋…„ 06์›” 13์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: Maatwebsite/Laravel-Excel

[์˜๋ฌธ]

์›น์‚ฌ์ดํŠธ/์—‘์…€ 2.1, ๋ผ๋ผ๋ฒจ 5.4

์•ˆ๋…•, ์–˜๋“ค์•„. Excel ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค๋ ค๊ณ  ํ•˜๋Š”๋ฐ "๋‹ซ๊ธฐ ์ง๋ ฌํ™” ์‹คํŒจ: 'Illuminate\Http\UploadedFile' ์ง๋ ฌํ™”๊ฐ€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค"๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ช‡ ๊ฐ€์ง€ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์‹œ๋„ํ–ˆ์ง€๋งŒ ์ด ๋ฌธ์ œ๊ฐ€ ๊ณ„์† ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ๋ฅผ ์ดํ•ดํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ Excel ํŒŒ์ผ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ์†Œ์Šค ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

if($request->hasFile('file')){
     $file = $request->file('file')->getRealPath();
     \Excel::filter('chunk')->load($file)->chunk(200, function($result) use ($request){
        foreach ($result as $item) {
           if(collect($item)->has('title') && collect($item)->has('category_id')  && collect($item)->has('file')){
                Content::create([
                     'title' => $item->title,
                     'category_id' => $item->category_id,
                     'file' => $item->file,
                     'reference' => $request->input('reference')
                ]);
           }
        }
       });
      return redirect()->back();
}

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋„๋ก ๋„์™€์ฃผ์„ธ์š”.

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

$request ๋ฅผ ํด๋กœ์ €์— ์ „๋‹ฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ง๋ ฌํ™”๋˜๊ณ  ๋Œ€๊ธฐ์—ด์— ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ช…์‹œ์  ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ด ์‹œ๋„:

if($request->hasFile('file')){
     $file = $request->file('file')->getRealPath();
    $reference = $request->input('reference');
     \Excel::filter('chunk')->load($file)->chunk(200, function($result) use ($reference){
        foreach ($result as $item) {
           if(collect($item)->has('title') && collect($item)->has('category_id')  && collect($item)->has('file')){
                Content::create([
                     'title' => $item->title,
                     'category_id' => $item->category_id,
                     'file' => $item->file,
                     'reference' => $reference
                ]);
           }
        }
       });
      return redirect()->back();
}

๋ชจ๋“  3 ๋Œ“๊ธ€

$request ๋ฅผ ํด๋กœ์ €์— ์ „๋‹ฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ง๋ ฌํ™”๋˜๊ณ  ๋Œ€๊ธฐ์—ด์— ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ช…์‹œ์  ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ด ์‹œ๋„:

if($request->hasFile('file')){
     $file = $request->file('file')->getRealPath();
    $reference = $request->input('reference');
     \Excel::filter('chunk')->load($file)->chunk(200, function($result) use ($reference){
        foreach ($result as $item) {
           if(collect($item)->has('title') && collect($item)->has('category_id')  && collect($item)->has('file')){
                Content::create([
                     'title' => $item->title,
                     'category_id' => $item->category_id,
                     'file' => $item->file,
                     'reference' => $reference
                ]);
           }
        }
       });
      return redirect()->back();
}

@patrickbrowers ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์€ ๋‚ด ๋ผ์ด๋ธŒ๋ฅผ ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค :+1: .

@patrickbrowers ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰