【问题标题】:$request->input returns null with Laravel 5.5$request->input 在 Laravel 5.5 中返回 null
【发布时间】:2018-04-24 03:18:25
【问题描述】:

我创建了一个允许发布多个语言的表单。定义语言后,我希望用户创建一个默认的“扇区”并保存它...

我有 2 张桌子:

sectors
    id
    admin_id
    created_at
    updated_at
    deleted_at

langs
    id
    langname_fr
    langname
    ....

还有一个包含我可以填写的字段的数据透视表:

category_lang
    lang_id
    category_id
    catname
    catshortname
    ....

我创建了一个视图:

{!! Form::open( array('route' => 'setup.sectorCreate','method' => 'POST') ) !!}
<fieldset>
    <legend>Nom du secteur</legend>

    @foreach($langs as $lang)

        <div class="form-group m-form__group">
            {{ Form::label( 'Nom du secteur en ' . $lang->langname_fr) }}
            {{ Form::text('sectname_lang_' . $lang->id, 'Nom du secteur en ' . $lang->langname_fr, [ 'class' => 'form-control m-input' ]) }}
        </div>
        <div class="form-group m-form__group">
            {{ Form::label( 'Nom abrégé du secteur en ' . $lang->langname_fr ) }}
            {{ Form::text('sectshortname_lang_' . $lang->id, 'Nom abrégé du secteur en ' . $lang->langname_fr, [ 'class' => 'form-control m-input' ]) }}
        </div>
    @endforeach
</fieldset>

<fieldset>
    <legend>Description du secteur</legend>

    @foreach($langs as $lang)
        <div class="form-group m-form__group">
            {{ Form::label( $lang->langname_fr ) }}
            {{ Form::text('sectdescription_lang_' . $lang->id, 'Description du secteur en ' . $lang->langname_fr, [ 'class' => 'form-control m-input' ]) }}
        </div>
        <div class="form-group m-form__group">
            {{ Form::text('sectshortdescription_lang_' . $lang->id, 'Description courte du secteur en ' . $lang->langname_fr, [ 'class' => 'form-control m-input' ]) }}
        </div>
        <br>
    @endforeach
</fieldset>

<div class="col-xs-12 col-sm-12 col-md-12 text-center">
    <button type="submit" class="btn btn-primary">Valider</button>
</div>
{!! Form::close() !!}

我还创建了一个方法来保存数据...我必须先创建一个扇区,然后将必须插入数据透视表的字段值附加到数据透视表中(使用方法 attach()):

public function sectorCreate(Request $request) {
    // 1. Create a sector
    $sector = Sector::create(array(
        'admin_id' => Auth::guard('admin')->user()->id,
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now(),
     ));

    $sector = Sector::all()->last();
    // 2. getting the ID
    $sector_id = Sector::all()->last()->id;

    // 3. counting published langs
    $countLang = Lang::count();

    // 4. Getting the datas and inserting into pivot table
    for ($i = 1; $i < $countLang; $i++) {
        $sector[$i] = $sector->langs()->attach(
            ['sector_id' => $sector_id],
            ['lang_id' => $i],
            ['sectname' => $request->input('sectname_lang_' .$i)],
            ['sectshortname' => $request->input('sectshortname_lang_' .$i)],
            ['sectdescription' => $request->input('sectdescription_lang_' .$i)],
            ['sectshortdescription' => $request->input('sectshortdescription_lang_' .$i)],
            ['created_at' => Carbon::now()],
            ['updated_at' => Carbon::now()],
            ['deleted_at' => NULL]
        );
    }

    $sector = Sector::with('langs')->get();
    return dd($sector);
    //
    //return redirect()->route('admin.home')->with('success', 'it works');

}

不幸的是,我在插入数据时遇到问题......

 #original: array:18 [▼
     "id" => 1
     "langname_fr" => "Français"
     "langname" => "Français"
     "langisocode" => "fr-FR"
     "langshortcode" => "fr"
     "ismainlanguage" => 1
     "created_at" => "2018-04-23 15:09:19"
     "updated_at" => "2018-04-23 15:10:07"
     "deleted_at" => null
     "pivot_sector_id" => 2
     "pivot_lang_id" => 1
     "pivot_sectname" => null
     "pivot_sectshortname" => null
     "pivot_sectdescription" => null
     "pivot_sectshortdescription" => null
     "pivot_created_at" => "2018-04-23 15:47:14"
     "pivot_updated_at" => "2018-04-23 15:47:14"
     "pivot_deleted_at" => null
 ]

pivot_sectname、pivot_sectshortname、pivot_sectdescription、pivot_sectshortdescription 返回“null” 谢谢...

【问题讨论】:

    标签: crud laravel-5.5


    【解决方案1】:

    通过几项研究...我对 $sector_id 进行了附件... 我将值传递给其他字段的数组...

        for ($i = 1; $i <= $countLang; $i++) {
            $insertSector[$i] = $sector->langs()->attach(
                $sector_id,
                [
                    'lang_id' => $i,
                    'sectname' => $request->input('sectname_lang_' .$i),
                    'sectshortname' => $request->input('sectshortname_lang_' .$i),
                    'sectdescription' => $request->input('sectdescription_lang_' .$i),
                    'sectshortdescription' => $request->input('sectshortdescription_lang_' .$i),
                    'created_at' => Carbon::now(),
                    'updated_at' => Carbon::now(),
                    'deleted_at' => NULL
                ]
            );
        }
    

    【讨论】:

      猜你喜欢
      • 2022-01-27
      • 2018-03-08
      • 2018-04-21
      • 1970-01-01
      • 2022-06-15
      • 2016-11-08
      • 2017-11-19
      • 2019-08-11
      • 1970-01-01
      相关资源
      最近更新 更多