laravel:查询得到重复的记录laravel



当我编写这个查询时。结果显示我有两张唱片尽管这张桌子只有一个。

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

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

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

如果你需要调试返回了多少行,你可以使用

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

我认为你发布的结果是你的消息模型的一个例子。所以您不能确定它是否返回了重复的记录。

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

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

object(Message)#412(20){["可填充":protected]=>array(4){[0]=>string(5)"to_id"[1]=>string(7)"from_id"[2]=>string(3)"msg"[3]=>string]=>bool(true)["时间戳"]=>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" }["relationship":protected]=>array(0){}["hidden":protect]=>array(0){}【"visible":protect】=>array

对于更清洁的转储,执行以下操作:

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

告诉我第二个物体在哪里?!

最新更新