laravel:查询获取重复记录laravel

laravel:查询获取重复记录laravel,laravel,laravel-4,record,query-builder,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]=&

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

$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) }

first方法返回单个模型实例(illumb\Database\elounce\Collection

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

$count = Message::orderBy('id' , 'desc')->where('to_id' , Auth::id())->orWhere('from_id' , Auth::id())->count();
我认为您发布的结果是您的消息模型的一个实例。所以你不能确定它是否返回了重复的记录


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

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

对象(消息)#412(20){[“可填充的”:受保护的]=>数组(4){[0]=>字符串(5)“到”[1]=>string(7)”从[2]=>string(3)“msg”[3]=>string(4)“seen”}[“protected”:受保护的]=>array(0){}[“connection”:受保护的]=>NULL[“table”:受保护的]=>NULL[“primaryKey”:受保护的]=>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”[“seed”=>string(1)”0”[“created"”]=>string(19)“0000-00-00:00”[“updated"string at”=>string(19)“updated”=>0000-00:00:00”:protected=>数组(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”[“updated_at”=>string(19)“0000-00-00-00:00:00:00”}
“relations]=>string(18)”测试查询”[“seen”=>数组(0)”受保护”=>string(0)“隐藏的”=>:protected]=>array(0){}[“appends”:protected]=>array(0){}[“dates”:protected]=>array(0){}[“touches”:protected]=>array(0){}[“observates”:protected]=>array(0){}[“morphClass”:protected]=>NULL[“exists”=>bool(true)}

要获得更干净的转储,请执行以下操作:

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

告诉我第二个物体在哪里

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

,在我看来就像一个对象。计数确实返回(1)。。但我想知道为什么结果是这样的。但是谢谢你,兄弟