将用户输入发送到控制器的参数

时间:2019-12-13 21:30:03

标签: laravel

我正在一个有订单的laravel项目中工作,我希望人们输入他们的订单代码,然后他们的订单就会出现。我正在努力寻找一种解决方案,以使控制器的功能“显示”知道显示顺序。

TrackController

WITH examples(_servicetype) AS (
  VALUES 
    ('{1,2,3,6015}'::int[]),
    ('{2,1}'::int[]),
    ('{1}'::int[])
)
SELECT _servicetype, '{12, 1, 2, 3}' @> _servicetype
FROM examples;
┌──────────────┬──────────┐
│ _servicetype │ ?column? │
├──────────────┼──────────┤
│ {1,2,3,6015} │ f        │
│ {2,1}        │ t        │  -- set-wise "contains", order does not matter
│ {1}          │ t        │
└──────────────┴──────────┘
(3 rows)

web.php

<?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::find($id);
        return view('track.show',[
            'order' => $order
        ]);
    }
}

track.index

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

所以我的问题是:如何放置表格,以便将用户输入作为变量发送到我的“显示”功能?

编辑:我尝试制作重定向函数,但是我一直得到输出“找不到订单”。

TrackController

@extends('layouts/app')

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

@endsection

web.php

<?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);
        return view('track.show',[
            'order' => $order
        ]);
    }

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

track.index

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

编辑2:

更改了TrackController,但即使我输入了“订单”表中“代码”列中存在的代码,现在它仍继续退出404页面

TrackController

@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

1 个答案:

答案 0 :(得分:0)

首先,您不应该使用Orders::all(),因为它将从表中获取所有行。因此,您可以做的是$order = Order::where('code',$orderCode)->first();。之后,您可以像这样检查

if(!$order){
 abort(404);
}else{
 return response()->redirect('/track/'.$order->code);
}