lundi 25 février 2019

How to insert static field in request array(Laravel 5.7)

This is the first time I am asking a question on StackOverflow. I am having an issue with some small feature of a store function and it seems that I didn't found any proper answer on the internet that will work for me.

I want to add static data in the array request so that when sent data the field checked in the pivot table to be automatically filled with some integer.

I have the following tables:

    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();
    });

The following models:

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']);
}

Here is my store function:

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);
}

I've already tried some methods found online, but it seems that didn't work for me. Something I am clearly doing wrong or I don't know, maybe I don't understand how this works.

The thing is I want that every time a person checks some filed in evaluation form this check to be stored on the pivot table. Any idea how can I do this?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Sp4TBS
via IFTTT

Aucun commentaire:

Enregistrer un commentaire