如何将用户ID传入foreach循环?

时间:2016-11-06 18:04:23

标签: laravel authentication foreach import maatwebsite-excel

对于PHP和Laravel来说,我是一个新手,我很尴尬,我还没有想到这一点。我正在尝试为我的用户提供一个选项,将他们的数据库导入应用程序。但是,我需要将用户ID附加到每一行,以便可以与该用户一起保存。我已尝试多次尝试获取该用户ID并将其传递到foreach循环,以便可以保存。我能收到任何指导,我将非常感激。我正在使用Maatwebsite / Laravel-Excel外观。

这是我的控制器:     

class ImportController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function importExcel()
    {
        if(Input::hasFile('import_file')){
            $path = Input::file('import_file')->getRealPath();
            $data = Excel::load($path, function($reader) {
            })->get();
            if(!empty($data) && $data->count()){
                foreach ($data as $key => $value) {
                    $user = Auth::user();
                    $data['user_id'] = $user->id;
                    $insert[] = [
                        'user_id' => $value->user_id,
                        'first_name' => $value->first_name,
                        'last_name' => $value->last_name,
                        'title' => $value->title,
                        'level' => $value->level,
                        'company' => $value->company,
                        'email' => $value->email,
                        'address_1' => $value->address_1,
                        'address_2' => $value->address_2,
                        'city' => $value->city,
                        'state' => $value->state,
                        'zip_code' => $value->zip_code,
                        'office_tel' => $value->office_tel,
                        'mobile_tel' => $value->mobile_tel,
                        'member_since'=> $value->member_since
                    ];
                }
                if(!empty($insert)){
                    DB::table('members')->insert($insert);
                    Session::flash('flash_message', 'Database successfully   imported!');
                }
            }
        }
        return back();
    }
}

这是我的路线:

 Route::post('importExcel', 'ImportController@importExcel');

以下是我的观点:

<button type="button" class="btn btn-danger btn-lg" data-toggle="modal" data-target="#importExcel">Import</button>

<div class="modal fade" id="importExcel" tabindex="-1" aria-labelledby="importExcelLabel">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">Import Your Database</h4>
            </div>
            <div class="modal-body">
                <p>Click browse to import your database. Only Microsoft Excel extensions are acceptable. Please label your columns as follows:</p>
                    <ul>
                        <li>user_id (leave this column empty)</li>
                        <li>first_name</li>
                        <li>last_name</li>
                        <li>title</li>
                        <li>level</li>
                        <li>company</li>
                        <li>address_1</li>
                        <li>address_2</li>
                        <li>city</li>
                        <li>state</li>
                        <li>zip_code</li>
                        <li>office_tel</li>
                        <li>mobile_tel</li>
                        <li>member_since</li>
                    </ul>

                <form action="{{ URL::to('importExcel') }}" class="form-horizontal" method="post" enctype="multipart/form-data">
                    <input type="file" name="import_file" />
                    <input type="hidden" name="_token" value="{{csrf_token()}}">
                    <button type="submit" class="btn btn-primary">Import File</button>
                </form>
            </div><!-- /.modal-body-->
            <div class="modal-footer">
                <button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Close</button>
            </div><!-- /.modal-footer-->
        </div><!-- /.modal-content-->
    </div><!-- /.modal-dialog-->
</div><!-- /.modal-->

这是我的模特:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Member extends Model
{
    protected $fillable = [
        'user_id',
        'first_name',
        'last_name',
        'title',
        'level',
        'company',
        'email',
        'address_1',
        'address_2',
        'city',
        'state',
        'zip_code',
        'office_tel',
        'mobile_tel',
        'member_since' ];

}

1 个答案:

答案 0 :(得分:0)

首先:你在哪里设置$insert?您必须使用要导入的数据设置此变量。尽管如此,您可以尝试以下方法:

如果我理解正确,数据库字段user_id应该包含登录用户的ID - 如果是这样,请在控制器中尝试importExcel中的以下内容(请参阅注释):

public function importExcel(Request $request)
    {
        if(Input::hasFile('import_file')){
            $path = Input::file('import_file')->getRealPath();
            $data = Excel::load($path, function($reader) {
            })->get();
            if(!empty($data) && $data->count()){
                foreach ($data as $key => $value) {
                    $user = Auth::user();
                    $data['user_id'] = $user->id;
                    [
                        'user_id' => $user->id, // Access here the userId of the logged in user
                        'first_name' => $value->first_name,
                        'last_name' => $value->last_name,
                        'title' => $value->title,
                        'level' => $value->level,
                        'company' => $value->company,
                        'email' => $value->email,
                        'address_1' => $value->address_1,
                        'address_2' => $value->address_2,
                        'city' => $value->city,
                        'state' => $value->state,
                        'zip_code' => $value->zip_code,
                        'office_tel' => $value->office_tel,
                        'mobile_tel' => $value->mobile_tel,
                        'member_since'=> $value->member_since
                    ];
                }
                if(!empty($insert)){
                    DB::table('members')->insert($insert);
                    Session::flash('flash_message', 'Database successfully   imported!');
                }
            }
        }
        return back();
    }

希望有所帮助:)