I want delete a comment, but when i click, this redirect me to laravel error page and display "" that error. Why is happen ?
web.php
Route::post('/comments/destroy/{id}', 'CommentController#destroy')->name('comment.destroy');
commentcontroller
public function destroy($id){
$comment=Comment::where('id',$id)->first();
$comment->delete();
return redirect()->back();
}
blade
#foreach($comment as $comments)
<form action="{{route('comment.destroy',$comments->id )}}" method="post">
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn"><i class="fa fa-trash-o"></i></button>
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="comment-body"><p>{{ $comments->body }}</p></div>
</form>
#endforeach
If you add {{ method_field('DELETE') }} on the html form, you must change the route method to delete:
Route::delete('/comments/destroy/{id}', 'CommentController#destroy')->name('comment.destroy');
But if you want to use POST method on the route, you must remove {{ method_field('DELETE') }} from your form.
Related
When I try to delete a post from my model Perk and click "cancel" in deleting it using onclick the data is still deleted. Here's the code that I did:
<form method="POST" onclick="return confirm('Are you sure you want to delete this item?');" action="{{ route('perk.destroy', $perk->id) }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<i class="cursor-pointer fas fa-trash text-secondary"></i>
</form>
When I want to edit a specific category I want to see the previous value of that category, but I don't get previous value of the category in the form. I have used route model binding in CategoryController. The main part of the code is given bellow:
Controller:
public function edit(Category $category){
return view('admin.editcategory',compact('category'));
}
View:
<form class="form-horizontal" action="{{ route('Category.update',['category'=>$category]) }}" method="POST">
#method('PATCH')
<input type="text" name="category_name" value="{{ $category->category_name }}">
{{ $errors->first('category_name') }}
<textarea name="category_description" row="3">
{{ $category->category_description }}
</textarea>
{{ $errors->first('category_description') }}
<button type="submit" class="btn btn-primary">Edit category</button>
#csrf
</form>
Route:
Route::resource('Category','CategoryController');
In edit function passing the id of a category
public function edit($id){
$category=Category::find($id);
return view('admin.editcategory',compact('category'));
}
I am using laravel 5.2 and I am unable to delete article in laravel. Below is my view link:
<form method="DELETE" action="/article/{{ $article->id }}">
{{ csrf_field() }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Below is my controller code:
public function destroy($id)
{
Article::destroy($id);
Session::flash('msg','Article deleted successfully');
return redirect()->back();
}
Below are route listing:
HTML forms don't actually support any methods other than GET and POST. To get around this Laravel spoofs the method and then picks this up in the request.
From the docs:
HTML forms do not support PUT, PATCH or DELETE actions. So, when
defining PUT, PATCH or DELETE routes that are called from an HTML
form, you will need to add a hidden _method field to the form. The
value sent with the _method field will be used as the HTTP request
method
As such, you just need to alter your form like so:
<form method="POST" action="/article/{{ $article->id }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
You can also generate the _method with {{ method_field('DELETE') }} using Blade.
In your view file what you need to do is...
<form method="POST" action="/article/{{ $article->id }}">
<input type="hidden" name="_method" value="DELETE">
{{ csrf_field() }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Okay so I've got a view composer; that basically pulls a row from my table 'heros'. This then allow's me to obviously use the object $hero>whatever; on a page. The problem is this obviously only works for someone who is logged in.
View::composer(array('home'), function($view)
{
$view->with('herodata', Hero::where('owner_id', Auth::user()->id)->first());
});
As its using the
Auth::user()->
to find the correct row in the 'heros' table, once the user is no longer logged in it cannot access this. As no one is set to Auth.
So the error when I logout (or anyone no logged in) is this:
ErrorException
Trying to get property of non-object
));
//Authenticated group
Route::group(array('before' => 'auth'), function() {
View::composer(array('home'), function($view)
{
$view->with('herodata', Hero::where('owner_id', Auth::user()->id)->first());
});
How does one go about this?
Note: Still learning, I DID try and search but I found nothing probably because Im not sure how to put my issue into the correct sentance..
EDIT: Included home.blade.php
#extends('layout.main')
#section('content')
#if(Auth::check())
<p>Hello, {{{ Auth::user()->username }}}.</p>
#if($herodata)
Your hero:<br>
<b>{{ $herodata->hero; }}</b><br>
<b>Stats:</b>
LvL: {{ $herodata->level }}
Exp: {{ $herodata->exp }}
HP: {{ $herodata->hp }}/{{ $herodata->max_hp }}
Str: {{ $herodata->str }}
Atk: {{ $herodata->atk }}
Def: {{ $herodata->def }}
Int: {{ $herodata->int }}
Blk: {{ $herodata->blk }}
<form action="{{ URL::route('hero-delete') }}" method="POST">
<input type="submit" value="Delete"><br>
</form>
<form action="{{ URL::route('rest') }}" method="POST">
<input type="submit" value="Rest">
</form>
<form action="{{ URL::route('attack') }}" method="POST">
<input type="submit" value="Random attack">
</form>
<form action="{{ URL::route('hero-died') }}" method="POST">
<input type="submit" value="Died">
</form>
#else
<form action="{{ URL::route('hero-create') }}" method="POST">
Hero:<br>
You do not have a hero, create one!
<input type="text" name="hero">
<input type="submit" value="Create hero">
#if($errors->has('hero'))
{{ $errors->first('hero')}}
#endif
{{ Form::token()}}
</form>
#endif
#else
<p>You are not signed in.</p>
#endif
#stop
Check if the user is logged in before setting the hero data:
if (Auth::check())
{
View::composer(array('home'), function($view)
{
$view->with('herodata', Hero::where('owner_id', Auth::user()->id)->first());
});
}
I already had a post that lead to this question but now I isolated one problem and so its better to make a new clean post :
Symfony 2.1.3 with jordillonch/CrudGeneratorBundle
I used this CrudGenerator on an entity in which I have 2 timestampable fields that need to be updated automatically :
I installed Gedmo for the timestampable funtionnality.
The fields are :
cree_le (in english created_at) : #Gedmo\Timestampable(on="create")
and modifiele (in english updated_at) - #Gedmo\Timestampable(on="update")
The problem is that I want to customize the form to show only the fields to update by the user.
When I use the not customized edit.html.twig using {{ form_widget(edit_form) }} the update is working but the values of updated_at are not changed because the old value is submitted with the form.
I tried several things to customize the form but I did not yet find the solution.
This version of customized edit.html.twig is not working because the submitted form is not valid :
<form class="well" action="{{ path('employee_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(edit_form) }}>
<input type="hidden" name="_method" value="PUT" />
<div>
{{ form_label(edit_form.nom) }}
{{ form_errors(edit_form.nom) }}
{{ form_widget(edit_form.nom) }}
</div>
<div>
{{ form_label(edit_form.email) }}
{{ form_errors(edit_form.email) }}
{{ form_widget(edit_form.email) }}
</div>
<div>
{{ form_label(edit_form.telephone, 'Téléphone') }}
{{ form_errors(edit_form.telephone) }}
{{ form_widget(edit_form.telephone) }}
</div>
<div>
{{ form_label(edit_form.actif) }}
{{ form_errors(edit_form.actif) }}
{{ form_widget(edit_form.actif) }}
</div>
<input type="hidden" id="too_employeebundle_employee_cree_le" name="too_employeebundle_employee[cree_le]" value="{{ entity.creele|date('Y-m-d H:i:s') }}">
<input type="hidden" id="too_employeebundle_employee_modifie_le" name="too_employeebundle_employee[modifie_le]" value="{{ entity.modifiele|date('Y-m-d H:i:s') }}">
{{ form_widget(edit_form._token) }}
<p>
<button type="submit" class="btn btn-success">{{ 'views.edit.editbutton'|trans({}, 'JordiLlonchCrudGeneratorBundle') }}</button>
</p>
</form>
Sure I'm doing something wrong but who could tell me how to go on ?
Remove cree_le and modifiele from your Form Class and dont render them on your template.
Then let Timestampable do his job! when you create something the cree_le value will be automatic set. When you change something the modifiele value will be automatic set too.