Route not defined even I defined it - laravel

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>

Related

why my database does not update ? (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.

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

The POST method is not supported for this route. Supported methods: GET, HEAD

Iam trying to update a specific data using post method. After submitting the form it shows an error : The POST method is not supported for this route. Supported methods: GET, HEAD.
editpage.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<h3>Update Book</h3>
<br>
<form action="update" method="post" >
{{csrf_field()}}
#foreach($array as $fetch)
<div><input type="hidden" name="id" value="{{$fetch->id}}"></div>
<div><input type="text" name="name" class="form-control " placeholder="Bookname" value="{{$fetch->name}}" ></div><br>
<div><textarea name="content" class="form-control" rows="5" placeholder="Description" >{{$fetch->content}}</textarea></div><br>
<div><input type="text" name="author" class="form-control" placeholder="Author" value="{{$fetch->author}}"></div> <br>
<div><input type="submit" name="submit" value="Update Book" class="btn btn-success" ></div>
#endforeach
</form>
</div>
#endsection
Web Route
Route::get('/', function () {
return view('welcome');
});
Route::get('/addbook',function () {
return view('AddBook');
});
Route::post('/insert',['uses'=>'BookController#insert']);
Route::get('/delete/{id}',['uses'=>'BookController#delete']);
Route::get('/edit/{id}',['uses'=>'BookController#edit']);
``````
Route::post('/update',['uses'=>'BookController#update']);
```````
Route::get('/home',['uses'=>'BookController#index']);
Auth::routes();
There are action like update which requires the method submitted to the server url to be either PUT/PATCH (to modify the resource)
Try with this,
<form action="{{ route('book.update') }}" method="post" >
{{csrf_field()}}
{{ method_field('PUT') }}
#foreach($array as $fetch)
// ...
#endforeach
</form>
Your Route,
Route::put('update',['uses'=>'BookController#update', 'as' => 'book.update']);
Your Controller
public function update(Request $request)
{
// ...
}
Hope this helps :)

Laravel undefined variable: search

Version of Laravel 5.0 and following is my code:
My form:
<form method="get" action="{{ url('result')}}" class="search-wrap" >
<div class="form-group">
<input type="text" class="form-control search" placeholder="Search" name="key"/>
<button class="btn btn-primary submit-search text-center" type="submit">
<i class="icon-search"></i>
</button>
</div>
</form>
My controller:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\products;
public function searchName(Request $request){
$key = $request->key;
$data = product::where('name','like','%'.$key.'%')->get();
return view('footwear/result',['search'=>$data]);
}
My route:
Route::get('result', 'myControler#searchName');
My view:
<?php
var_dump($search);
?>
I receive the following error: (Undefined variable: search)
Problem is in your routes, why don't you
Try:
Route::post('result', 'myControler#searchName');
or
Route::any('result', 'myControler#searchName');
change method to post in form too, and try.
NOTE: I should have added this as comment but my rep is less than 50.

Resources