Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel中基于表单输入的Mysql查询_Php_Html_Mysql_Laravel - Fatal编程技术网

Php Laravel中基于表单输入的Mysql查询

Php Laravel中基于表单输入的Mysql查询,php,html,mysql,laravel,Php,Html,Mysql,Laravel,我正在尝试使用Laravel在下拉选择列表上的post方法将三个参数传递给控制器。我想不出如何通过第三关。它取决于以下MySQL下的其他两个参数: SELECT id FROM maintable WHERE profileId='ValueOfTheFirstSelection' AND departmentId='ValueOfTheSecondSelection'; 我的表格: <form action="/admin/postmaps" name="form"

我正在尝试使用Laravel在下拉选择列表上的post方法将三个参数传递给控制器。我想不出如何通过第三关。它取决于以下MySQL下的其他两个参数:

SELECT id 
FROM maintable 
WHERE profileId='ValueOfTheFirstSelection' 
      AND departmentId='ValueOfTheSecondSelection';
我的表格:

<form action="/admin/postmaps" name="form" method="post">
                <div class="input-group">
                    <span class="input-group-addon">Profile ID</span>
                    <select class="form-control" name="select1" id="select1">
                        <?php use App\Department; $ids = DB::table('departments')->select('profileId')->get();$a = array();?>
                        @foreach ($ids as $id )
                            <?php  $a[] = $id->profileId; ?>                                
                        @endforeach
                        <?php $a = array_unique($a); ?> 
                        <option disabled selected value>  </option>
                        @foreach ($a as $element )
                            <option value="<?php echo $element; ?>">{{ $element }}</option>

                        @endforeach

                    </select>
                </div>

                <div class="input-group">
                    <span class="input-group-addon">Department ID</span>
                    <select class="form-control" name="select2" id="select2">
                      <?php $ids = DB::table('departments')->select( 'id')->get();$b = array();?>
                        @foreach ($ids as $id )
                            <?php  $b[] = $id->id; ?>                               
                        @endforeach
                        <option disabled selected value>  </option>
                        <?php for ($i = 0; $i < count($b); ++$i) {
                            echo "<option value='$b[$i]'>".$b[$i]."</option>";

                        }
                        ?>

                    </select>
                </div>
                <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
            <button type="submit" class="btn btn-primary" id="updateTerminalInfo">Save</button>
            <input type="hidden" value="{{ Session::token() }}" name="_token">
        </div>
</form>

配置文件ID
@foreach($id作为$id)
@endforeach
@foreach($a作为$element)

永远不要在模板中执行DB查询,这是一种糟糕的做法。提交表单后,使用Eloquent获取信息(我假设您需要一个ID数组):

在控制器中:

use DB;
在你的职能中: 提交后

$r = DB::table('Maintable')->select('id')->where('profileId', $request->profileId)
             ->where('departmentId', $request->departmentId)->get();

如果要在视图中编写查询,则不需要Laravel。你没有注意到MVC范式,Laravel就是围绕它构建的……正如Jonathan所说,你真的应该在你的控制器中加载数据,并在视图中使用它。用数据库调用混乱视图是一种糟糕的形式。
$r = DB::table('Maintable')->select('id')->where('profileId', $request->profileId)
             ->where('departmentId', $request->departmentId)->get();