laravel:查询获取重复记录laravel

时间:2015-11-01 22:46:00

标签: laravel laravel-4 record query-builder

当我写这个查询。我在结果中显示了两条记录 虽然表只有一个。

$side_messages = Message::orderBy('id' , 'desc') ->where('to_id' , Auth::id())->orWhere('from_id' , Auth::id())->first(); dd($side_messages);

结果:

object(Message)#412 (20) { ["fillable":protected]=> array(4) { [0]=> string(5) "to_id" [1]=> string(7) "from_id" [2]=> string(3) "msg" [3]=> string(4) "seen" } ["guarded":protected]=> array(0) { } ["connection":protected]=> NULL ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(7) { ["id"]=> string(1) "1" ["from_id"]=> string(1) "1" ["to_id"]=> string(1) "2" ["msg"]=> string(18) "test_query" ["seen"]=> string(1) "0" ["created_at"]=> string(19) "0000-00-00 00:00:00" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } ["original":protected]=> array(7) { ["id"]=> string(1) "1" ["from_id"]=> string(1) "1" ["to_id"]=> string(1) "2" ["msg"]=> string(18) "test_query" ["seen"]=> string(1) "0" ["created_at"]=> string(19) "0000-00-00 00:00:00" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) }

3 个答案:

答案 0 :(得分:1)

这是一个值而不是两个值,但由于某种原因而重复一次,一个用于#attributes:[],另一个用于#original:[]

答案 1 :(得分:0)

第一个方法返回单个模型实例( Illuminate \ Database \ Eloquent \ Collection )。

如果需要调试返回的行数,可以使用

$count = Message::orderBy('id' , 'desc')->where('to_id' , Auth::id())->orWhere('from_id' , Auth::id())->count();

我认为您发布的结果是Message模型的一个实例。因此,您无法确定它是否会返回重复记录。

第一个方法始终返回单个模型实例。不是模型实例的集合(多记录)。

答案 2 :(得分:0)

此转储您发布的是模型对象,因此它包含所有模型设置。重要的是

部分

对象(消息)#412(20){[" fillable":protected] => array(4){[0] => string(5)" to_id" [1] => string(7)" from_id" [2] => string(3)" msg" [3] =>字符串(4)"看到" } [" guarded":protected] => array(0){} [" connection":protected] => NULL [" table":protected] => NULL [" primaryKey":protected] => string(2)" id" [" perPage":保护] => int(15)["递增"] => bool(true)[" timestamps"] => bool(true)["attributes":protected]=> array(7) { ["id"]=> string(1) "1" ["from_id"]=> string(1) "1" ["to_id"]=> string(1) "2" ["msg"]=> string(18) "test_query" ["seen"]=> string(1) "0" ["created_at"]=> string(19) "0000-00-00 00:00:00" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } ["original":protected]=> array(7) { ["id"]=> string(1) "1" ["from_id"]=> string(1) "1" ["to_id"]=> string(1) "2" ["msg"]=> string(18) "test_query" ["seen"]=> string(1) "0" ["created_at"]=> string(19) "0000-00-00 00:00:00" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } [" relations":protected] => array(0){} [" hidden":protected] => array(0){} [" visible":protected] => array(0){} ["追加":protected] => array(0){} [" dates":protected] => array(0){} [" touches":protected] => array(0){} [" observables":protected] => array(0){} [" with":protected] => array(0){} [" morphClass":protected] => NULL ["存在"] => bool(true)}

对于更干净的转储执行此操作:

 $side_messages = Message::orderBy('id' , 'desc')->where('to_id' , Auth::id())->orWhere('from_id' , Auth::id())->first();
        dd($side_messages->toArray());

告诉我第二个对象在哪里?!