A
A
Apostol632020-01-28 14:48:28
Laravel
Apostol63, 2020-01-28 14:48:28

Why doesn't delete work in laravel?

Hello. In fact, the problem has already been solved, but I am haunted by the fact that I could have solved it incorrectly. And in general, the very fact that I don’t understand how and what works (until the end)

The bottom line is this
. I have an axios request to the controller:

axios.post('/deletefolderntd', {id: this.fornewfolder[0].id}).then(response => {
        console.log(response.data);
      });

My route is the following:
Route::post('/deletefolderntd', '[email protected]');

The controller was like this (when it didn't work):
public function deleteFolder(Request $request) {
    $id = $request->id;
          $model = new Filemanager();
    $result = $model->deleteFolderById($id);
  }

The Filemanager model and its own deleteFolderById method:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Filemanager extends Model {
  protected $table = 'folders';
  protected $connection = 'sqlite';
     	public function deleteFolderById($id) {
    return $this->where('id', $id)->delete();
  }
}

So. In this situation, my answer was 0 and nothing was deleted
. If I changed the route from post to get, then everything worked and was deleted. (I removed Request in the controller)
But I need the deletion to be through axios.post and only this version of the controller works
public function deleteFolder(Request $request) {
    $id = $request->id;
    $model = Filemanager::find($id);
    $result = $model->delete();
}

There is a little, but not much (as for me) the request differs.
Did I do everything right? And why didn't it work with where, but with find?

PS
Thanks in advance. I'm trying to figure it out)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
ettychel, 2020-01-28
@ettychel

Can it be otherwise

public function destroy(Request $r) {
    return Filemanager::destroy($r->id);
}

But in general, both will work.
Regarding POST, as mentioned above, the CSRF token could very well be the reason. Look at what the server and logs return, it will clearly describe whether CSRF is to blame or not.
In addition, about the request method. To delete, use the DELETE request, your routes will clearly reflect what they do, then it will be convenient to look
php artisan route:list

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question