我是Laravel 4的新手,我似乎无法找到一个可靠的答案,因为在Laravel 4中有几种连接表的方法。连接两个表的正确方法是什么?我有一个用户表和一个可用性表。
用户可用性存储在可用性表中。在我的可用性表中,我有一个名为usersid的列,它与用户相关联。我知道如何运行sql查询来加入表,但我不知道如何在Laravel 4中执行此操作。
SQL QUERY
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 UserModel
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);
}
答案 0 :(得分: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();