Why Can't I Update Fields in laravel?

Viewed 335

I have a table for my project & just have a problem in update fields. I am checking PUT or PATCH , But it didn't work. I deleted Foreign Key and Repeat Check but Problem not resolved. I use lavavel debugger and see right of request params for update Method. I Check request is OK but not sent to mysql. (mysql is running) (not have a error, but not update) plz help me. Tks.


edit.blade.php

<form action="{{ route('ProductCategory.update',$productCategory->id) }}" method="POST">
@csrf
@method('PATCH')

<input type="hidden" name="user_id" value="{{ $usr_id }}">
<input type="hidden" name="name" value="{{ $productCategory->name }}">
<div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12">
        <div class="form-group">
            <strong>نام:</strong>
            <input type="text" value="{{ $productCategory->name }}" class="form-control" disabled>
        </div>
    </div>

    <div class="col-xs-12 col-sm-12 col-md-6">
        <div class="form-group">
            <strong>ترتیب:</strong>
            <select name="order_id" class="form-control">
                @for($i = $cnt; $i >= 0; $i--)
                    <option value="{{$i+1}}" {{($productCategory->orderid == $i+1 ? "selected" : "")}}>{{$i+1}}</option>
                @endfor
            </select>
        </div>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-6">
        <div class="form-group">
            <input type="checkbox" name="shown" style="margin: 30px 8px 30px 10px; transform : scale(2);" value="{{$productCategory->shown == 1 ? '1' : '0'}}" {{ ($productCategory->shown == 1 ? "checked" : "") }} onclick="$(this).val(this.checked ? '1' : '0')">
            <strong>نمایش</strong>
        </div>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12 text-center">
        <button type="submit" class="btn btn-success">بروز رسانی</button>
    </div>
</div>

</form>

Model

class ProductCategory extends Model
{
    protected $table = 'product_category';

    protected $primaryKey = 'id';

    protected $fillable = [
        'name', 'user_id', 'shown', 'order_id'
    ];

    public $timestamps = true;
}

Controller

public function edit($id)
{
    $cnt = ProductCategory::count();
    $usr_id = Auth::id();
    $productCategory = ProductCategory::find($id);
    return view('ProductCategory.edit',compact('productCategory', 'cnt', 'usr_id'));
}

public function update(Request $request, ProductCategory $productCategory)
{
    $shown = $request['shown'];

    if (empty($shown)) {
        $request['shown'] = 0;
    }

    $productCategory->update($request->all());

    /*$productCategory->user_id = Auth::id();
    $productCategory->name = $request->name;
    $productCategory->shown = $request->shown;
    $productCategory->order_id = $request->order_id;

    $productCategory->save();*/

    /*DB::table('product_category')
        ->where('id', $request['id'])
        ->update(
            [
                'shown' => $request['shown'],
                'order_id' => $request['order_id'],
            ]
        );*/

    return redirect()->route('ProductCategory.index')
        ->with('success','رکورد با موفقیت بروزرسانی شد.');
}

Migrate

public function up()
{
    Schema::create('product_category', function (Blueprint $table) {
        $table->id();
        $table->foreignId('user_id')->constrained('users');
        $table->string('name', 100)->unique();
        $table->boolean('shown')->nullable()->default(true);
        $table->tinyInteger('order_id');
        $table->timestamps();
    });

web.config

    Route::resource('ProductCategory','ProductCategoryController');

dd($productCategory);

App\ProductCategory {#1401 
  #table: "product_category"
  #primaryKey: "id"
  #fillable: array:4 [▶]
  +timestamps: true
  #connection: null
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}
1 Answers

Please check whether checkbox value is passed as string or bool. If it is string, then accordingly need to change in migrate.

Related