【问题标题】:Laravel missing argument 2Laravel 缺少参数 2
【发布时间】:2014-04-08 05:20:01
【问题描述】:

缺少参数 2 Illuminate\Database\Eloquent\Model::setAttribute(),调用 /vagrant/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php 在第 2335 行并定义

上面我尝试上传一个 CSV 文件。

控制器:

 public function postUpload ()
{
    if ( Input::hasFile('file') )
    {
        DB::transaction(function()
        {
            // Clear out what we have written
            DB::table('wc_program')->delete();

            $csv = new CsvFile(Input::file('file')->getRealPath());

            // Get the csv headers and move to the next line (the start of actual data)
            $columns = $csv->getHeader();
            $csv->next();

            // Loop through the rows creating / saving a record for each
            while( $csv->valid() )
            {
                $row = $csv->current();
                $pc = new Programmes();

                for( $i=0; $i<count($columns); $i++ )
                {
                    $pc->$columns[$i] = $row[$i];
                }

                $pc->save();
                $csv->next();
            }
        });

    return Redirect::to('admin/programmes')->with('flash_success', 'Upload completed &amp; new data inserted.');
    }

然后我有一个非常基本的模型,没有什么像这样的主要模型:

 class Programmes extends Eloquent {

 protected $guarded = array('id');
//public static $rules = array();

 protected $table = 'wc_program_1';

 public $timestamps = false;
 }

路线设置如下:

Route::get('admin/programmes/excelUpload','ProgrammesController@excelUpload');
Route::post('admin/programmes/doUpload', ['as' => 'admin.programmes.doUpload', 'uses' => 'ProgrammesController@postUpload']);

【问题讨论】:

    标签: laravel-4


    【解决方案1】:

    代码 sn-p Illuminate\Database\Eloquent\Model::setAttribute() 需要两个参数,键和它的值。分配给列时,您的值似乎丢失了。

    我建议你在 at 附近调试你的代码

    $pc->$columns[$i] = $row[$i];
    

    如果您觉得有帮助,请更新您的问题 :)

    【讨论】:

      猜你喜欢
      • 2017-04-06
      • 2014-01-10
      • 2017-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 2020-01-22
      相关资源
      最近更新 更多