0

I have this function for my delete button

<a href="#" onclick='deleteFile("{{ ($event->id) }}", "{{ ($file->name) }}")' class="btn btn-danger btn-xs delete">L&ouml;schen</a>

and the route in routes/web.php

Route::post('/delete-file', 'MyController@deleteEventFile');

that hits this function

public function deleteEventFile($eventid, $filename){
   dd($eventid);
}

and this is my ajax function:

function deleteFile(eventid, filename){
    //alert(filename);
    //alert(eventid);
    $.ajax({
      url: '/delete-file/',
      type: "post",
      data:{ _token: "{{csrf_token()}}", eventid: eventid, filename: filename },
      dataType: 'json',
    });
}

And i always get this error:

Missing argument 1 for App\Http\Controllers\MyController::deleteEventFile()

my variables can't get through... How to pass the eventid and filename to controller

2 Answers 2

1

According to your code, you are expecting route to give 2 params - eventid & filename into the controller method.

Instead it you should code your method like this:

public function deleteEventFile() {
   $event_id = request()->get('eventid');
   $file_name = request()->get('filename');
}

Fetch the POST data from the laravel's request() method instead.

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

well you got it! but how does this request() knows what are the variables?
@lewis4u - If you find this answer correct and helpful then accept & upvote this answer as it motivates me to give answers to other questions like this and helps others to quickly find the correct answer!
yes i will but there is a time limit 8 min..now it's 3 min and counting. That means you can't accept an answer for 8 min after you post a question. Actually maybe it's 10 min...because when i tried it was 8
0

You can access your parameters in controller using Request like bellow.

public function deleteEventFile(Request $request){
   dd($request->eventid);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.