今天是個好日子。我是 Laravel 的新手,正在做一些基本的 CRUD 編碼,我能夠撰寫添加和查看功能的代碼,但是我在編輯和洗掉功能方面遇到了困難。我有 4 個檔案,主刀片檔案、web.php(路由)、刀片檔案和表單控制器。
這是 promo.blade.php 檔案:
<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 hljs-string">"><i hljs-string">"></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 hljs-string">"><i hljs-string">"></i></span>
</a>
</td>
<td hljs-string">">{{ $item->offerId }}</td>
<td hljs-string">">{{ $item->promoName }}</td>
<td hljs-string">">{{ $item->promoPrice}}</td>
<td hljs-string">">{{ $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 hljs-string">">
<label for="">ID</label>
<input type="text" hljs-string">" name="id" id="id" value="">
<span style="color:red">@error('id'){{$message}} @enderror</span>
</div>
<div hljs-string">">
<label for="">Offer ID</label>
<input type="text" hljs-string">" name="offerId" id="offerId" value="">
<span style="color:red">@error('offerId'){{$message}} @enderror</span>
</div>
<div hljs-string">">
<label for="">Promo Name</label>
<input type="text" hljs-string">" name="promoName" id="promoName" value="">
<span style="color:red">@error('promoName'){{$message}} @enderror</span>
</div>
<div hljs-string">">
<label for="">Promo Price</label>
<input type="number" hljs-string">" name="promoPrice" id="promoPrice" value="">
<span style="color:red">@error('promoPrice'){{$message}} @enderror</span>
</div>
</div>
<div hljs-string">">
<button type="button" hljs-string">" data-bs-dismiss="modal">
<i hljs-string">"></i>
<span hljs-string">">CANCEL</span>
</button>
<button type="submit" hljs-number">1">
<i hljs-string">"></i>
<span hljs-string">">SAVE</span>
</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- End Modal Edit-->
然后這是 web.php 檔案,用于路由:
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');
這是 master.blade.php 檔案:
<!--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-->
我認為這是代碼無法正確執行的部分。這是 FormControllerPromo.php 檔案:
// 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'));
}
我一直在嘗試撰寫這個代碼將近一個星期,但沒有成功,非常感謝任何幫助。:)
uj5u.com熱心網友回復:
在update似乎是正確的,應該作業。但是當您將$data變數傳遞給您的視圖時,您應該呼叫->get(),否則您將回傳一個查詢構建器實體,稍后Undefined property在嘗試訪問時會引發錯誤{{$item->offerId}}。
更改示例中的倒數第二行
//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();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/403662.html
標籤:
