【问题标题】:learning laravel excel import function学习laravel excel导入功能
【发布时间】:2018-10-26 08:19:31
【问题描述】:

$titles = Item::lists('title')->toArray();

我收到错误“找不到类 'App\Http\Controllers\Item'”对不起,但我今天才开始使用 laravel :'(

我刚刚在stackoverflow中得到了这段代码:(

public function excel_import(Request $request){
        $file = Input::file('file');
        $file_name= $file->getClientOriginalName();
        $file->move('files',$file_name);
        $titles = Item::lists('title')->toArray();
        if($request->hasFile('file')){
        $path = $request->file('file')->getRealPath();
        $data = Excel::load('files/'.$file_name, function($reader) {
            $reader->all();
        })->get();
        if(!empty($data) && $data->count()){
            $insert = array();
            foreach ($data as $key => $value) {
                if (in_array($value->title, $titles))
                    continue;
                    $insert[] = array(
    'ke_y'   =>  $value['ke_y'], 
'en'     =>  $value['en'], 
       );
                    $titles[] = $value->title;

                }
            }
            if(!empty($insert)){
                DB::table('matrix_localization')->insert($insert);
                echo "all good. ";
            }
            else {
                echo "Please Check your file, Something is wrong there.";
            }
        }
        }

【问题讨论】:

  • 您好!如果您发布任何人都可以在他们的机器上执行的代码,它会帮助人们。这样就更容易为您提供帮助。见本站:minimal reproducible example
  • 在顶部添加导入语句:use Item;

标签: php excel laravel import


【解决方案1】:

首先你必须使用这个命令创建一个名为“Item”的模型

php artisan make:model Item

2。现在将此模型添加到您的控制器命名空间中

use App\Item;

现在您可以在控制器中的任何位置使用此 Item 类

【讨论】:

    猜你喜欢
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 2019-03-10
    相关资源
    最近更新 更多