如何在请求数组中插入静态字段(Laravel 5.7)

时间:2019-02-25 09:40:39

标签: php laravel laravel-5 laravel-5.7

我在使用商店功能的某些小功能时遇到问题,似乎我在互联网上找不到任何适合我的正确答案。

我想在数组请求中添加静态数据,以便在发送数据时,数据透视表中选中的字段将自动填充一些整数。

我有下表:

Schema::create('mounter_evaluations', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('project_id')->index()->unsigned()->nullable();
    $table->text('mentions')->nullable();
    $table->softDeletes();
    $table->timestamps();
});  

Schema::create('mounter_procedures', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('mounter_norm_id')->index()->unsigned()->nullable();
    $table->string('detail');
    $table->softDeletes();
    $table->timestamps();
});

Schema::create('mounter_procedures_checks', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('mounter_evaluation_id')->unsinged()->nullable()->index();
    $table->integer('mounter_procedure_id')->unsinged()->nullable()->index();
    $table->integer('checked')->unsinged()->nullable();
    $table->timestamps();
});

以下型号:

public function mounterevaluation() {
    return $this->belongsToMany(
        'App\MounterEvaluation',
        'mounter_procedures_checks',
        'mounter_evaluation_id',
        'mounter_procedure_id'
    )->withPivot(['checked']);
}

public function mounterprocedures() {
    return $this->belongsToMany(
        'App\MounterProcedures',
        'mounter_procedures_checks',
        'mounter_evaluation_id',
        'mounter_procedure_id'
    )->withPivot(['checked']);
}

这是我的商店功能:

public function store(Request $request, $idProiect) {
    $evaluation = MounterEvaluation::where('project_id', $idProiect)->first();
    if (!$evaluation) {
        $evaluation = new MounterEvaluation();
        $evaluation->project_id = $idProiect;
    }
    $evaluation->mentions = $request->mentions;
    $evaluation->save();

    // $request->request->add(['checked' => 1]);
    // $evaluation->mounterprocedures()->sync($request->procedures);

    // $evaluation->mounterprocedures()->sync($request->all() + ['checked' => 1]);
    $evaluation->mounterprocedures()->sync($request->procedures);
    // $request->request->add(['checked' => 1]);
    $evaluation->mounterprocedures()->request->add(['checked' => 1]);
    return response()->json($evaluation, 201);
}

每次有人检查评估表中提交的某些文件时,该检查将存储在数据透视表中。

1 个答案:

答案 0 :(得分:0)

以简单的方式将其放入$request数组中,就像将值放入任何其他数组中一样:

$request['new_key1'] = 'new value 1';
$request['new_key2'] = 'new value 2';

这是我确定可以正常工作的方式,在您的控制器中,您已将Request实例作为参数传递,然后可以像下面这样正常添加值:

namespace App\Http\Controllers;
use Illuminate\Http\Request;

public function example(Request $request)
{
     $request['new_key1'] = 'new value 1';
     $request['new_key2'] = 'new value 2';
     dd($request->input('new_key1'));//new value 1
}

在您的情况下,如果$request->procedures是一个数组,则可以这样做:

$request->procedures = $request->procedures->toArray();
$request->procedures['checked'] = 1;
$evaluation->mounterprocedures()->sync($request->procedures);

我希望对您有帮助