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

时间:2016-09-08 17:26:30

标签: php html mysql laravel

我尝试使用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>

2 个答案:

答案 0 :(得分:2)

在您的控制器中:

use DB;

并在您的函数中: 提交后

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

for more

答案 1 :(得分:1)

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

public function submitForm(Request $request)
{

    Maintable::where('profileId', $request->profileId)
             ->where('departmentId', $request->departmentId)
             ->pluck('id');