app/Http/Controllers/ItemController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Item  $item
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, Item $item)
    {
        $request->validate([
            'name' => [
                'required',
                Rule::unique('items')->ignore($item->id)
            ],
            'code_no' => [
                'required',
                Rule::unique('items')->ignore($item->id)
            ],
            'price' => 'required|numeric|gt:0',
            'discount' => 'nullable|numeric|gt:0'
        ]);
        if($request->file()){
            $fileName = rand().'_'.$request->photo->getClientOriginalName();
            $filePath = $request->file('photo')->storeAs('itemimg',$fileName,'public');
            $path = '/storage/'.$filePath;
        }else{
            $path = '/storage/itemimg/item.png';
        }
        $item->code_no = $request->code_no;
        $item->name = $request->name;
        $item->photo = $path;
        $item->price = $request->price;
        $item->discount = $request->discount;
        $item->description = $request->description;
        $item->category_id = $request->category_id;
        $item->save();
        return redirect()->route('items.index');
    }
public function update ( Request $ request , Item $ item ) { $ request -> validate ( [ 'name' => [ 'required' , Rule :: unique ( 'items' ) -> ignore ( $ item -> id ) ] , 'code_no' => [ 'required' , Rule :: unique ( 'items' ) -> ignore ( $ item -> id ) ] , 'price' => 'required|numeric|gt:0' , 'discount' => 'nullable|numeric|gt:0' ] ) ; if ( $ request -> file ( ) ) { $ fileName = rand ( ) . '_' . $ request -> photo -> getClientOriginalName ( ) ; $ filePath = $ request -> file ( 'photo' ) -> storeAs ( 'itemimg' , $ fileName , 'public' ) ; $ path = '/storage/' . $ filePath ; } else { $ path = '/storage/itemimg/item.png' ; } $ item -> code_no = $ request -> code_no ; $ item -> name = $ request -> name ; $ item -> photo = $ path ; $ item -> price = $ request -> price ; $ item -> discount = $ request -> discount ; $ item -> description = $ request -> description ; $ item -> category_id = $ request -> category_id ; $ item -> save ( ) ; return redirect ( ) -> route ( 'items.index' ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
// dd($request); // validation // upload file // 1665003670_290012.png // itemimg/1665003670_290012.png // data update // redirect