InvalidArgumentException ("Route [{$ name}] not defined."); with laravel 5.7

0

this is the controller,

public function index()
    {
        //$files = File::all();
       //return view('file/create', compact(''));
        $paises = DB::table('clientes')->pluck("nombre","id")->all();
        return view('file/create',compact('paises'));
    }
     public function selectAjax(Request $request)

    {

        if($request->ajax()){

            $usuario = DB::table('clientes')->where('pais',$request->pais)->pluck("nombre","id")->all();
            $data = view('file',compact('usuario'))->render();
            return response()->json(['options'=>$data]);
        }
    }

and then this is the ajax that is in sight,

<option>--- Select State ---</option>

@if(!empty($usuario))

  @foreach($usuario as $value)

    <option value="{{ $usuario }}">{{ $usuario }}</option>

  @endforeach

@endif
  <script type="text/javascript">

  $("select[nombre='pais']").change(function(){

      var id_country = $(this).val();

     // var token = $("input[name='_token']").val();

      $.ajax({

          url: "<?php echo route('file') ?>",

          method: 'POST',

          data: {id_country:pais},

          success: function(data) {

            $("select[nombre='pais'").html('');

            $("select[nombre='pais'").html(data.options);

          }

      });

  });

</script>

Because I get that error, I do not use the routes nor the modal because I do an ajax. but I would like to know if what I did is wrong, or can be done differently.

    
asked by Rojas Rodriguez Ricardo 26.10.2018 в 00:12
source

2 answers

0

Your problem is that you have not defined the path file .

I'll explain: when you create a

Route::resource('file', 'FileController');

Not only do you create a route, but 7 routes, all of them serve to realize the crud of each model, your routes would be the following:

      Url          | Method  | Route  
_____________________________________________________________________
file               | get     |file.index
file               | post    |file.store
file/create        | get     |file.create
file/{file}        | get     |file.show
file/{file}        | put     |file.update
file/{file}        | delete  |file.destroy
file/{file}/edit   | get     |file.edit

You can verify it by running php artisan route:list in console in the direction of your project.

Then the column Route is the one that indicates the names of routes and if you realize there is no one that is called only file .

In your case, apparently you need to get to the function of your controller selectAjax for which I recommend you create another route:

Route::post("selectAjax","FileController@selectAjax")->name("file.selectAjax");

This route redirects us to the function you want. But I explain a bit that line code: selectAjax would become the URL not the path, FileController becomes the controller where the data will be processed, selectAjax is the name of the function inside your FileController driver and finally you just have the route that would become: file.selectAjax .

Then in the case of your ajax the line url: "<?php echo route('file') ?>", is the part that is failing, if we use any route of resource you should put something like:

route('file.index')
route('file.store')
route('file.create')
route('file.show')
route('file.update')
route('file.destroy')
route('file.edit')

which are the routes you have. And if you want to use the post-> selectAjax route:

route("file.selectAjax").

In case you want to enter through the url you can use the facade url() :

url('file')// si el metodo es get se va al index y si es post al store
url('file/create')//redirige a la funcion create
url('file/3')//siendo 3 el id del file este metodo accede a las funciones show cuando el metodo es get,update cuando el metodo es put ó destroy cuando el metodo es delete.
url('file/3/edit')//siendo 3 el id del file este metodo accede a la funcion edit
    
answered by 26.10.2018 / 02:20
source
0

It does not work because you are not sending the input method. If you add a field in tubdata with the name method and some predefined method in your path that you defined with the resourse method, it should work for you. Currently your route needs a parameter that you are not sending. As you were told you should not mix programming languages under any circumstances.

    
answered by 26.10.2018 в 01:05