我想使用Laravel Eloquent中的orderBy()方法对Laravel 4中的多个列进行排序。查询将使用Eloquent像这样生成:

SELECT *
FROM mytable
ORDER BY
  coloumn1 DESC, coloumn2 ASC

我该怎么做呢?

我有两个表,User和Post。一个用户可以有多个帖子,一个帖子只能属于一个用户。

在我的用户模型中,我有一个hasMany关系…

public function post(){
    return $this->hasmany('post');
}

在我的post模型中,我有一个belongsTo关系…

public function user(){
    return $this->belongsTo('user');
}

现在我想使用Eloquent with()连接这两个表,但想要第二个表中的特定列。我知道我可以使用查询生成器,但我不想这样做。

在Post模型中,我写…

public function getAllPosts() {
    return Post::with('user')->get();
}

它运行以下查询…

select * from `posts`
select * from `users` where `users`.`id` in (<1>, <2>)

但我想要的是…

select * from `posts`
select id,username from `users` where `users`.`id` in (<1>, <2>)

当我用…

Post::with('user')->get(array('columns'....));

它只返回第一个表中的列。我想要第二个表中使用with()的特定列。我该怎么做呢?

我怎么说WHERE (a =1 OR b =1) AND (c =1 OR d =1)

对于更复杂的查询,我应该使用原始SQL吗?

我使用Laravel雄辩的查询构建器,我有一个查询,我想在多个条件上有一个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();

有没有更好的方法,或者我应该坚持这个方法?

给定以下代码:

DB::table('users')->get();

我想得到上面的数据库查询生成器将生成的原始SQL查询字符串。在本例中,它将是SELECT*FROM用户。

我该怎么做?