0

Good day everyone. I'm quite new to new to Laravel and was doing some basic CRUD coding, I was able to code the add and view function, but I'm having a hard time with the edit and delete function.

I have 4 files, the master blade file, the web.php (routing), the blade file and the Form Controller.

This is the promo.blade.php file:

<table class="table table-striped" id="table1" >
    <thead>
        <tr>
            <th class="text-center">ACTION</th>
            <th class="text-center">Offer ID</th>
            <th class="text-center">Promo Name</th>
            <th class="text-center">Promo Price</th>
            <th class="text-center">Status</th>
        </tr>    
    </thead>
    <tbody>
        @foreach ($data as $key => $item)
            <tr>
            <td class="text-center">
                <a href="#" data-bs-toggle="modal" data-bs-target="#show" data-myofferid="{{$item->offerId}}" data-mytitle="{{$item->promoName}}" data-myprice="{{$item->promoPrice}}">
                    <span class="badge bg-success"><i class="bi bi-eye-fill"></i></span>
                </a> 
                
                <a href="#" data-bs-toggle="modal" data-bs-target="#edit" data-mainid="{{$item->id}}"  data-myofferid="{{$item->offerId}}" data-mytitle="{{$item->promoName}}" data-myprice="{{$item->promoPrice}}">
                    <span class="badge bg-primary"><i class="bi bi-pencil-square"></i></span>
                </a>  

                <a href="#" data-bs-toggle="modal" data-bs-target="#delete" data-myofferid="{{$item->offerId}}" data-mytitle="{{$item->promoName}}" data-myprice="{{$item->promoPrice}}">
                    <span class="badge bg-danger"><i class="bi bi-trash"></i></span>
                </a>  
            </td>
                <td class="date text-center">{{ $item->offerId }}</td>
                <td class="date text-center">{{ $item->promoName }}</td>
                <td class="number text-center">{{ $item->promoPrice}}</td>
                <td class="number text-center">{{ $item->isActive }}</td>
            </tr>
        @endforeach
    </tbody>
</table>
<!--Start Modal Edit -->
<div class="modal fade text-left" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel160" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable" role="document">
        <div class="modal-content">
            <div class="modal-header bg-primary">
                <h5 class="modal-title white" id="add">
                    Edit Promo
                </h5>

                <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                    <i data-feather="x"></i>
                </button>
            </div>
                <div class="modal-body">

                    <form action="{{ route('promo.edit') }}" method="POST">
                        @csrf
                        
                        <div class="form-group">
                            <label for="">ID</label>
                            <input type="text" class="form-control" name="id" id="id" value="">
                            <span style="color:red">@error('id'){{$message}} @enderror</span>
                        </div>        
                        
                        <div class="form-group">
                            <label for="">Offer ID</label>
                            <input type="text" class="form-control" name="offerId" id="offerId" value="">
                            <span style="color:red">@error('offerId'){{$message}} @enderror</span>
                        </div>        
                        
                        <div class="form-group">
                            <label for="">Promo Name</label>
                            <input type="text" class="form-control" name="promoName" id="promoName" value="">
                            <span style="color:red">@error('promoName'){{$message}} @enderror</span>
                        </div>        

                        <div class="form-group">
                            <label for="">Promo Price</label>
                            <input type="number" class="form-control" name="promoPrice" id="promoPrice" value="">
                            <span style="color:red">@error('promoPrice'){{$message}} @enderror</span>
                        </div>                                           
                </div>
                        
                <div class="modal-footer">
                    <button type="button" class="btn btn-light-secondary" data-bs-dismiss="modal">
                        <i class="bx bx-x d-block d-sm-none"></i>
                            <span class="d-none d-sm-block">CANCEL</span>
                    </button>
                    <button type="submit" class="btn btn-primary ml-1">
                        <i class="bx bx-check d-block d-sm-none"></i>
                            <span class="d-none d-sm-block">SAVE</span>
                    </button>
                </div>
                    </form> 
            </div>
        </div>
    </div>
</div>
<!-- End Modal Edit-->

Then this is the web.php file, for the routing:

Route::get('promo.promo', [App\Http\Controllers\FormControllerPromo::class, 'viewRecord'])->middleware('auth')->name('promo.promo');
Route::post('promo.add', [App\Http\Controllers\FormControllerPromo::class, 'addPromo'])->name('promo.add');
Route::post('promo.delete/{id}', [App\Http\Controllers\FormControllerPromo::class, 'viewDelete'])->middleware('auth');

Route::get('promo.edit', [App\Http\Controllers\FormControllerPromo::class, 'viewRecord'])->middleware('auth')->name('promo.edit');
Route::get('promo.edit/{id}', [App\Http\Controllers\FormControllerPromo::class, 'viewDetail'])->middleware('auth');
Route::post('promo.edit', [App\Http\Controllers\FormControllerPromo::class, 'edit'])->name('promo.edit');

This is the master.blade.php file:

<!--Start Modal edit for Promo-->
    <script type="text/javascript">
        $('#edit').on('show.bs.modal', function (event){

            var button = $(event.relatedTarget)
            var mainid = button.data('mainid')
            var id = button.data('myofferid')
            var title = button.data('mytitle')
            var price = button.data('myprice')

            var modal = $(this)
            modal.find('.modal-body #id').val(mainid);
            modal.find('.modal-body #offerId').val(id);
            modal.find('.modal-body #promoName').val(title);
            modal.find('.modal-body #promoPrice').val(price);
        })
    </script>
<!--End Modal edit for Promo-->

I think this is the part where the code wont execute properly. This is the FormControllerPromo.php file:

    // view form
    public function index()
    {
        return view('promo.promo');
    }

    // view record
    public function viewRecord()
    {
        $data = DB::table('promo')->get();
        return view('promo.promo',compact('data'));
    }

    // view detail
    public function viewDetail($id)
    {
        $data = DB::table('promo')->where('id',$id)->get();
        return view('promo.promo',compact('data')); 
    }

    // edit promo
    public function edit(Request $request){
        $id = $request->input('id');
        $offerId = $request->input('offerId');
        $promoName = $request->input('promoName');
        $promoPrice = $request->input('promoPrice');

        DB::table('promo')
        ->where('id', $id)  // find your user by their email
        ->limit(1)  // optional - to ensure only one record is updated.
        ->update(array('offerId' => $offerId, 'promoName' => $promoName, 'promoPrice' => $promoPrice));  // update the record in the DB. 

        $data = DB::table('promo');
        return view('promo.promo',compact('data'));
    }

I've been trying to code this for almost a week now with no success, any help is highly appreciated. :)

6
  • You need to include your problem. What is not working the way you are expecting? And what error are you getting? Commented Jan 4, 2022 at 8:43
  • im sorry if i didnt include, the problem is that it wont execute the update statement and will give me an error Undefined property: Illuminate\Database\MySqlConnection::$offerId Commented Jan 4, 2022 at 8:47
  • updated the FormController file to what I'm coding now.. but still gives an error :( Commented Jan 4, 2022 at 8:50
  • I guess you should remove ->limit(1) because it will query records. If you have where on id you should always get one item, because ids should be unique. Commented Jan 4, 2022 at 8:51
  • 1
    it's been a while since I worked with laravel, but if I remember correctly (hence why this is a comment and not an answer), you still have to get the record you want to update either by doing ->get() or ->first() at the end of your query, only then can you update them ( ->first()->update()) Commented Jan 4, 2022 at 9:34

1 Answer 1

1

the update seems right, should work. but when you pass the $data variable to your view, you should call ->get(), because otherwise you return a query builder instance, that later raises the Undefined property error when trying to access {{$item->offerId}}.

change second last line in your example

//from this:
$data = DB::table('promo');

//to this:
$data = DB::table('promo')->get();

//or to this if you want to show only one record:
$data = DB::table('promo')->where('id', $id)->get();
Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much for this, it finally worked! :)
cant believe all those weeks of struggle just for 1 line of code lmao

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.