在laravel 5.6中创建搜索表单

时间:2018-06-30 23:06:26

标签: php html mysql laravel forms

过去一天左右,我一直在学习Laravel,并设法在WAMP设置上成功创建了一些页面,这些页面显示了数据库查询的结果。我正在尝试在页面上添加一个表格,该表格使我可以对结果进行过滤和排序。我遇到了一个问题,我遇到了一个ReflectionException,指出“类App \ Http \ Controllers \ Result不存在。”

我有一个控制器

 <?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Input;

class ResultsController2 extends Controller
{
    /*  
        Display results for search query on cars table.
    */

    public function show_all()
    {
        //This will query the database for our cars
        //$cars = DB::connection('mysql')->select("select * from cars");

        $cars = \DB::select('SELECT * FROM `cars` INNER JOIN makes on cars.make=makes.id INNER JOIN models on cars.model=models.id');
        $makes = \DB::select('SELECT * FROM `makes`');

        return view('results2',['cars'=>$cars,'makes'=>$makes]);
    }

    public function show_all_filtered(Result $result)
    {
        $QueryAppend = "";
        if(Input::post())
        {
            $formMake = Input::post('make');
            $QueryAppend = ' WHERE cars.make=`'.$formMake.'`';//This only filters the results if there has been a form selection
        }
        //This will query the database for our cars
        //$cars = DB::connection('mysql')->select("select * from cars");

        $cars = \DB::select(
            'SELECT * FROM `cars` 
            INNER JOIN makes on cars.make=makes.id 
            INNER JOIN models on cars.model=models.id'
            .$QueryAppend);
        $makes = \DB::select('SELECT * FROM `makes`');

        return view('results2',['cars'=>$cars, 'makes'=>$makes]);
    }   
}

我的路线如下:

    Route::get('/', 'ResultsController@show_all');

Route::get('/question3','ResultsController2@show_all');
Route::post('/question3','ResultsController2@show_all_filtered');

Route::get('/question4','ResultsController2@show_all');

我的剑刃看起来像这样:

    <!== This should be the template for the results for the database query -->


@extends('layouts.app')

@section('title', 'Question 2')

@section('content')
    <p>Results</p>
    <div class="grid">
        <div class="col100">Make</div><div class="col100">Model</div><div class="col100">Reg</div><div class="col100">Mileage</div>
    </div>
    <div class="clear"></div>
    @foreach ($cars as $car)
        <div class="grid">

                <div class="col100">
                {{ $car->make }}        
                </div>
                <div class="col100">
                {{ $car->model }}       
                </div>
                <div class="col100">
                {{ $car->number_plate }}        
                </div>
                <div class="col100">
                {{ $car->mileage }}     
                </div>

        </div>
        <div class="clear"></div>
    @endforeach
    <div class="clear"></div>
    {{ Form::open( ['action'=> 'ResultsController2@show_all_filtered'] ) }}

        <select name"make">
            @foreach ($makes as $make)
                <option value="{{ $make->make }}">{{ $make->make}}</option>
            @endforeach
        </select>
        {{ Form::submit('Update') }}
    {{ Form::close() }}
    <div class="grid">
        <div class="col300">
@endsection

当我导航到/ question3时,我得到了结果列表和搜索表单,但是当我提交表单时,就会发生错误。如果您还有其他问题,我们很乐意为您解答

1 个答案:

答案 0 :(得分:0)

您需要在控制器顶部导入public class MyFragment extends Fragment implements OnMapReadyCallback { GoogleMap mgoogleMap; MapView mapView; View view; public MyFragment(){ } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);} @Override public View onCreateView(LayoutInflater inflater , ViewGroup container, Bundle savedInstanceState){ view = inflater.inflate(R.layout.content_main,container , false); return view; } @Override public void onViewCreated(View view,Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mapView = view.findViewById(R.id.map); if(mapView!=null) mapView.onCreate(null); mapView.onResume(); mapView.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap){ MapsInitializer.initialize(getContext()); mgoogleMap = googleMap; googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); googleMap.addMarker(new MarkerOptions().position(new LatLng(23.2599,77.4126)).title("BHOPAL")); CameraPosition bhopal = CameraPosition.builder().target(new LatLng(23.2599,77.4126)).zoom(16).bearing(0).build(); googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(bhopal)); } 类:

Result

其中use App\Path\To\Result; 部分是App\Path\To\类的完整名称空间。

问题在于此功能:

Result

PHP假定类public function show_all_filtered(Result $result) 为当前名称空间,因为它不是通过Result显式导入的。