使用Laravel-Php将输入值传递到URL


Using Laravel - Php pass input value to URL

我的目的是获取操作url表单的输入值。

Html:

<form method="POST" action="/search/the/{{$request->find}}">
{{csrf_field()}}
<div id="check" class="input-group margin-bottom-sm">       
    <input class="form-control" type="text" name="find" placeholder="Search">
    <button type="submit"><div id="search" class="input-group-addon"><i class="fa fa-search"></i></div></button>
</div>
</form>

路线:

Route::post('/search/the/{names}', 'maincontroller@search');
Route::get('/tfind', 'maincontroller@takefind');

控制器:

public function search($names, Request $request)
    {
        //dd($request->find);
        $names = $request->input('find');

    if(!empty($names)){
        $find = DB::table('products')
        ->select('name', 'description', 'price')
        ->where('name', 'LIKE', '%' . $names . '%')
        ->orwhere('description', 'LIKE', '%' . $names . '%')
        ->orwhere('price', 'LIKE', '%' . $names . '%')
        ->get();    
    }
    return view('layouts.search', compact('find', 'names'));
}
public function takefind(Request $request)
{
    $names = $request->input('find');
    return view('layouts.find', compact('names'));
}

当我手动输入通配符时,它会起作用(例如):

<form method="POST" action="/search/the/5">

首先将数字5放在文本字段中,然后按enter键即可!但我想要一个动态的方式!我这样做的主要目的是转换这个URL

http://localhost:8000/search/the/?find=5

到这个

http://localhost:8000/search/the/5

通常使用类似的url查询http://localhost:8000/search/the/?find=5。但如果你想获得类似的urlhttp://localhost:8000/search/the/5。您应该使用javascript来更改表单操作url。因为您想通过在文本字段中输入文本来更改url。

您在routes.php中将names设置为命名路由。因此,您需要替换此

<form method="POST" action="/search/the/5">

带有

<form method="POST" action="{{route('/search/the', ['names' => 5])}}">

具有可变

<form method="POST" action="{{route('/search/the', ['names' => $request->find])}}">