Laravel - Action not defined - php

I create a CRUD application in Laravel 7 and MongoDB. Adding items to the collection works fine. But I wanted to delete an item and unfortunately Laravel stopped liking me... I get an error:
Action App\Http\Controllers\InQuestController#destroy not defined.
I checked a dozen times and wrote such a function
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Quests;
use DB;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class InQuestsController extends Controller
{
...
public function destroy($id) {
$quest = Quests::find($id);
$quest->delete();
return redirect('inside');
}
}
To function I am reffering from:
<form action="{{ action('InQuestsController#destroy', $quest->id) }}" method="POST">
#csrf
<input name="_method" type="hidden" value="DELETE">
<button id="details" class="idButton detailsButton" type="button"><img src="{{ asset('images/arrow.png') }}" alt="" class="icon arrow"></button>
<button class="editButton" type="button"><img src="{{ asset('images/pencil.svg') }}" alt=""></button>
<button class="deleteButton" type="submit"> <img src="{{ asset('images/bin.png') }}" alt=""></button>
</form>
What's wrong?

FIX:
In web.php add line:
Route::delete('{id}', 'InQuestsController#destroy')->name('destroy');

Related

Having bug with destroy and update method in Laravel

I'm trying to create simple Laravel application to use resource controller, but i'm having some kind of bug in update and destroy method when I pass the object itself to method.
My update and destroy functions are simple, they just take $todoList and either updates or deletes that object. However, they seem not working as expected. I try to dd($todoList) and it returns correct value, but does not update the database.
public function update(TodoList $todoList)
{
$todoList->update(['completed' => 1]);
return redirect()->route('todos.index');
}
public function destroy(TodoList $todoList)
{
$todoList->delete();
return redirect()->route('todos.index');
}
My blade template looks like this:
<form action="{{route('todos.destroy', $todo)}}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-link btn-sm float-end"><i class="fas fa-trash"></i></button>
</form>
<form action="{{route('todos.update', $todo)}}" method="POST">
<span #class([$todo->completed ? 'bg-success':''])>{{$todo->content}}</span>
#csrf
#method('PATCH')
<button type="submit" class="btn btn-link btn-sm float-end"><i class="fas fa-edit"></i></button>
</form>
I have also tried to pass $todo-id to route, but none seem to be working. I have checked documentation and I'm usually working with documentations, but this seems to be interesting bug to me.
My route is
Route::resource('/todos', TodoListController::class);
and finally my Model is like following
class TodoList extends Model
{
use HasFactory;
protected $fillable = ['content'];
protected $guarded = [];
}

Laravel - get data from input field not working

i'm having trouble to get data from an input.
I try to be more specific
My application has many views, and each of these has an #include component that works as a search field.
For example the user types in the input the ID of the store, the controller compares the ID that user inserted with the DB store's ID and then compacts data and fill the views with infos of that specific store.
Im just testing how to get that data from the input but i'm getting this error:
Route [search.get_kcli] not defined.
Actually i'm trying to use that function only for get data by using a controller only for that input field.
What's wrong in it?
Thanks for help!
My code looks like this:
inside of app.blade.php
#auth
#include('partials.search')
#endauth
inside search.blade.php
<form method="POST" class="form-inline position-relative"
action="{{ route('search.get_kcli') }}">
#csrf
#method('POST')
<input class="form-control shadow-none" name="kcli" id="kcli" type="number"
placeholder="Codice..." aria-label="Search">
<button type="submit" class="btn btn-light search-btn"><i class="fas fa-search"></i></button>
</form>
Inside the SearchController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class SearchController extends Controller
{
function get_kcli(Request $request) {
$kcli = $request->input('kcli');
dd($kcli);
}
}
Inside web.php
Route::post('/search', [App\Http\Controllers\SearchController::class, 'get_kcli'])->name('search');
Change
<form method="POST" class="form-inline position-relative"
action="{{ route('search.get_kcli') }}">
to
<form method="POST" class="form-inline position-relative"
action="{{ route('search') }}">

Passing variable from button to controller Laravel

I am having a little routing problem in Laravel 5.2. I have a result page which shows detailed information about personnel. I would like a button, which when enabled, generates a PDF page. Passing the variables has been a problem but I am very close now! I will public my code to elaborate.
result page
<form action="generatePDFpage" method="get">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</form>
routes.php
Route::get('/dashboard/result/generatePDFpage', 'resultController#GeneratePDFc');
GeneratePDFc controller
public function GeneratePDFc(){
$id_array_implode = "HALLO";
$pdf= PDF::loadView('GeneratePDF', ["test"=>$id_array_implode])->setPaper('a4', 'landscape');
return $pdf->stream('invoice.pdf');
}
So, on the result page I am using a array ($id_array) to search the database for the matching records. I need to pass this variable onto the GeneratePDFc controller, so that I can pass that again to the loadView function!
Could someone please help me out? :-)
When you're using get method, you can do just this:
<a href="{{ route('route.name', $parameter) }}">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</a>
For other methods you can use something like this (this one is for DELETE method):
<form method="POST" action="{{ route('route.name', $parameter) }}" accept-charset="UTF-8">
<input name="_method" type="hidden" value="DELETE">
{{ csrf_field() }}
<button type="submit" class="btn btn-sm btn-default">Generate PDF!</button>
<input type="hidden" value="someVariable" />
</form>
To get variable, use something like this:
public function generatePDF(Request $request)
{
$someVariable = $request->someVariable;
I don't know Laravel but I think when in your action="" of the form you can put your route with its parameters no ?
I've found it here : https://laravel.com/docs/4.2/html#opening-a-form
And access the variable in your controller using the $request var

Laravel 5.1.26 : MethodNotAllowedHttpException in RouteCollection.php line 219

I will get MethodNotAllowedHttpException when submitting a form in laravel
HTML file
<form action="{{ action('HomeController#store') }}" method="post">
<input name="_method" type="hidden" value="PATCH">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<input type="submit" name="Submit" value="submit">
</form>
Im my routes.php
Route::post('formaction','HomeController#store')
Controller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class HomeController extends Controller
{
public function store(){
echo 'form submitted';
}
}
Why i will get MethodNotAllowedHttpException in my form action page?
I've refereed some questions related to this,but nothing help me
Even if the form is using the POST method, you are sending the extra param _method which will let the framework know that you want to use that method instead. If you send that extra param then you should set the route accordingly:
Route::patch('formaction','HomeController#store');

delete data from database using model in laravel 5

I am trying to delete data from my table employes by getting the id of the data and transferring it by using get route but i get the following error
ModelNotFoundException in Builder.php line 125: No query results for
model [App\employe].
her is my route
Route::get('/delete/{employe}', 'EmployeController#delete');
Route::post('/delete', 'EmployeController#handleDelete');
and i have model called employe to access my table
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class employe extends Model {
protected $fillable = array('fname','lname','age','sex','phone','email','houseno','city','kebele','state','username','password','workposition','salary','bankaccount','bankname','contactid','employedate');
}
part of my controller for deleting
public function delete(employe $employe)
{
// Show delete confirmation page.
return View('deleteemploye', compact('employe'));
}
public function handleDelete()
{
$employe = employe::findOrFail(Input::get('employe'));
$employe->delete();
return Redirect::action('EmployeController#index');
}
her is my view for deleting file
#extends('app')
#section('content')
<div class="page-header">
<h2>Delete {{$employe->fname}} Are you sure?</h2>
</div>
<form action="{{ action('EmployeController#handleDelete') }}" method="post" role="form">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="employe" id="employe" value="{{ $employe->id }}" />
<input type="submit" class="btn btn-default" value="Yes" />
No
</form>
#stop
Looks like laravel cant find the model with the primary key you are passing. Check the database and see if its there. If it is check the deleted_at field. If it has a date in it, it means it is already deleted. You can still find it by using withTrashed()
$employe = employe::withTrashed()->findOrFail(Input::get('employe'));
and if you have soft deleting enabled for this model, you can completely remove the given row by running
$employe = employe::withTrashed()->findOrFail(Input::get('employe'))->forceDelete();

Categories