Laravel Eloquent to php - laravel-5.6

How to make delete function in laravel model, usually when in blade we use :
<form action="{{ action('ItemNameController#destroy', $ItemName->id) }}" method="post">
#csrf
#method("DELETE")
<input type="submit" class="btn btn-danger btn-sm btn-style" href="{{ $ItemName->id }}" value="Delete" onclick="return confirm('Are You Sure To Delete This Item? #{{ $ItemName->inc }} ')">
</form>
So how to write that code when we write it in controller, because in controller cannot to write eloquent or cannot to process (.blade.php)
Example in Controller:
foreach ($i as $value) {
echo "<form action='' method='post'>";
echo "<th><input type='submit' class='btn btn-danger btn-sm btn-style' href=' value='Delete' value='Delete' style='font-size: 10px'></th>";
echo "</form>";
}

You can achieve a this by:
<form action="{{ action('ItemNameController#destroy', $ItemName->id) }}" method="post">
{{csrf_field()}}
<input type="submit" class="btn btn-danger btn-sm btn-style" href="{{ $ItemName->id }}" value="Delete" onclick="return confirm('Are You Sure To Delete This Item? #{{ $ItemName->inc }} ')">
</form>
Where in your controller you would handle the delete method. If you were using an ajax request you could send a delete request to the server.
Your route something like be:
Route::post('/itemname/{ItemName}/delete', 'ItemNameController#destroy');
To stop it being confused with:
Route::post('itemname/{ItemName}', 'ItemNameController#update);
Hope this helps.

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

Laravel pretty url in edit method

can someone tell me how can I still have pretty URL when trying to edit my post? I have slugs etc but for edit method, we need to use GET|HEAD which means we will have something like this:
www.our-domen.com/admin/posts/this-is-my-post-for-edit/edit?_token=0GXpk4oaLdGy8YOdp0591ogAOIHF89ZCciWk79h&btn-editPost=
Instead of this:
www.our-domen.com/admin/posts/this-is-my-post-for-edit/edit
Here is the part of the code:
<td>
<form action="{{ route('posts.edit', $post->slug) }}" method="get">
#csrf
<button type="submit" name="btn-editPost" class="btn btn-info btn-sm" style="color: white;">Edit</button>
</form>
</td>
Note : slugs are set up correctly, it is not a problem :)
You don't need the form actually, if you just delete #csrf, it will work.
Or, change your code :
<td>
<form action="{{ route('posts.edit', $post->slug) }}" method="get">
#csrf
<button type="submit" name="btn-editPost" class="btn btn-info btn-sm" style="color: white;">Edit</button>
</form>
<td>
To,
<td>
Edit
</td>

Laravel | Form not submitting to route given in action [SOLVED]

I'm trying to submit a form to the right route using the action:
<form action="{{ route('document.destroy', $d->id) }}" method="POST" style="display: inline;">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<span type="text" value="" class="btn btn-success" readonly="readonly">
{{ $d->file_name }} ({{ $d->file_size }}) <i class="fa fa-times"></i>
</span>
<button class="btn btn-xs btn-default" type="submit" data-toggle="tooltip" title="Verwijder" onclick="return confirm('Weet je zeker dat je dit document wilt verwijderen')"><i class="fa fa-times"></i></button>
</form>
But yet it links to a different controller. What am I doing wrong?
I also specified the controller in the routes file:
Route::resource('document', 'DocumentController');
To add more context: this view gets passed from a different controller than I want to use for the DELETE function.
EDIT
I used a form inside another form, my stupid head didn't see it.
for deleting you can try this
<form action="{{action('DocumentController#destroy', $d->id)}}" method="post">
{{csrf_field()}}
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
and Destroy method
public function destroy($id)
{
$doc= Document::find($id);
$doc->delete();
return redirect('/home')->with('success', 'Document has been deleted!!');
}

laravel delete method not working

I've made a small laravel project but the delete method is nog working:
I use a resource controller
my route is :
Route::resource('roles','Admin\RoleController');
in my view I have
<form action="{{route('roles.destroy',$role->id)}}" style="display:inline">
#method('delete')
#csrf
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>
But when i click the button it will show me the role ( = get method of the resource )
What am I doing wrong ?
If you're using Laravel 5.1 or later
<form action="{{ route('roles.destroy', 'YOUR_ID') }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>
If you're using Laravel 5.6 or later
<form action="{{ route('roles.destroy', 'YOUR_ID') }}" method="POST">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>
You can read more about method spoofing in Laravel Documentation.
Check Laravel's documentation.
Did you try adding method="POST" to the form?

Laravel resource controller destroy

I'm having trouble using the destroy method from my resource controller. I know in html you can only POST/GET so I used a hidden value but it comes up with a MethodNotAllowedHttpException error. Here is my code:
Modal where the Delete Button is:
<div class="modal-footer">
<form action="/wod/{{$wod->id}}" method="POST">
{{ csrf_field() }}
{{ method_field('DELTE') }}
<button type="button" class="btn btn-default navbar-inverse" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-default navbar-inverse"><a class="bottom" href="/wod/{{$wod->id}}/edit">Edit</a></button>
<button class="btn btn-primary navbar-inverse" value="submit" type="submit">Delete</button>
</form>
</div>
And here is my resource controller:
public function destroy(Id $id)
{
//
Wod::find($id)->delete();
return redirect()->action("WodController#index");
}
you have a typo in {{ method_field('DELTE') }}
it should be {{ method_field('DELETE') }}

Resources