I describe the destroy method of the TasksController class:
I wrote this function:
public function destroy ($ id) {
$ task = Task :: find ($ id);
$ task- & gt; delete ();
return redirect () - & gt; route ('tasks.index');
}
find () does not work and if you hover the mouse over it, it says that method find not found in App \ Task.
Full controller code:
& lt;? php
namespace App \ Http \ Controllers;
use Illuminate \ Http \ Request;
use App \ Task;
class TasksController extends Controller
{
public function index () {
$ tasks = Task :: getAllTasks ();
return view ('tasks.index', compact ('tasks'));
}
public function create () {
return view ('create');
}
public function store (Request $ request) {
$ task = new Task;
$ task- & gt; fill ($ request- & gt; all ());
$ task- & gt; save ();
return redirect () - & gt; route ('tasks.index');
}
public function destroy ($ id) {
$ task = Task :: find ($ id);
$ task- & gt; delete ();
return redirect () - & gt; route ('tasks.index');
}
}
I searched a lot on the Internet, but did not find specific answers, including on stackoverflow (eng). They write that maybe I connected something wrong, but I don’t see an error. Please point in the right direction.
Code of the used Task model:
& lt;? php
namespace App;
use Illuminate \ Database \ Eloquent \ Model;
class Task extends Model
{
protected $ fillable = ['body', 'id'];
public static function getAllTasks () {
return self :: all ();
}
}
The strangest thing is that the fill (), save (), all () methods work fine.
I am making additions. Since the beginning of the question, I found such an interesting point:
public function destroy ($ id) {
$ task = Task :: all () - & gt; find ($ id) - & gt; delete ();
return redirect () - & gt; route ('tasks.index');
}
In this case, find () works successfully, delete () works as expected, but still highlights unhandled \ exception.
With this approach, can the question be considered closed?
Answer 1
Take action:
- Verify
App \ Task
class inheritsIlluminate \ Database \ Eloquent \ Model
- Change the
destroy
method like this:
public function destroy ($ id) {
/ ** @var App \ Task $ task * /
$ task = Task :: find ($ id);
$ task- & gt; delete ();
// why would you want to show task pages? You deleted it.
// return view ('tasks.show', compact ('task'));
// better redirect to the task list page
// it will work here if your resource route has the name `tasks`
return redirect () - & gt; route ('tasks.index');
}
- Run command in console
composer dumpautoload
Answer 2
The problem lies in the framework itself, for some reason it does not detect some methods.
Solved the problem with _ide_helper.
Link to the source of the solution, the question is less specific (generalized), but the solution fits:
https://stackoverflow.com/questions/29439753/eloquent-orm-code-hinting- in-phpstorm
Solution: Write to the console in the root of the project – “php artisan ide-helper: models”
Answer 3
Try this option
public function destroy ($ id) {
$ task = new Task ();
$ result = $ task- & gt; find ($ id);
.....
}
Answer 4
public function destroy ($ id) {
$ task = Task :: all () - & gt; find ($ id) - & gt; delete ();
return redirect () - & gt; route ('tasks.index');
}
Incorrect removal approach. In this case, you pull out ALL tasks, and then work with the collection (that is, you are looking for the first one that matches the id)
Use the destroy
or delete
method directly to not select this model
public function destroy ($ id) {
$ task = Task :: destroy ($ id);
return redirect () - & gt; route ('tasks.index');
}
// or
public function destroy ($ id) {
$ task = Task :: where ('id', $ id) - & gt; delete (); // the request will work immediately for deletion, instead of 2 requests (find and delete), it will be 1 (delete)
return redirect () - & gt; route ('tasks.index');
}
And as for what he writes about the unknown method, this is, as mentioned above, the ide problem. It is solved by connecting all the facades, but at the same time (for me personally) other problems arose. Therefore, one cannot but pay attention to it. They work great.