Laravel用户输入表单来查询数据库

时间:2017-03-31 18:45:17

标签: laravel userform

一直在尝试创建一个包含多个字段的laravel表单,用户可以在字段中输入文本/数字,然后使用数据字段并执行数据库查询。现在表单只使用一个字段,但是当我添加更多字段时,它只返回最终查询的数据,而不是其他两个。

香水控制器

class perfumescontroller extends Controller
{
  public function index()
 {
   $pstoreNum = request('pstoreNum');


   $result = perfumes::where('StoreNumber','=',$pstoreNum)
        ->get();

    return view('perfumes',compact('result'));


   }

   public function perfWeekSearch()
    {
   $weekNum = request('perfWeekNum');


   $result = perfumes::where('WeekNumber','=',$weekNum)
        ->get();

    return view('perfumes',compact('result'));


    }


    }

Route :: get(' / perfumes',' perfumescontroller @ index'); Route :: get(' / perfumes',' perfumescontroller @ perfWeekSearch');

刀片:

<form action="perfumes" method="get">
            {{ csrf_field() }}
              <div class="input-group">
                <input type="text" class="form-control" name="perfWeekNum"                     placeholder="Type in Store Number"> 
                  <span class="input-group-btn"> 
                    <button type="submit" class="btn btn-default">
                      <span class="glyphicon glyphicon-search"></span>
                    </button>
                  </span>
              </div>
          </form>

我是否需要使用某种检查,如果不是null方法?或者有更简单的方法吗?

由于

1 个答案:

答案 0 :(得分:0)

我认为下面的工作是您的香水控制器

valgrind

您可以使用以下路线:

  class perfumescontroller extends Controller
  {
     public function index()
     {
       $data = $request->all();

       if(!empty($data['pstoreNum'])){
       $pstoreNum = $data['pstoreNum'];


        $result = DB::table('perfumes')->where('StoreNumber','=',$pstoreNum)
                  ->get();

      return view('perfumes',compact('result'));
      } else if(!empty($data['perfWeekNum'])){
          $weekNum = $data['perfWeekNum'];


          $result = DB::table('perfumes')->where('WeekNumber','=',$weekNum)
                 ->get();

           return view('perfumes',compact('result'));
       }

      }



 }