Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ArsenMK's avatar

laravel controller create if

i have productions/create , i need that when i save type_id = 1 save cut = 1 ,when type_id = 2 save grinding = 1 , type_id = 3 save polishing = 1, when type_id = 4 save hem = 1 (these four), I tried this

   DB::transaction(function () use ($prices, $request, $product) {
            Product::where('id', '=', (string) $product->id)->update(['status' =>0]);
if (Product::where($request->input('type_id'),'=',1)){
    Product::create([

        'worker_id' => $request['worker_id'],
        'price_id' => $request['price_id'],
        'cut' => 1,
        'type_id' => $request['type_id'],
        'storage_id' => $request['storage_id'],
        'length' => $product->length,
        'width' => $product->width,
        'area'=> $product->length * $product->width,



    ]);
} elseif (Product::where($request->input('type_id'),'=', 2)){
    Product::create([

        'worker_id' => $request['worker_id'],
        'price_id' => $request['price_id'],
        'type_id' => $request['type_id'],
        'grinding' => 1,
        'storage_id' => $request['storage_id'],
        'length' => $product->length,
        'width' => $product->width,
        'area'=> $product->length * $product->width,



    ]);
} elseif (Product::where($request->input('type_id'),'=',3)){
    Product::create([

        'worker_id' => $request['worker_id'],
        'price_id' => $request['price_id'],
        'type_id' => $request['type_id'],
        'polishing' => 1,
        'storage_id' => $request['storage_id'],
        'length' => $product->length,
        'width' => $product->width,
        'area'=> $product->length * $product->width,



    ]);
} elseif (Product::where($request->input('type_id'),'=',4)) {
    Product::create([

        'worker_id' => $request['worker_id'],
        'price_id' => $request['price_id'],
        'type_id' => $request['type_id'],
        'hem' => 1,
        'storage_id' => $request['storage_id'],
        'length' => $product->length,
        'width' => $product->width,
        'area' => $product->length * $product->width,



    ]);
}

        });

but it didn't work out, how can i do this

0 likes
29 replies
tykus's avatar
tykus
Best Answer
Level 104

I don't know what these queries are supposed to be doing; they're nonsense:

Product::where($request->input('type_id'),'=',1)
Product::where($request->input('type_id'),'=', '2')
Product::where($request->input('type_id'),'=', '3')
Product::where($request->input('type_id'),'=',4)

All of that duplicated code can be replaced with:

$map = [
  1 => 'cut',
  2 => 'grinding',
  3 => 'polishing',
  4 => 'hem'
];

Product::create(array_merge([
  'worker_id' => $request['worker_id'],
  'price_id' => $request['price_id'],
  'type_id' => $request['type_id'],
  'storage_id' => $request['storage_id'],
  'length' => $product->length,
  'width' => $product->width,
  'area' => $product->length * $product->width,
], [
  $map[$request['type_id']] => 1
]);
tykus's avatar

Only zeros where; in the status column, or every column?

Show me what your complete code looks like now.

tykus's avatar

@ArsenMK is 0 the default for those columns in the database table?

Show me what your complete code looks like now.

ArsenMK's avatar

@tykus

DB::transaction(function () use ($prices, $request, $product) {
           Product::where('id', '=', (string) $product->id)->update(['status' =>0]);
           $map = [
               1 => 'cut',
               2 => 'grinding',
               3 => 'polishing',
               4 => 'hem'
           ];

           Product::create(array_merge([
               'worker_id' => $request['worker_id'],
               'price_id' => $request['price_id'],
               'type_id' => $request['type_id'],
               'storage_id' => $request['storage_id'],
               'length' => $product->length,
               'width' => $product->width,
               'area' => $product->length * $product->width,
           ], [
               $map[$request['type_id']] => 1
           ]));

       });
ArsenMK's avatar

@tykus I don't understand, I'm sorry I don't understand English well)

ArsenMK's avatar

@tykus when type_id =1 $request['type_id'] = 1, when type_id =2 $request['type_id'] =2,when type_id =3 $request['type_id'] =3,when type_id =4$request['type_id'] =4

ArsenMK's avatar

@tykus i want when i choose type_id=1 cut becomes 1 when i choose type_id=2 grind becomes 1 when i choose type_id=3 polish becomes 1 when i choose type_id=4 edge becomes 1(these four)

tykus's avatar

@ArsenMK so then dd($map[$request['type_id']]) should return the appropriate value 'cut', 'grinding' etc?

ArsenMK's avatar

@tykus i need when i choose type_id=1 cut becomes 1 when i choose type_id=2 grind becomes 1 when i choose type_id=3 polish becomes 1 when i choose type_id=4 edge becomes 1(these four)

tykus's avatar

@ArsenMK what is the output:

DB::transaction(function () use ($prices, $request, $product) {
    Product::where('id', '=', (string) $product->id)->update(['status' =>0]);
    $map = [
        1 => 'cut',
        2 => 'grinding',
        3 => 'polishing',
        4 => 'hem'
    ];
    dd($map[$request['type_id']]); // add this
    Product::create(array_merge([
        'worker_id' => $request['worker_id'],
        'price_id' => $request['price_id'],
        'type_id' => $request['type_id'],
        'storage_id' => $request['storage_id'],
        'length' => $product->length,
        'width' => $product->width,
        'area' => $product->length * $product->width,
    ], [
        $map[$request['type_id']] => 1
    ]));
});
1 like
ArsenMK's avatar

@tykus when i select type_id 1 output is cut,when i select type_id 2 output is grindingwhen i select type_id 3 output is polishing,when i select type_id 4 output is hem

tykus's avatar

@ArsenMK because the type_id is 1; so cut should be a key in the resulting array below:

dd(array_merge([
        'worker_id' => $request['worker_id'],
        'price_id' => $request['price_id'],
        'type_id' => $request['type_id'],
        'storage_id' => $request['storage_id'],
        'length' => $product->length,
        'width' => $product->width,
        'area' => $product->length * $product->width,
    ], [
        $map[$request['type_id']] => 1
    ])
);
1 like
ArsenMK's avatar

@tykus when i select type_id 1 and press submit output is

array:8 [▼
  "worker_id" => "1"
  "price_id" => null
  "type_id" => "1"
  "storage_id" => "1"
  "length" => "100"
  "width" => "100"
  "area" => 10000
  "cut" => 1
]
tykus's avatar

@ArsenMK okay; so it should create the Product correctly with the cut column getting a value of 1 assuming cut, grinding etc all are fillable

ArsenMK's avatar

@tykus everything works correctly, now need to save value in the database under the appropriate section

tykus's avatar

@ArsenMK is it not saving the to the correct column now? Are those columns in mass-assignable (either in $fillable array, or model is unguarded)?

tykus's avatar

@ArsenMK make sure you are validating the Request - with this implementation you need to ensure the type_id is in [1, 2, 3, 4] to prevent any error!

1 like

Please or to participate in this conversation.