#method('DELETE') and version 5.4.13 - laravel

In my form index.blade, I see that my method delete() has a problem to display???
In my index.blade I have this:
#foreach($students as $student)
<tr>
<td> {{$student->name}}</td>
<td> {{$student->firstname}}</td>
<td>
<form method="POST" action="{{ route('students.destroy', $student) }} ">
<a class="btn btn-sm btn-warning" href="{{route('students.edit',$student->id)}}">Editer</a>
{{csrf_field()}}
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Effacer</button>
</form>
</td>
</tr>
#endforeach
In my Controller I have this:
public function destroy($id)
{
$students = Student::find($id);
$students->delete();
return redirect()->route('students.index')
->with('success', 'Effacé !');
}
For information, I have the version '5.4.13'.
Thank you

In laravel 5.4 you need to use
{{ method_field('delete') }}
From 5.6 #method('delete') is introduced.

Related

how to make readable the url of an image that comes from an api that I created?

I created an api it's been 2 weeks. the api works well but I can't retrieve the images from this api during my HTTP requests.
My Controller Api/admin/AgencyController
`
public function store(AgencyRequest $request)
{
if ($request->hasFile('logo')) {
$path = $request->file('logo')->store('logo','public');
}
$agency=Agency::create([
'name'=>$request->name,
'headquarters'=>$request->headquarters,
'email'=>$request->email,
'logo'=>$path,
'phone_number'=>$request->phone_number,
'state'=>$request->state,
'password'=>bcrypt($request->password),
]);
return new AgencyResource($agency);
}
`
My index file
`
#foreach ($datas as $agencies)
#forelse ($agencies as $agency)
<tr>
<td><img src="{{ url('https://kipart.stillforce.tech/storage/'.$agency->logo) }}" width="48" alt="Product img"></td>
<td><h5>{{ $agency->logo }}</h5></td>
<td>
<i class="zmdi zmdi-edit"></i>
<form method="POST" action="{{ route('admin.agencies.destroy', $agency->id) }}" onsubmit="return confirm('Are you sure?')">
#csrf
#method('delete')
<button type="submit" class="btn btn-default waves-effect waves-float btn-sm waves-red" ><i class="zmdi zmdi-delete" aria-hidden="true" title="Suprimer"></i></button>
</form>
</td>
</tr>
`
#foreach ($datas as $agencies)
#forelse ($agencies as $agency)
<tr>
<td><img src="{{ url('https://kipart.stillforce.tech/storage/'.$agency->logo) }}" width="48" alt="Product img"></td>
<td><h5>{{ $agency->logo }}</h5></td>
<td>
<i class="zmdi zmdi-edit"></i>
<form method="POST" action="{{ route('admin.agencies.destroy', $agency->id) }}" onsubmit="return confirm('Are you sure?')">
#csrf
#method('delete')
<button type="submit" class="btn btn-default waves-effect waves-float btn-sm waves-red" ><i class="zmdi zmdi-delete" aria-hidden="true" title="Suprimer"></i></button>
</form>
</td>
</tr>
When I do a test registration of an agency. all the data is registered but when I retrieve the image to a flutter client or a laravel client the image does not appear but all the data is called

Invalid argument supplied for foreach() (View: xxx\resources\views\admin\info-admin.blade.php) http://127.0.0.1:8000/tambah-info

Invalid argument supplied foreach()when try to add data
here's the form for submitting the data
info-admin.blade.php
<tbody class="text-center align-middle">
#foreach ( $info as $infos )
<tr>
<th scope="row" class="align-middle">{{ $loop->iteration }}</th>
<td class="align-middle">{{ $infos->judul }}</td>
<td class="align-middle">{{ $infos->konten }}</td>
<td class="align-middle">{{ $infos->image }}</td>
<td class="align-middle">{{ $infos->created_at }}</td>
<td class="align-middle">{{ $infos->Updated_at }}</td>
<td class="align-middle form">
<button type="submit" class="btn btn-info mb-3">Edit</button>
<form method="POST" action="{{ route('infos.destroy', [$infos->id]) }}">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger">Hapus</button>
</form>
</td>
</tr>
#endforeach
</tbody>
Store logic
InfosController
public function store(Request $request)
{
$info = new Info();
$info->judul = $request->input('judul');
$info->konten = $request->input('konten');
$info->image = $request->input('image');
if($request->hasFile('image')){
$file = $request->file('image');
$extension = $file->getClientOriginalExtension();
$filename = time() . '.' . $extension;
$file->move('upload/info_penting' , $filename);
$info->image = $filename;
} else{
return $request;
$info->image = '';
}
$info->save();
return view('admin.info-admin')->with('info','$info');
}
my routes, in case if you want to see it
web.php
route::post('/tambah-info','InfosController#store')->middleware('auth','admin');
EDITED
undefined variable when trying to update
the controller for the logic for updating the data
InfosController
public function update(Request $request, Info $info)
{
//
Info::where('id', $info->id)
->update([
'judul' => $request->judul,
'konten' => $request->konten,
'image' => $request->image,
]);
return redirect('/info-admin')->with('success', 'Berhasil Diedit');
}
here's the form for for the updating the data
edit-info.blade.php
<form action="{{ route('infos.update', [$info->id]) }}" method="patch" enctype="multipart/form-data>
#csrf
<input type="hidden" name="_method" value="PATCH">
<div class="mb-3">
<label for="judul">Judul</label>
<input type="string" class="form-control" value="{{$info->judul}}" id="judul" name="judul" required>
<div class="invalid-feedback">
Kolom Wajib Diisi
</div>
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Konten</label>
<textarea class="form-control" value="{{$info->konten}} id="exampleFormControlTextarea1" rows="3" name="konten" required></textarea>
<div class="invalid-feedback">
Kolom Wajib Diisi
</div>
</div>
<input class="mb-3" type="file"value="{{$info->image}} name="image">
<button type="submit" class="btn btn-success btn-lg btn-block mt-auto">Kirim</button>
</form>
</div>
After saving your post, you should redirect to another route, probably the route for displaying all the posts
return redirect()->route('info-admin');
You should have another controller method for displaying all your data, you will have something similar to this
public function index()
{
$infos = Info::all();
return view('admin.info-admin', ['infos' => $infos]);
}
Then for sending a collection to the view. You should use your foreach this way and not the other way
#foreach($infos as $info)
// your code here
#endforeach
UPDATES
Your admin view should be like this
<tbody class="text-center align-middle">
#foreach ( $infos as $info )
<tr>
<th scope="row" class="align-middle">{{ $loop->iteration }}</th>
<td class="align-middle">{{ $info->judul }}</td>
<td class="align-middle">{{ $info->konten }}</td>
<td class="align-middle">{{ $info->image }}</td>
<td class="align-middle">{{ $info->created_at }}</td>
<td class="align-middle">{{ $info->updated_at }}</td>
<td class="align-middle form">
<button type="button" class="btn btn-info mb-3">Edit</button>
<form method="POST" action="{{ route('infos.destroy', $info->id) }}">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger">Hapus</button>
</form>
</td>
</tr>
#endforeach
</tbody>
Add data
define two routes, one route to access the form and another route to store form data
Route::get('/add-info', 'InfosController#create)->name('infos.add');
Route::post('/store-info', 'InfosController#store)->name('infos.store');
Access your add form as
<button type="button" class="btn btn-Success mb-3">Add</button>
Your create method
public function create()
{
return view('infos.add');
}
Your store method
public function store(Request $request)
{
$info = new Info();
$info->judul = $request->input('judul');
$info->konten = $request->input('konten');
$info->image = $request->input('image');
if($request->hasFile('image')){
$file = $request->file('image');
$extension = $file->getClientOriginalExtension();
$filename = time() . '.' . $extension;
$file->move('upload/info_penting' , $filename);
$info->image = $filename;
} else{
return $request;
$info->image = '';
}
$info->save();
return redirect()->route('admin.info-admin');
Edit data
define two routes, one route to access the form and another route to update form data
Route::get('/edit-info/{infos}/edit', 'InfosController#edit')->name('infos.edit');
Route::post('/update-info/{infos}/update', 'InfosController#update)->name('infos.update');
Access your edit form as
<button type="button" class="btn btn-Success mb-3">Edit</button>
Your edit method
public function edit($id)
{
$info = Info::findOrFail($id)
return view('infos.edit', ['infos' => $info]);
}
Your update method
public function update(Request $request, $id)
{
Info::where('id', $id)
->update([
'judul' => $request->judul,
'konten' => $request->konten,
'image' => $request->image,
]);
return redirect()->route('admin.info-admin')->with('success', 'Berhasil Diedit');
}
Add form action
<form action="{{ route('infos.store') }}" method="POST" enctype="multipart/form-data">
Edit form action
<form action="{{ route('infos.update', ['infos' => $info->id]) }}" method="POST" enctype="multipart/form-data">

product is not deleting from cart

hi m trying to delete product from cart but its not deleting , any suggestion to fix it,when i click on submit button then it says 404|not found
controller:
public function deleteCartProduct(Product $product)
{
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->delProduct($product);
Session::put('cart', $cart);
return redirect()->route('product.cart')->with('flash_message_success', 'Product product has been removed from Cart');
}
model
public function deleteProduct($product)
{
if ($this->contents) {
if (array_key_exists($product->product_slug, $this->contents)) {
$delProduct = $this->contents[$product->slug];
$this->totalQty -= $delProduct['qty'];
$this->totalPrice -= $delProduct['price'];
array_forget($this->contents, $product->slug);
}
}
}
blade file
#foreach($contents as $slug => $cartItem)
<form action="{{ route('deleteCartProduct', $product) }}" method="POST">
#csrf
<tr class="table-row">
<td class="column-1">
<div class="cart-img-product b-rad-4 o-f-hidden">
<img src="{{ URL::to('/') }}/images/backend_images/product_images/{{ $cartItem['product']->product_image }}" alt="IMG-PRODUCT">
</div>
</td>
<td class="column-2">{{ $cartItem['product']->product_name }}</td>
<td class="column-3">${{ $cartItem['product']->product_price }}</td>
<td class="column-4">
<div class="flex-w bo5 of-hidden w-size17">
<button class="btn-num-product-down color1 flex-c-m size7 bg8 eff2">
<i class="fs-12 fa fa-minus" aria-hidden="true"></i>
</button>
<input class="size8 m-text18 t-center num-product" type="number" name="num-product1" value="{{ $cartItem['qty'] }}">
<button class="btn-num-product-up color1 flex-c-m size7 bg8 eff2">
<i class="fs-12 fa fa-plus" aria-hidden="true"></i>
</button>
</div>
</td>
<td class="column-5">${{ $cartItem['price'] }}</td>
<td class="column-5">
<input type="submit" class="btn btn-danger value="Remove Product">
</td>
</tr>
</form>
#endforeach
route:
Route::get('/cart/delete-product/{id}','ProductController#deleteCartProduct')->name('deleteCartProduct');
Your route should be Route::delete instead of Route::get and then in the form add this as well:
#method('delete')
I saw your error on the button:
<input type="submit" class="btn btn-danger value="Remove Product">
Change it with this:
<input type="submit" class="btn btn-danger" value="Remove Product">
Missing quote..
EDIT
Your route should be this:
Route::delete('/cart/delete-product/{id}','Admin\ProductController#deleteCartProduct')->name('deleteCartProduct');
You need to change the code in a controller as below.
public function deleteCartProduct(Product $product)
{
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->deleteProduct($product);
Session::put('cart', $cart);
return redirect()->route('product.cart')->with('flash_message_success', 'Product product has been removed from Cart');
}
You defined it as method="POST" in blade. and "get" in route. So you need to change verb to Route::post

Getting error while clicking on edit function button in laravel

I need to edit by data from database via my admin panel. So i created table with clickable button for edit function.
Now when I click on edit button, I am seeing ID number at bottom but getting sorry page couldn't found error ! I have double checked all controller, route and everything. It seems all good, but I don't know what's the error!
Route Code:
Route::get('/admin/baseFare',[
'uses' => 'ExtraBaseFareController#basefare',
'as' => 'base.fare'
]);
Route::get('/admin/baseFare/edit/{$id}',[
'uses' => 'ExtraBaseFareController#editBaseFare',
'as' => 'editbase.fare'
]);
Route::post('/admin/baseFare/update/{id}', [
'uses' => 'ExtraBaseFareController#baseFareUpdate',
'as' => 'base.fareupdate'
]);`
Controller Code:
public function basefare()
{
$base = BaseFare::all();
return view('Admin.BaseFare.index')->With('base', $base);
}
public function editBaseFare($id)
{
$base = BaseFare::find($id);
return view('Admin.BaseFare.editBaseFare')->with('base', $base);
}
public function baseFareUpdate(Request $request, $id)
{
$base = BaseFare::find($id);
$base->fareinpercentage = $request->fareinpercentage;
$base->fareinrupees = $request->fareinrupees;
$base->save();
Session::flash('success','Base fare successfully updated');
return redirect()->route('base.fare');
}
Index Page code:
<table class="table display nowrap table-striped table-bordered bootstrap-3 scroll-horizontal">
<thead>
<tr>
<th>S.No</th>
<th>Fare in Percentage (%)</th>
<th>Fare in Rupees (Rs)</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#php $number = 1; #endphp
#foreach($base as $base)
<tr>
<td>
{{ $number.'.' }}
#php $number++; #endphp
</td>
<td>{{ $base->fareinpercentage }}</td>
<td>{{ $base->fareinrupees }}</td>
<td>
<a href="{{ route('editbase.fare',['id' => $base->basefareid ]) }}" class="m-portlet__nav-link btn m-btn m-btn--hover-accent m-btn--icon m-btn--icon-only m-btn--pill" title="Edit ">
<i class="la la-edit"></i>
</a>
</td>
</tr>
#endforeach
</tbody>
</table>`
Edit Page Code:
<form class="form" method="post" action="{{ route('base.fareupdate',['id' => $base->basefareid ]) }}">
<div class="form-body">
<h4 class="form-section"><i class="la la-eye"></i>Base Fare Controller</h4>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="userinput2">Fare in Percentage (%)</label>
<input type="text" id="fareinpercentage" value="{{ $base->fareinpercentage }}" class="form-control border-primary" name="fareinpercentage">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="userinput3">Fare in Rupee (Rs)</label>
<input type="text" id="fareinrupees" value="{{ $base->fareinrupees }}" class="form-control border-primary" name="fareinrupees">
</div>
</div>
</div>
</div>
<div class="form-actions right">
<button type="button" class="btn btn-warning mr-1">
<i class="ft-x"></i> Cancel
</button>
<button type="submit" name="submit" class="btn btn-primary">
<i class="la la-check-square-o"></i> Save
</button>
</div>
</form>`
These are the codes, the kindly help me to find our the error, The main function is to edit the field from database!
If I understand the question correctly you can't go to the edit page.
Run 'php artisan route:list' and compare the routes.
And I can't figure out why you have dollar sign before the id in the route.

how to handle dynamically created forms laravel 5.4

I want to know how to fetch name of checkboxes from dynamically created form. First I am displaying each records of data in a separate form then If records need to be deleted we will be able to delete. I am able to give different name as somename+id. But how do I know which name is going in the controller. As It is not clear that which name is going in the controller, I am not able to delete the record. I am doing it in laravel 5.4. Here is my code -
#if (isset($allcolors))
#foreach ($allcolors as $color)
<tr>
<form method="post" action="/delete">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<td>
<span class=""><input type="checkbox" name="deletecolor[{{$color->id}}]" value="{{$color->id}}"></span>
</td>
<td>
<div style="background:{{$color->web_color}}">a</div>
</td>
<td>{{$color->color_name}}</td>
<td>
<button type="submit" class="btn btn-danger">
Delete
</button>
</td>
</form>
</tr>
#endforeach
#endif
then on form submission I want to fetch which I am doing like this -
public function destroy(Request $request)
{
//
$id = $request->input('deletecolor');
$affected = DB::update("DELETE FROM vehicle_color where id = ?", [$id]);
//echo $affected==1?"Successfully Deleted":"Delete Fail";
}
I see you have $color->id. Why not delete them based on that?
#if (isset($allcolors))
#foreach ($allcolors as $color)
<tr>
<form method="post" action="/delete">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<td><span class=""><input type="checkbox" name="deletecolor[{{ $color->id }}]" value="{{$color->id}}"></span></td>
<td><div style="background:{{$color->web_color}}">a</div></td>
<td>{{$color->color_name}}</td>
<td><button type="submit" class="btn btn-danger" data-toggle="modal" data-target="#colorDelPopup">Delete</button></td>
</form>
</tr>
#endforeach
#endif
And in your controller:
public function destroy(Request $request)
{
$ids = $request->input('deletecolor');
// Instead of raw SQL, you can use the query builder to make your life a bit easier
$affected = DB::table('vehicle_color')->whereIn('id', $ids)->delete();
//echo $affected==1?"Successfully Deleted":"Delete Fail";
}
This should do the trick.

Resources