给出以下代码:
DB::table('users')->get();
Run Code Online (Sandbox Code Playgroud)
我想获取上面的数据库查询生成器将生成的原始SQL查询字符串.在这个例子中,它将是SELECT * FROM users.
我该怎么做呢?
我正在使用Laravel Eloquent查询构建器,我有一个查询,我希望WHERE在多个条件下有一个子句.它有效,但它并不优雅.
例:
$results = User::where('this', '=', 1)
->where('that', '=', 1)
->where('this_too', '=', 1)
->where('that_too', '=', 1)
->where('this_as_well', '=', 1)
->where('that_as_well', '=', 1)
->where('this_one_too', '=', 1)
->where('that_one_too', '=', 1)
->where('this_one_as_well', '=', 1)
->where('that_one_as_well', '=', 1)
->get();
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点,还是我应该坚持这种方法?
我怎么说 WHERE (a = 1 OR b =1 ) AND (c = 1 OR d = 1)
对于更复杂的查询,我应该使用原始SQL吗?
我想通过使用orderBy()Laravel Eloquent中的方法对Laravel 4中的多个列进行排序.查询将使用Eloquent生成,如下所示:
SELECT *
FROM mytable
ORDER BY
coloumn1 DESC, coloumn2 ASC
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
如何使用Laravel Query Builder或Eloquent ORM在Laravel 3/4中检索原始执行的SQL查询?
例如,像这样:
DB::table('users')->where_status(1)->get();
Run Code Online (Sandbox Code Playgroud)
要么:
(posts (id, user_id, ...))
User::find(1)->posts->get();
Run Code Online (Sandbox Code Playgroud)
否则,至少我如何保存执行到laravel.log的所有查询?
I have two tables, User and Post. One User can have many posts and one post belongs to only one user.
In my User model I have a hasMany relation...
public function post(){
return $this->hasmany('post');
}
Run Code Online (Sandbox Code Playgroud)
And in my post model I have a belongsTo relation...
public function user(){
return $this->belongsTo('user');
}
Run Code Online (Sandbox Code Playgroud)
Now I want to join these two tables using Eloquent with() but want specific columns from the second table. I know I can use the Query …
如果新记录不存在,插入新记录或更新的简写是什么?
<?php
$shopOwner = ShopMeta::where('shopId', '=', $theID)
->where('metadataKey', '=', 2001)->first();
if ($shopOwner == null) {
// Insert new record into database
} else {
// Update the existing record
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Laravel查询生成器的JOIN查询添加条件.
<?php
$results = DB::select('
SELECT DISTINCT
*
FROM
rooms
LEFT JOIN bookings
ON rooms.id = bookings.room_type_id
AND ( bookings.arrival between ? and ?
OR bookings.departure between ? and ? )
WHERE
bookings.room_type_id IS NULL
LIMIT 20',
array('2012-05-01', '2012-05-10', '2012-05-01', '2012-05-10')
);
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用Raw表达式,但之后会有SQL注入点.我已经尝试了以下查询生成器,但生成的查询(显然,查询结果)不是我想要的:
$results = DB::table('rooms')
->distinct()
->leftJoin('bookings', function ($join) {
$join->on('rooms.id', '=', 'bookings.room_type_id');
})
->whereBetween('arrival', array('2012-05-01', '2012-05-10'))
->whereBetween('departure', array('2012-05-01', '2012-05-10'))
->where('bookings.room_type_id', '=', null)
->get();
Run Code Online (Sandbox Code Playgroud)
这是Laravel生成的查询:
select distinct * from `room_type_info`
left join `bookings`
on `room_type_info`.`id` = `bookings`.`room_type_id` …Run Code Online (Sandbox Code Playgroud) $ book = array('book1','book2'); $ book数组元素数是可变的.它可能有2个元素或20个元素
我需要进行这样的查询:
select * from book where bookname like %book1% or bookname like %book2%
Run Code Online (Sandbox Code Playgroud)
要在laravel 5中进行此查询,可以选择:
$name = DB::Table('bookinfo')
->select('*')
->wherein('bookname',$book)
->get();
Run Code Online (Sandbox Code Playgroud)
但它使用=运算符我需要使用like运算符
我正面临多形关系中的问题,我无法在哪里工作.基本上我有一个"where"条件,我想申请.关系代码正常工作以返回相关模型,但一旦应用whereHas它就会返回错误.
下面是代码
订单类:
class Order extends Model
{
// function to return orders
public static function getAllOrders()
{
return $orders = Order::with('part.pcategory')->whereHas('part', function ($query)
{
$query->where('cat_id',4);
})->get();
}
// the relation
public function part()
{
return $this->morphTo(null,'department_short_code','part_stock_number','stock_number', 'dep_short_code');
}
}
Run Code Online (Sandbox Code Playgroud)
SFD零件类:
class sfd_part extends Model
{
public function orders()
{
return $this->morphMany('App\Order','part','department_short_code','part_stock_number');
}
public function pcategory()
{
return $this->belongsTo('App\Pcategories','cat_id', 'category_id');
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用getAllOrders()时,它会给出以下错误
SQLSTATE [42S22]:未找到列:1054'where子句'中的未知列'cat_id'(SQL:选择count(*)作为
orders存在的聚合(select*fromordersaslaravel_reserved_0wherelaravel_reserved_0.id=laravel_reserved_0.part_stock_number和cat_id …
php foreign-keys relational-database laravel-query-builder laravel-5.6
php ×9
laravel ×8
eloquent ×6
laravel-4 ×3
orm ×2
foreign-keys ×1
laravel-5 ×1
laravel-5.6 ×1
sql ×1