首页 文章

laravel:查询获取重复记录laravel

提问于
浏览
1

当我写这个查询 . 我在结果中显示了两条记录,尽管该表只有一条 .

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

2 回答

  • 0

    first 方法返回单个模型实例( Illuminate\Database\Eloquent\Collection ) .

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

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

    我认为您发布的结果是Message模型的一个实例 . 所以你不能确定它是否会返回重复记录 .

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

  • 0

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

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

    为了更清洁的转储执行此:

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

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

相关问题