当尝试在控制器中传递带有两个参数的命名路由时,出现 500 内部错误

因此,当我将路由名称与单个参数放在一起时,它可以完美地工作,但是当我传递带有两个参数的命名路由时,我在控制台中收到 500 错误,如下所示:GET http://127.0.0.1:8000/admin/packages/package-programs/kathmandu/action?query= 500 (Internal Server Error)。


<?php


namespace App\Http\Controllers\AdminVisible;


use Illuminate\Http\Request;

use Illuminate\Support\Str;

use App\Program;

use App\Package;

use DB;


class PackageProgramController extends Controller

{

    public function __construct()

    {

        $this->middleware('auth');

    }


    public function index($packageSlug)

    {

        $showCounts = Program::count();

        $packages = Package::firstOrFail();

        return view('admin.pages.packageprogram',compact('showCounts','packageSlug','packages'));      

    }


    function action($packageSlug,Request $request)

    {

        if($request->ajax())

        {

         $output = '';

         $query = $request->get('query');

         if($query != '')

         {

          $data = DB::table('programs')

            ->where('id', 'like', '%'.$query.'%')

            ->orWhere('day', 'like', '%'.$query.'%')

            ->orWhere('Package_Type', 'like', '%'.$query.'%')

            ->orWhere('title', 'like', '%'.$query.'%')

            ->orderBy('id', 'desc')

            ->get();

            

         }

         else

         {

          $data = DB::table('programs')

            ->orderBy('id', 'desc')

            ->get();

         }

         $total_row = $data->count();


         if($total_row > 0)

         {

          foreach($data as $row)

          {



智慧大石
浏览 119回答 1
1回答

繁星淼淼

首先从blade开始(请查看评论):@extends('layout')@section('content')&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <table class="table table-hover table-striped table-dark" id="slugTb">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col"><input type="checkbox" id="checkHead" class="selectall"></th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Id</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Day</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Title</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Description</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; <tbody></tbody>&nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; </div>@endsection@section('scripts')&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; $(document).ready(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var query = 'damnSon';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: "{{ route('test.action') }}",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method: 'GET',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'slug': '{{ $packageSlug }}',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'query': query&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .done(function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(data) //use console.log to debug&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#slugTb tbody').html(data.table_data); //set table id so that you don't miss the right one&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .fail(function(err) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(err) //in case if error happens&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .always(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log( "complete" ); //result despite the response code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script>@endsection您使用了已弃用的 jquery 方法,例如success check最好使用这三个:done, fail,always下一路线 web.php:Route::get('action', ['as' => 'test.action', 'uses' => 'TestController@action']);在您的情况下,最好使用Requestparams bag,以便您可以添加所需数量的参数。下一个控制器:function action(Request $request){&nbsp; &nbsp; $total_row = 1;&nbsp; &nbsp; $packageSlug = $request->get('slug'); //names that you set in ajax data tag: {'slug': '{{ $packageSlug }}','query': query}&nbsp; &nbsp; $query = $request->get('query');&nbsp; &nbsp; $output = '<tr>&nbsp; &nbsp; &nbsp; &nbsp;<td align="center" colspan="1">' . $packageSlug . '</td>&nbsp; &nbsp; &nbsp; &nbsp;<td align="center" colspan="1">' . $query .'</td>&nbsp; &nbsp; &nbsp; </tr>';&nbsp; &nbsp; &nbsp; &nbsp; $data = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'table_data'&nbsp; => $output,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'total_data'&nbsp; => $total_row&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; return response()->json($data);}您应该从控制器返回一些内容,以便 Blade 可以显示数据并对其进行 json 编码,以便 js 可以解析它。因此return response()->json($data);另一种方式:路线:Route::get('/action/{slug}/{query}',['as' => 'test.action', 'uses' => 'TestController@action']);刀片脚本:<script>&nbsp; &nbsp; $(document).ready(function() {&nbsp; &nbsp; &nbsp; &nbsp; var query = 'damnSon';&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'action/{{ $packageSlug }}/' + query,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method: 'GET',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .done(function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(data) //use console.log to debug&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#slugTb tbody').html(data.table_data); //set table id so that you don't miss the right one&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .fail(function(err) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(err) //in case if error happens&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .always(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log( "complete" ); //result despite the response code&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });</script>和控制器:function action($slug, $query){&nbsp; &nbsp; $total_row = 1;&nbsp; &nbsp; $packageSlug = $slug;&nbsp; &nbsp; $query = $query;&nbsp; &nbsp; $output = '<tr>&nbsp; &nbsp; &nbsp; &nbsp;<td align="center" colspan="1">' . $packageSlug . '</td>&nbsp; &nbsp; &nbsp; &nbsp;<td align="center" colspan="1">' . $query .'</td>&nbsp; &nbsp; &nbsp; </tr>';&nbsp; &nbsp; &nbsp; &nbsp; $data = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'table_data'&nbsp; => $output,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'total_data'&nbsp; => $total_row&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; return response()->json($data);}不推荐,因为您在ajax请求中手动输入路由:url: 'action/{{ $packageSlug }}/' + query如果您的路由发生变化,您必须在js中更改它。
打开App,查看更多内容
随时随地看视频慕课网APP