I want update a comment in Laravel.
<div class="edit-input" id="edit{{$comment->id}}">
<input type="text" name="edit_comment" class="form-control">
<div class="input-group-append">
OK
<button class="btn btn-danger" id="editCancel" type="button">Cancel</button>
</div>
</div>
This my route:
Route::get('review-edit/{id}', 'CommentController#editComment')->name('review-edit');
And CommentController:
public function editComment(Request $request, $id)
{
$updateComment = Comment::findOrFail($id);
$updateComment->user_id = Auth::id();
$updateComment->comment = $request->edit_comment;
$updateComment->save();
return back();
}
When I try update comment, I'm getting an error telling me
SQLSTATE[23502]: Not null violation: 7
dd($request->edit_comment) gives null also. What am I overlooking here?
Try this
you edit_comment should be inside form then only you can send data to controller
<form action="{{ route('review-edit', [ 'id' => $comment->id]) }}" method="get">
<div class="edit-input" id="edit{{$comment->id}}">
<input type="text" name="edit_comment" class="form-control">
<div class="input-group-append">
<button class="btn btn-info" type="submit">OK</button>
<button class="btn btn-danger" id="editCancel" type="button">Cancel</button>
</div>
</div>
</form>
try this and dd($request->edit_comment);
<form action="{{ route('review-edit', [ 'id' => $comment->id]) }}" method="get">
<div class="edit-input" id="edit{{$comment->id}}">
<input type="text" name="edit_comment" class="form-control">
<div class="input-group-append">
<button class="btn btn-info" type="submit">OK</button>
<button class="btn btn-danger" id="editCancel" type="button">Cancel</button>
</div>
</div>
</form>
Related
I need your help guys on this, my app can't delete neither can it Update. it keeps on popping these errors on my edit and Delete Buttons;
(1/1) MethodNotAllowedHttpException
The DELETE method is not supported for this route. Supported methods: GET, HEAD.
(1/1) MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD.
this is my route
Route::group(['middleware' => ['role:Admin']], function () {
Route::resource('/device', 'DeviceController');
});
this is my edit blade
<form action="/device" method="POST" id="editForm">
{{csrf_field()}}
{{ method_field('PUT') }}
<div class="col-md-4 mb-3">
<label>Serial Number</label>
<input type="text" name="Serial_No" id="" class="form-control" placeholder="Enter Serial number">
</div>
<button class="btn btn-primary" type="submit">Add Data</button>
<button type="reset" class="btn btn-default float-right">Cancel</button>
<button type="reset" class="btn btn-default float-middle">Clear</button>
</form>
This is my delete blade
<form action="/device" method="POST" id="deleteForm">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="form-row">
<input type="hidden" name="_method" value="DELETE" >
<P>Are You Sure!.. You want to delete this Device?</P>
</div>
<button class="btn btn-primary " type="submit" >YES! DELETE DEVICE</button>
<button type="button" class="btn btn-secondary float-right" data-dismiss="modal" >CANCEL</button>
</form>
This is my controller for delete
public function destroy($id)
{
$devices = Device::find($id);
$devices -> delete();
return Redirect::back() -> with('success','Data Deleted Successfully');
}
This is my script that deletes
<script>
//Start Delete Record
table.on('click', '.delete', function () {
$tr = $(this).closest('tr');
if ($($tr).hasClass('child')) {
$tr = $tr.prev('.parent');
}
var data = table.row($tr).data();
console.log(data);
$('#deleteForm').attr('action', '/laptops/'+data[0]);
$('#deleteModal').modal('show');
});
//End Delete Record
});
</script>
You are not passing the correct actions
your update form should look like this instead
<form action="" method="POST" id="editForm">
{{csrf_field()}}
{{ method_field('PUT') }}
<div class="col-md-4 mb-3">
<label>Serial Number</label>
<input type="text" name="Serial_No" id="" class="form-control" placeholder="Enter Serial number">
</div>
<button class="btn btn-primary" type="submit">Add Data</button>
<button type="reset" class="btn btn-default float-right">Cancel</button>
<button type="reset" class="btn btn-default float-middle">Clear</button>
</form>
your delete form should look like this instead.
<form action="" method="POST" id="deleteForm">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="form-row">
<p>Are You Sure!.. You want to delete this Device?</p>
</div>
<button class="btn btn-primary " type="submit">YES! DELETE DEVICE</button>
<button type="button" class="btn btn-secondary float-right" data-dismiss="modal">
CANCEL
</button>
</form>
your script should look like this (change 'laptops/' to 'device/')
table.on('click', '.delete', function () {
$tr = $(this).closest('tr');
if ($($tr).hasClass('child')) {
$tr = $tr.prev('.parent');
}
var data = table.row($tr).data();
console.log(data);
$('#deleteForm').attr('action', '/device/'+data[0]);
$('#deleteModal').modal('show');
});
Also as an opinion you could pluralize your resource route instead of the singular approach. i.e /devices instead of /device
so in your web.php routes file you have:
Route::group(['middleware' => ['role:Admin']], function () {
Route::resource('/devices', 'DeviceController');
});
That way you have a more friendly route list like:
GET - /devices
GET - /devices/create
POST - /devices/update
GET - /devices/{device}
GET - /devices/{device}/edit
PUT/PATCH - /devices/{device}
DELETE - /devices/{device}
All these can be viewed in your cli using php artisan route:list
Route
Route::post('/review/{pId}','RentalController#review');
Controller:
public function review(Request $request,$propId){
$review = new Reviews();
$rpId = rand();
$review->rvid=$rpId;
$review->usid_fk = Auth::user()->uid;
$review->prId_fk = Property::find($propId);
$review->comment = $request->input('comment');
$review->rating = $request->input('rating');
$review->date = Carbon::now();
$review->save();
}
PHP
<form action="/review/{{ $prop->pid}}" method="POST">
{{csrf_field()}}
<div class="modal-body">
<input type="hidden" name="propid" value="{{ $prop->pid }}"/>
<input id="rating" name="rating" class="rating rating-loading" data-show-clear="false" data-min="0" data-max="5" data-step="1" value="0">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">Comment</span>
</div>
<textarea name="comment" class="form-control" aria-label="Comment">
</textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
I am getting "page not found", so I don't know whether should I use "put" or "post" method in route as well as in form .
Your replies would be highly appreciated.
I think the problem is from the action in your form.
change your form to
<form action="{{route('review.post',['id'=>$prop->pid])}}" method="POST">
</form>
and change your route to
Route::post('review/{pId}','RentalController#review')->name('review.post');
Inspect the form from browser and make sure csrf token and action url are well formed.
I Have a List of projects and three type of payments for any project I Want to give all type of payments and send payment id to ajax But I give just id=1 for all request my Code is here:
#foreach($project->payments as $payment)
#if($payment->type==1)
#if(empty($payment->code))
<input type="hidden"id="payment_id"value="{{$payment->id}}">
<input type="text" class="form-control" id="paymentCode" name="paymentC">
{{csrf_field()}}
<button id="AddCode" type=submit class="btn btn-info generate-label AddCode">enter</button>
#else
{{$payment->code}}
#endif
#elseif($payment->type==2)
#if(empty($payment->code))
<input type="hidden" id="payment_id" value="{{$payment->id}}">
<input type="text" class="form-control" name="paymentC" id="paymentCode">
{{csrf_field()}}
<button id="AddCode" type="submit" class="btn btn-info generate-label AddCode">enter</button>
#else
{{$payment->code}}
#endif
#endif
#endforeach
Javascript Code:
$('.AddCode').click (function (event) {
var payment_id = $('#payment_id').val();
var paymentCode = $('#paymentCode').val();
console.log(payment_id);
});
How I Can fix my problem that When click in button of payment print this id?
You're not targetting an unique id with var payment_id = $('#payment_id').val();
Change this line of html
<button id="AddCode" type="submit" class="btn btn-info generate-label AddCode">enter</button>
Into
<button value="{{$payment->id}}" type="submit" class="btn btn-info generate-label AddCode">enter</button>
JQuery
$('.AddCode').on("click", function(event){
var payment_id = $(this).val(); // payment id
});
Try this
#foreach($project->payments as $payment)
#if($payment->type==1)
#if(empty($payment->code))
<div>
<input type="hidden" id="payment_id" value="{{$payment->id}}">
<input type="text" class="form-control" id="paymentCode" name="paymentC">
{{csrf_field()}}
<button id="AddCode" class="btn btn-info generate-label AddCode">enter</button>
</div>
#else
{{$payment->code}}
#endif
#elseif($payment->type==2)
#if(empty($payment->code))
<div>
<input type="hidden" id="payment_id" value="{{$payment->id}}">
<input type="text" class="form-control" name="paymentC" id="paymentCode">
{{csrf_field()}}
<button id="AddCode" class="btn btn-info generate-label AddCode">enter</button>
</div>
#else
{{$payment->code}}
#endif
#endif
#endforeach
Javascript Code:
$('.AddCode').click (function (event) {
var payment_id = $(this).closest('div').find("#payment_id").val();
var paymentCode = $(this).closest('div').find("#paymentCode").val();
console.log(payment_id);
});
ID should be unique for individual HTML entity.
Code Update:
{{csrf_field()}}
#foreach($project->payments as $payment)
#if($payment->type==1)
#if(empty($payment->code))
<input type="hidden"id="payment-id-1"value="{{$payment->id}}">
<input type="text" class="form-control" id="payment-code-1" name="payment_code">
<button type=submit class="btn btn-info generate-label add-code" data-payment=1>enter</button>
#else
{{$payment->code}}
#endif
#elseif($payment->type==2)
#if(empty($payment->code))
<input type="hidden" id="payment_id_2" value="{{$payment->id}}">
<input type="text" class="form-control" name="payment_code" id="paymentCode">
<button type="submit" class="btn btn-info generate-label add-code" data-payment=1>enter</button>
#else
{{$payment->code}}
#endif
#endif
#endforeach
Javascript Code:
$('.add-code').on("click", function(event){
var payment_type = $(this).data("payment");
var payment_id = $('#payment-id-'+payment_type).val();
var paymentCode = $('#payment-code-'+payment_type).val();
});
Try this out. Not tested.
This is my Laravel route where I am getting value from as 'q' from a form to perform search from quiz table.
Route::get('/admin/Searchquizzes',function ($q) {
$quizzes = DB::table('quiz')
->leftjoin('category', 'quiz.category_id', '=', 'category.id')
->where('name','LIKE','$q')
->select('quiz.*', 'category.name As category_name')
->get();
$categories = DB::table('category')->select('id', 'name')->get();
return view('admin/quizlisting', ['quizzes' => $quizzes, 'categories' => $categories]);
)->middleware('auth')->name('admin.Searchquizzes');
This is code for my laravel Form to perform search ..
<div class="input-group">
<input type="text" name="q" id="q" class="form-control" placeholder="Search...">
<span class="input-group-btn">
<button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
</button>
</span>
</div>
</form>
This is not working I don't know why, help shall be appreciated.
Fixed it by my self :)
Route::get('/admin/Searchquizzes',function (Request $request) {
//dd($request->q);
$quizzes = DB::table('quiz')
->leftjoin('category', 'quiz.category_id', '=', 'category.id')
->where('quiz.name','LIKE',$request->q)
->select('quiz.*', 'category.name As category_name')
->get();
$categories = DB::table('category')->select('id', 'name')->get();
return view('admin/quizlisting', ['quizzes' => $quizzes, 'categories' => $categories]);
})->middleware('auth')->name('admin.Searchquizzes');
Here is my form , Now i can search quizes :)
<form method="get" action="{{route('admin.Searchquizzes')}}">
{{ csrf_field() }}
<div class="input-group">
<input type="text" name="q" id="q" class="form-control" placeholder="Search...">
<span class="input-group-btn">
<button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
</button>
</span>
</div>
</form>
YOu won't get that form value. You are not using POST method
<form method="POST" action="{{route('admin.Searchquizzes')">
{{ csrf_field() }}
<div class="input-group">
<input type="text" name="q" id="q" class="form-control" placeholder="Search...">
<span class="input-group-btn">
<button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
</button>
</span>
</div>
</form>
Route :
use Illuminate\Http\Request;
Route::POST('/admin/Searchquizzes',function (Request $request) {
//dd($request->q);
$quizzes = DB::table('quiz')
->leftjoin('category', 'quiz.category_id', '=', 'category.id')
->where('name','LIKE','$q')
->select('quiz.*', 'category.name As category_name')
->get();
$categories = DB::table('category')->select('id', 'name')->get();
return view('admin/quizlisting', ['quizzes' => $quizzes, 'categories' => $categories]);
})->middleware('auth')->name('admin.Searchquizzes');
Here is my form :
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="DELETE">
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
<input type="hidden" name="_method" value="DELETE" />
</div>
</form>
Here is my controller :
public function destroy($id)
{
$invoice = Invoice::find($id);
if(!$invoice){
return redirect()->route('invoice.index')->with(['fail' => 'Page not found !']);
}
$invoice->delete();
return redirect()->route('invoice.index')->with(['success' => 'Invoice Deleted.']);
}
But it can not delete where is the problem ? How solve this ?
You need to use POST method for the form and add input element with name _method and value DELETE. Also, add token:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="POST">
<input name="_method" type="hidden" value="DELETE">
{{ csrf_field() }}
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
</div>
</form>
I think you must add a hidden input to the form which will contain the method used:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="POST">
<input type="hidden" name="_method" value="DELETE" />
</form>
Read more on Laravel documentation about Form method spoofing
In order to get PUT and DELETE methods to work, you need an additional field, since only POST and GET are possible within HTML (out-of-the-box).
The additional field will be made with the code:
{!! method_field('DELETE') !!}
So your form will look like this:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="DELETE">
{!! method_field('DELETE') !!}
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
</div>
</form>
Also, if you are using blade templates, you can add the method field like so:
#method('DELETE')
More Laravel way you can do this
<form action="{{ route('invoice.destroy',$invoice->id)}}" method="POST">
#method('DELETE')
<button type="submit" class="btn btn-primary">Yes</button>
</form>