why my database does not update ? (LARAVEL) - laravel

my database does not update while when i do
dd(request()->has('validated'));
this is what my web.php looks like:
Route::patch('prof/theme/{id}/validated', 'Theme\ThemeController#valide')->name('prof.theme.validated');
this is what my ThemeController.php looks like:
public function valide(Theme $theme) {
$theme->update([
'validated' => request()->has('validated')
]);
return back();
}
this is what my show.blade.php looks like:
<form action="{{route('prof.theme.validated', $theme)}}" method="POST">
#csrf
#method('PATCH')
#if ($theme->validated)
<button type="submit" class="btn btn-danger text-center" style="width: 350px">
INVALIDER LE THEME
</button>
#else
<button type="submit" class="btn btn-success text-center" name="validated" id="validated" style="width: 350px">
VALIDER LE THEME
</button>
#endif
</form>
this code does not show me any error but it does not produce the expected action. my variable "validated" is a boolean

thank you Harshith VA!!!!! i was able to solve the problem thanks to your idea. I kept your view and used your controller idea this way
public function valide(Request $request, Theme $theme) {
$theme->validated = $request->has('validated');
$theme->save();
return back();
}
thanks you for your help

From first look, I would believe this is due to the fact that you have added name="validated" to the <button> and not as an <input>.
<form action="{{route('prof.theme.validated', $theme)}}" method="POST">
#csrf
#method('PATCH')
#if ($theme->validated)
<button type="submit" class="btn btn-danger text-center" style="width: 350px">
INVALIDER LE THEME
</button>
#else
<input type="hidden" name="validated" value="true">
<button type="submit" class="btn btn-success text-center" style="width: 350px">
VALIDER LE THEME
</button>
#endif
</form>
Here, we add a hidden input field with the validated input. Doing this should pass that the validated key/value to the controller.
Another point. dd() is used to die and dump data, and would stop execution of whatever script is running. Therefore it won't save to the db once you do this.
The above assumes you didn't mean it doesn't save to the database when you dd().
Update
Also note that ->has() checks if the input exists. You should use request('validated') to get its value.

Related

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!!');
}

How to update specific row using modal Laravel 5.8

I am trying to update a specific row using modal. However I don't have any idea how to pass the value of the row id to the route parameter.
Here's the update form.
<form action="{{route('subcategory.update', 'idhere')}}" method="POST">
#method('PATCH')
#csrf
<div class="modal-body">
<label for="editname">New sub-category name:</label>
<input type="text" name="editname" id="editname" class="form-control">
<input type="text" name="editid" id="editid" value="" hidden>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button class="btn btn-primary" type="submit">Save changes</button>
</div>
</form>
Edit button
<button class="btn btn-secondary btn-sm" data-myid="{{$item->id}}"
data-mytitle ="{{$item->name}}"
data-target="#editsub" data-toggle="modal">Edit</button>
And here's the controller
public function update(Request $request, Subcategory $subcategory)
{
// return $request;
Subcategory::where('id',$request->editid)->update([
'name' => $request->editname
]);
return back();
}
When you pass an id here and if your model name is subcategory.php you will get the db record in your controller.
<form action="{{route('subcategory.update', $subcategory )}}" method="POST">
Or you can do following too
<form action="{{route('subcategory.update', $subcategory->id )}}" method="POST">
Your route should have {subcategory} in it for eager loading. After that you can do following in your code.
public function update(Request $request, Subcategory $subcategory)
{
//variable $subcategory has the db record
$subcategory->update(['name' => $request->only('editname')]);
return back();
}
If I understand it correctly, you can store the item id in
<input type="text" name="editid" id="editid" value="{$item->id}" hidden>
And since you are using POST request, you can easily get the item id from the request.
Like the one in your post:
$request->input('editid');
Please try first to pass id in input box hidden field

Laravel 5.4 CRUD DELETE Method not working, there is no reaction on clicking DELETE

After clicking on DELETE, there is no reaction, absolutely nothing happens. I am using HTML Forms, that's why I used, #method('DELETE'). I had similar problem with edit, after using #method(PUT), edit is working properly.
<div class="row">
<div class="col-6 align-self-start">
<a class="btn btn-light" href="/listings/{{$listing->id}}/edit">Edit</a>
</div>
<div class="col-6 align-self-end">
<form action="{{action('ListingsController#destroy', [$listing->id])}}" method="POST">
<?= csrf_field()?>
#method('DELETE')
<a class="btn btn-danger" href="/dashboard">Delete</a>
</form>
</div>
</div>
Destroy Function:
public function destroy($id)
{
$listing = Listing::find($id);
$listing->delete();
return redirect('/dashboard')->with('success', 'Contact Deleted');
}
Change
<a class="btn btn-danger" href="/dashboard">Delete</a>
to
<a type="submit" class="btn btn-danger" href="/dashboard">Delete</a>
You should add submit otherwise it will not submit the form.
Its recommended to use a instead of :
<button type="submit" class="btn btn-danger">Delete</button>

Route not defined even I defined it

Hey I am working on a code in which when user clicks update button it will go to another page that is doctorEdit. I defined the route in my web file but its again and again giving error route not defined. Can anyone please help me to reslove my problem. Below is my code.
Route code:
Route::resource('doctor/doctorEdit','DoctorController#edit');
Controller code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class DoctorController extends Controller
{
public function edit()
{
return view('doctor.doctorEdit');
}
}
And my view code is
<form class="row" method="POST" action="#" onsubmit = "return confirm('Are you sure?')">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<a href="{{ route('doctor/doctorEdit', ['id' => $doctor->id]) }}" class="btn btn-warning col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Update
</a>
<button type="submit" class="btn btn-danger col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Delete
</button>
</form>
Please let me know what I am doing wrong because I am new at laravel.
Try using GET method with a named route.
Your routefile
Route::get('doctor/doctorEdit','DoctorController#edit')->name('doctor.edit');
Your view code
<form class="row" method="POST" action="#" onsubmit = "return confirm('Are you sure?')">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<a href="{{ route('doctor.edit', ['id' => $doctor->id]) }}" class="btn btn-warning col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Update
</a>
<button type="submit" class="btn btn-danger col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Delete
</button>
</form>
You're doing it wrong, take a look at the documentation on Resource Controllers.
Basically if you define a resource with Route::resource() method, you must not specify the controller action, since the Resource Controller is expected to provide compatible REST methods.
So if you want to create a REST resource controller you must specify your route as:
Route::resource('doctor', 'DoctorController');
so you have to specify just your controller class name.
Then in your controller you have to specify required methods:
class DoctorController extends Controller
{
public function index()
{
// GET yourapp.com/doctor -> typically return all doctors
}
public function create()
{
// GET yourapp.com/doctor/create -> typically show doctor creation form
}
public function show()
{
// GET yourapp.com/doctor/{doctor_id} -> show a single doctor
}
public function store()
{
// POST yourapp.com/doctor -> create a new doctor
}
public function edit()
{
// GET yourapp.com/doctor/{doctor_id}/edit -> show edit form view
return view('doctor.doctorEdit');
}
public function update()
{
// PUT|PATCH yourapp.com/doctor/{doctor_id} -> update a doctor
}
public function destroy()
{
// DELETE yourapp.com/doctor/{doctor_id} -> delete a doctor
}
}
If you just want to expose the edit form without the REST logic use the Request::get() method:
Request::get('doctor/doctorEdit', 'DoctorController#edit')->name('doctor.edit');
If your are caching your routes remember to refresh them using the artisan command sequence
php artisan route:clear
php artisan route:cache
or more concisely just
php artisan route:cache
You're defining a resource. The correct way to do this is:
Route::resource('doctor','DoctorController');
The name for the DELETE method is doctor.destroy, and the controller method is destroy
Your view code should be
<form class="row" method="POST" action="{{ route('doctor.delete') }}" onsubmit = "return confirm('Are you sure?')">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<a href="{{ route('doctor.edit', ['id' => $doctor->id]) }}" class="btn btn-warning col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Update
</a>
<button type="submit" class="btn btn-danger col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Delete
</button>
</form>

Laravel 5.2 - Form do not submit

Sorry, i'm very new to Laravel, i have this form:
<form action="/register" method="post">
{!! csrf_field() !!}
....
<div class="form-group">
<button type="button" class="btn btn-default">Register</button>
</div>
</form>
And the route is:
Route::get('/register', 'Auth\AuthController#getRegister');
Route::post('/register', 'Auth\AuthController#postRegister');
When i click on Register button nothing happen... the form do not submit, no errors etc.
Can you help me ?
Change this
<button type="button" class="btn btn-default">Register</button>
to
<button type="submit" class="btn btn-default">Register</button>
and always use dynamic url so that when you deploy your app to server,then you get no error(s)
action="{{ url('/register') }}
Change the type of the button:
<button type="submit"

Resources