[英]How to join tables in Laravel 4
我是Laravel 4的新手,我似乎找不到一个可靠的答案,因为Laravel 4中有几种联接表的方法。联接两个表的正确方法是什么? 我有一个用户表和一个可用性表。
用户可用性存储在可用性表中。 在我的可用性表中,有一列与用户关联的名为usersid的列。 我知道如何运行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);
}
我认为这可能对您有用:
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.