简体   繁体   中英

I want to edit record then update it using laravel

I am trying to edit record when I click Edit Button record is not fetching into input field using laravel

Does anyone have any idea How can I fix it? I am facing an error

Property [product_name] does not exist on this collection instance. (View: /home/sigmadel/public_html/resources/views/admin/product_update.blade.php)

Controller

public function update_projects($products_id){
    $category=DB::table('category')->get();
    $projects=DB::table('projects')->where('products_id','=',$products_id)->get();
    return view('admin/product_update',compact('category','projects'));
}

html view

<form   action="{{route('product.action')}}"   method="post"    >
    @csrf
    <div class="group-form">
        <select  class="form-control">
            <option > Update Category</option>
            @foreach($category as $categories)
            <option value="">{{$categories->category_name}}</option>
            @endforeach
        </select>
    </div>
    <br>
    <div class="group-form">
        <input type="text" class="form-control" value="{{$projects->product_name}}"
               placeholder="Update Product Name" name="product_name" >
    </div>
    <br>
    <div class="group-form">
        <input type="file" class="form-control"  name="select_file" >
    </div>
    <br>
    <div class="group-form">
        <input type="submit" class=" btn btn-primary form-control" value="UPDATE"  name="Update" >
    </div>
</form>
<a class="btn btn-success" href="{{route('product.update',$projects->products_id)}}">Edit</a>

Route

Route::get('view_projects','AdminController@view_projects');
Route::get('product_update/{products_id}/edit','AdminController@update_projects')->name('product.update');

you are doing wrong all along. you are getting collection as you are using get() but trying to use it as object. do it this way.

route:

Route::get('project_edit/{id}/edit','AdminController@edit_project')- 
     >name('project.edit');
Route::post('project_update/{id}/update','AdminController@update_project')- 
     >name('project.update');

controller edit function:

public function edit_project($id){           
    $categories=DB::table('category')->get();
    $project=DB::table('projects')->where('products_id','=',$id)->first();
    return view('admin/product_update',compact('categories','project'));    
}

edit blade:

<form action="{{route('project.update', $project->id)}}" method="POST" enctype="multipart/form-data">
    @csrf
    <div class="group-form">
        <select  class="form-control">
            <option value="">Select Category</option>
            @foreach($categories as $category)
            <option value="{{$category->id}}" @if($project->category_id) == $category->id) selected @endif>{{$category->category_name}}</option>       
            @endforeach
        </select>
    </div>
    <br>
    <div class="group-form">
        <input type="text" class="form-control" value="{{$project->product_name}}" 
             placeholder="Update Product Name" name="product_name" >  
    </div>
    <br>
    <div class="group-form">
        <input type="file" class="form-control"  name="select_file" >      
    </div>
    <br>
    <div class="group-form">
        <input type="submit" class=" btn btn-primary form-control" value="UPDATE"  name="Update" >  
    </div>
</form>

and finally the update function in controller:

public function update_project(Request $request, $id){           
    //validation and update process    
}

here project and product got mixed badly so change it according to your structure.

edit blade:

<form action="{{route('project.update', $project->id)}}" method="POST" enctype="multipart/form-data">
    @csrf
    <div class="group-form">
        <select  class="form-control">
            <option value="">Select Category</option>
            @foreach($categories as $category)
            <option value="{{$category->id}}" @if($project->category_id) == $category->id) selected @endif>{{$category->category_name}}</option>       
            @endforeach
        </select>
    </div>
    <br>
    <div class="group-form">
        <input type="text" class="form-control" value="{{$project->product_name}}" 
             placeholder="Update Product Name" name="product_name" >  
    </div>
    <br>
    <div class="group-form">
        <input type="file" class="form-control"  name="select_file" >      
    </div>
    <br>
    <div class="group-form">
        <input type="submit" class=" btn btn-primary form-control" value="UPDATE"  name="Update" >  
    </div>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM