Laravel重定向问题

时间:2019-12-14 19:51:31

标签: laravel

所以我正在研究一个laravel项目,我希望如果用户输入他们的订单代码,订单将显示详细信息。由于某种原因,订单代码无法通过if语句,因为我得到了输出“找不到订单”。即使我键入订单表中显示的订单代码,也始终如此。

TrackController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Order;

class TrackController extends Controller
{
    public function index()
    {
        return view ('track.index');
    }

    public function show($id)
    {
        $order = Order::where('code', $id)->first();
        return view('track.show',[
            'order' => $order
        ]);
    }

    public function redirect(Request $request)
    {
        $orderCode = $request->input('order-track-id');
        $order = Order::where('code', $orderCode)->first();
        if(!$order){
            return redirect('/track')->with('error', 'Order not found.');
        }else{
            return redirect('/track/' . $order->code);
       }
    }
}

web.php

Route::get('/track', 'TrackController@index');
Route::post('/track/redirect', 'TrackController@redirect');
Route::get('/track/{id}', 'TrackController@show');

track.index

@extends('layouts/app')

@section('content')
<div class="container">
    <div class="row justify-content center">
            {!! Form::open(['action' => 'TrackController@redirect', 'method' => 'post']) !!}
            {!! csrf_field() !!}
            <input type="number" name="input-order-track-id" id="order-track-id">
            {{ Form::button('Track', ['type' => 'submit', 'class' => 'btn btn-primary'] )  }}
            {!! Form::close() !!}
    </div>
</div>

@endsection

我在做什么错,为什么我的功能不能将我带入TrackController中的show功能?

1 个答案:

答案 0 :(得分:0)

在重定向控制器功能中。

public function redirect(Request $request)
    {
        $orderCode = $request->input('input-order-track-id');
        $orders = Order::where('code', $orderCode)->get();
        if($orders->isEmpty()){
            return redirect('/track')->with('error', 'Order not found.');
        }else{
        $order = Order::where('code', $orderCode)->first();
        
            return redirect('/track/' . $order->code);
       }
    }