如何在 Laravel 4 中连接表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19199310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:31:08  来源:igfitidea点击:

How to join tables in Laravel 4

phpmysqllaravellaravel-4jointable

提问by elodev

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? I have a users table and an availability table.

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

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. 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.

用户可用性存储在可用性表中。在我的可用性表中,有一列名为 userid 的列与用户相关联。我知道如何运行 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);
    }

回答by Antonio Carlos Ribeiro

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