0

我是 Laravel 4 的新手,我似乎找不到可靠的答案,因为在 Laravel 4 中有几种连接表格的方法。连接两个表格的正确方法是什么?我有一个用户表和一个可用性表。

用户可用性存储在可用性表中。在我的可用性表中,我有一个名为 userid 的列,它与用户相关联。我知道如何运行 sql 查询来加入表,但我不确定如何在 Laravel 4 中执行此操作。

SQL查询

SELECT users.id, users.firstname, users.lastname users.zipcode, availability.dateavailable
FROM users, availability
WHERE users.id=availability.userid

HTML 表格

        <form action="search" method="post" accept-charset="utf-8">
            <div class="form-group">

                <select  name="temptype" class="form-control">
                    <option value="" selected="selected">Select Temp Type</option>
                    <option value="hygienist" >Hygienist</option>
                    <option value="dentist" >Dentist</option>
                    <option value="dentalassistant" >Dental Assistant</option>
                </select>

            </div><!-- end username form group -->
            <div class="form-group">
                <input type="text" name="zipcode" class="form-control" id="zipcode"    placeholder="zipcode">
            </div>
            <div class="form-group">
                <input type="date" name="date" class="form-control" id="date"   placeholder="selectdate">
            </div>
    </div><!-- end .modal-body -->
    <div class="modal-footer">
        <input type="submit" name="submit"  class="btn btn-primary" />
    </div>
    </form>

Laravel 4 用户模型

  public static function getTemps()
    {

       // Create a array of allowed types.
        $types = array('hygienist', 'dentist', 'dentalassistance');

        // Get what type the user selected.
        $type = Input::get('temptype');

        //Get user location
        $location = Input::get('zipcode');

        //Get the date temp is needed
        $date = Input::get('date');

        // Make sure it is a valid type.
        if(!in_array($type, $types))
        {
            return App::abort(500, "Invaild temptype.");
        }

        $temps = DB::table('users')
            ->where('usertype', $type)
            ->where('zipcode', $location)
            ->get();

        var_dump( $temps);
    }
4

1 回答 1

0

我认为这可能对你有用:

DB::table('users')
    ->join('availability', 'users.id', '=', 'availability.userid')
    ->select('users.id', 'users.firstname', 'users.lastname', 'users.zipcode', 'availability.dateavailable')
    ->where('usertype', $type)
    ->where('zipcode', $location)
    ->get();
于 2013-10-05T15:23:43.100 回答