简体   繁体   English

如何在Laravel 4中联接表

[英]How to join tables in Laravel 4

I am new to Laravel 4 and I can't seem to find a solid answer since there are several ways of joining tables in Laravel 4. What is the correct way of joining two tables? 我是Laravel 4的新手,我似乎找不到一个可靠的答案,因为Laravel 4中有几种联接表的方法。联接两个表的正确方法是什么? I have a users table and an availability table. 我有一个用户表和一个可用性表。

A users availability is stored in the availability table. 用户可用性存储在可用性表中。 In my availability table I have a column called usersid which is associated with a user. 在我的可用性表中,有一列与用户关联的名为usersid的列。 I know how to run the sql query to join the table but I am not sure of how to do this in Laravel 4. 我知道如何运行sql查询来联接表,但是我不确定如何在Laravel 4中执行此操作。

SQL QUERY SQL查询

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

HTML FORM 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 UserModel 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);
    }

I think this might work for you: 我认为这可能对您有用:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM