2013-05-13 43 views

回答

2

你必須有一個對應於table1的對象。

雄辯對象:

class User extends Eloquent { 

    protected $table = 'table1'; 

    ... 
} 

ORM查詢:

$user = User::where('name', 'paul') 
       ->where('id', 1) 
       ->first(); 
+0

並不完全正確,你可以用'DB ::表( '表1')'代替雄辯模型。此外,你只需要'where或'orWhere'鏈接(而不是'和'')。 – 2013-05-13 18:41:06

+0

的確如此,你可以用流利的語言來做到這一點,但他要求雄辯。修正了哪裏,謝謝指點。 – 2013-05-13 18:47:40

+0

感謝它的作品太 – Ruben 2013-05-16 17:06:32

11

簡單,使用其他where

Model::where('name', '=', 'paul')->where('id', '=', 1); 

然後你可以使用get()first()來獲取行(S) 。

如果您只想使用查詢生成器(流利),然後用DB::table('table1')->替換Model::

注意

  • =是可選在這裏。在這裏你可以使用其他操作符。

更新

從Laravel 4.2,你也可以使用數組:

Model::where([ 
       'name' => 'paul', 
       'id' => 1 
      ]); 
+0

謝謝,它的作品! – Ruben 2013-05-16 17:06:01

+1

關於查詢的另一個問題,我想執行一個查詢類似於「SELECT r.id,avg(p.puntuacio) FROM receptes AS r,puntuacio_receptes_usuaris AS p WHERE r.id = p.recepta_id GROUP BY r .id「,但我不知道我該怎麼做 – Ruben 2013-05-16 17:07:49

+0

你可以請發佈這個作爲一個新的問題,我會在我的空閒時間回答它? – Usman 2013-05-16 17:23:28