简体   繁体   English

为什么Laravel路由无法按预期工作

[英]why laravel routing not working as expected

Below in my web.php content 下面在我的web.php内容中

Route::get('/tripScheduler', 'trip_scheduler@index')->name('listTripSche');
Route::post('/tripScheduler/create', 'trip_scheduler@store')->name('saveTripSche');
Route::get('/tripScheduler/create', 'trip_scheduler@create');
Route::get('/tripScheduler/{sche}', 'trip_scheduler@show');

Below is my controller file( trip_scheduler.php ) 以下是我的控制器文件( trip_scheduler.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use Redirect;
use DB;
use Illuminate\Support\Facades\Auth;

class trip_scheduler extends Controller
{
     public function __construct(){
        $this->middleware('auth');
    }
   public function show(trip_scheduler $sche){
        print_r($sche);
    }
}

Now when I visite mysite.com/tripScheduler/1 , it should show me the record from database where id is 1. However, it is giving the following output for print_r($sche) 现在,当我访问mysite.com/tripScheduler/1 ,它应该向我显示id为1的数据库中的记录。但是,它为print_r($sche)提供以下输出

App\Http\Controllers\trip_scheduler Object (
    [middleware:protected] => Array (
        [0] => Array ( 
            [middleware] => auth 
            [options] => Array ( )
        )
    )
)

I am not sure where I am going wrong. 我不确定我要去哪里错。 Thanks in advance for your help! 在此先感谢您的帮助!

Your show function like: 您的表演功能如下:

use trip_scheduler;

    public function show(trip_scheduler $sche){
            print_r($sche);
        }

NOTE : trip_scheduler is your model name 注意trip_scheduler是您的型号名称

OR 要么

public function show($sche){
  $rsltTrip = trip_scheduler::find($sche);
                print_r($rsltTrip);
            }

You are type hinting your controller trip_scheduler $sche which is why print_r displaying trip_scheduler controller class. 您正在键入提示您的控制器trip_scheduler $sche ,这就是为什么print_r显示trip_scheduler控制器类的原因。 Try this way 试试这个

class trip_scheduler extends Controller
{
     public function __construct(){
        $this->middleware('auth');
    }
   public function show($sche){
        $resp = YourModal::find($sche);
        return response($resp);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM