“Property of an object inside of for each returns: ‘Attempt to read property on null'”

huangapple go评论67阅读模式
英文:

Property of an object inside of for each returns: "Attempt to read property on null"

问题

我尝试在for-each循环内获取属性$seen->seen,但我得到了以下错误信息:

> "尝试在null上读取属性"seen""

当我在for-each循环之外执行时,它可以正常工作

public function getromes(Request $request)
{
    $user = Auth::id();
    $romes = Rome::where('room', $request->id)->get();
    $romeids = $romes->pluck('uuid');

    // $seen = Seen::where('message', 'IcHshOP2V')
    //    ->where('user', $user)
    //    ->first();
    // $wasseen = $seen->seen;
    
    foreach ($romeids as $uuid) {
        $seen = Seen::where('message', $uuid)
            ->where('user', $user)
            ->first();

        $wasseen = $seen->seen;

        if ($seen->seen === 0) {
            $seen->seen = 1;
            $seen->seen_at = Carbon::now();
            $seen->save();
        }
    }

    return response()->json([
        'message' => '获取帖子成功',
        'romes' => $romes,
        'wasseen' => $wasseen
    ], 200);
}

Rome模型

class Rome extends Model
{
    use HasFactory;

    protected $fillable = [
        'sender',
        'room',
        'msgtext',
        'uuid',
        'edited',
        'files',
        'images',
        'seen'
    ];
}

Seen模型

class Seen extends Model
{
    use HasFactory;

    protected $fillable = [
        'message',
        'user',
        'seen',
        'seen_at'
    ];
}

在for-each循环内的var_dump($seen)的输出如下:

object(Illuminate\Database\Eloquent\Collection)#309 (2) {  
    ["items":protected]=> array(0) { }  
    ["escapeWhenCastingToString":protected]=> bool(false) }
object(Illuminate\Database\Eloquent\Collection)#310 (2) {  
    ["items":protected]=> array(0) { }  
    ["escapeWhenCastingToString":protected]=> bool(false) }
object(Illuminate\Database\Eloquent\Collection)#1048 (2) {  
    ["items":protected]=> array(0) { }  
    ["escapeWhenCastingToString":protected]=> bool(false) }
object(Illuminate\Database\Eloquent\Collection)#313 (2) {  
    ["items":protected]=> array(0) { }  
    ["escapeWhenCastingToString":protected]=> bool(false) }
object(Illuminate\Database\Eloquent\Collection)#315 (2) {  
    ["items":protected]=> array(0) { }  
    ["escapeWhenCastingToString":protected]=> bool(false) }
object(Illuminate\Database\Eloquent\Collection)#314 (2) {  
    ["items":protected]=> array(0) { }  
    ["escapeWhenCastingToString":protected]=> bool(false) }
object(Illuminate\Database\Eloquent\Collection)#1274 (2) {  
    ["items":protected]=> array(1) {
        [0]=>
        object(App\Models\Seen)#1275 (30) {
            ["connection":protected]=>
            string(5) "mysql"
            ["table":protected]=>
            string(5) "seens"
            ["primaryKey":protected]=>
            string(2) "id"
            ["keyType":protected]=>
            string(3) "int"
            ["incrementing"]=>
            bool(true)
            ["with":protected]=>
            array(0) {
            }
            ["withCount":protected]=>
            array(0) {
            }
            ["preventsLazyLoading"]=>
            bool(false)
            ["perPage":protected]=>
            int(15)
            ["exists"]=>
            bool(true)
            ["wasRecentlyCreated"]=>
            bool(false)
            ["escapeWhenCastingToString":protected]=>
            bool(false)
            ["attributes":protected]=>
            array(7) {
                ["id"]=>
                int(3)
                ["message"]=>
                string(9) "IcHshOP2V"
                ["user"]=>
                string(1) "3"
                ["seen"]=>
                int(0)
                ["seen_at"]=>
                string(1) "0"
                ["created_at"]=>
                string(19) "2023-03-30 15:18:59"
                ["updated_at"]=>
                string(19) "2023-03-30 15:18:59"
            }
            ["original":protected]=>
            array(7) {
                ["id"]=>
                int(3)
                ["message"]=>
                string(9) "IcHshOP2V"
                ["user"]=>
                string(1) "3"
                ["seen"]=>
                int(0)
                ["seen_at"]=>
                string(1) "0"
                ["created_at"]=>
                string(19) "2023-03-30 15:18:59"
                ["updated_at"]=>
                string(19) "2023-03-30 15:18:59"
            }
            ["changes":protected]=>
            array(0) {
            }
            ["casts":protected]=>
            array(0) {
            }
            ["classCastCache":protected]=>
            array(0) {
            }
            ["attributeCastCache":protected]=>
            array(0) {
            }
            ["dates":protected]=>
            array(0) {
            }
            ["dateFormat":protected]=>
            NULL
            ["appends":protected]=>
            array(0) {
            }
            ["dispatchesEvents":protected]=>
            array(0) {
            }
            ["observables":protected]=>
            array(0) {
            }
            ["relations":protected]=>
            array(0) {
            }
            ["touches":protected]=>
            array(0) {
            }
            ["timestamps"]=
            bool(true)
            ["hidden":protected]=>
            array(0) {
            }
            ["visible":protected]=>
            array(0) {
            }
            ["fillable":protected]=>
            array(4) {
                [0]=>
                string(7) "message"
                [1]=>
                string(4) "user"
                [2]=>
                string(4) "seen"
                [3]=>
                string(7) "seen_at"
            }
            ["guarded":protected]=>
            array(1) {
                [0]=>
                string(1) "*"
            }
        }   }   ["escapeWhenCastingToString":protected]=>   bool(false) } {"message":"获取帖子成功","romes":[{"id":1,"sender":7,"room":"1","msgtext":"agfdasfasdf","uuid":"2tAwioIIu","edited":0,"files":null,"images":null,"seen":null,"participants":"\"ALL\"","created_at

<details>
<summary>英文:</summary>

I am trying to get the property `$seen-&gt;seen` inside of a for-each loop but I am getting:
&gt; &quot;Attempt to read property &quot;seen&quot; on null&quot;

When I do it outside of the for-each loop it works fine

    public function getromes(Request $request)
    {
        $user = Auth::id();
        $romes = Rome::where(&#39;room&#39;, $request-&gt;id)-&gt;get();
        $romeids = $romes-&gt;pluck(&#39;uuid&#39;);

        // $seen = Seen::where(&#39;message&#39;, &#39;IcHshOP2V&#39;)
        //    -&gt;where(&#39;user&#39;, $user)
        //    -&gt;first();
        // $wasseen = $seen-&gt;seen;
        
        foreach ($romeids as $uuid) {
            $seen = Seen::where(&#39;message&#39;, $uuid)
                -&gt;where(&#39;user&#39;, $user)
                -&gt;first();

            $wasseen = $seen-&gt;seen;

            if ($seen-&gt;seen === 0) {
                $seen-&gt;seen = 1;
                $seen-&gt;seen_at = Carbon::now();
                $seen-&gt;save();
            }
        }

        return response()-&gt;json([
            &#39;message&#39; =&gt; &#39;Getting posts was successful&#39;,
            &#39;romes&#39; =&gt; $romes,
            &#39;wasseen&#39; =&gt; $wasseen
        ], 200);
    }

#### Rome model

    class Rome extends Model
    {
        use HasFactory;
    
        protected $fillable = [
            &#39;sender&#39;,
            &#39;room&#39;,
            &#39;msgtext&#39;,
            &#39;uuid&#39;,
            &#39;edited&#39;,
            &#39;files&#39;,
            &#39;images&#39;,
            &#39;seen&#39;
        ];
    }

#### Seen Model

    class Seen extends Model
    {
        use HasFactory;
    
        protected $fillable = [
            &#39;message&#39;,
            &#39;user&#39;,
            &#39;seen&#39;,
            &#39;seen_at&#39;
        ];
    }



    *&gt; var_dump($seen) inside for-each loop looks like this*



    &gt; object(Illuminate\Database\Eloquent\Collection)#309 (2) {  
    &gt; [&quot;items&quot;:protected]=&gt;   array(0) {   }  
    &gt; [&quot;escapeWhenCastingToString&quot;:protected]=&gt;   bool(false) }
    &gt; object(Illuminate\Database\Eloquent\Collection)#310 (2) {  
    &gt; [&quot;items&quot;:protected]=&gt;   array(0) {   }  
    &gt; [&quot;escapeWhenCastingToString&quot;:protected]=&gt;   bool(false) }
    &gt; object(Illuminate\Database\Eloquent\Collection)#1048 (2) {  
    &gt; [&quot;items&quot;:protected]=&gt;   array(0) {   }  
    &gt; [&quot;escapeWhenCastingToString&quot;:protected]=&gt;   bool(false) }
    &gt; object(Illuminate\Database\Eloquent\Collection)#313 (2) {  
    &gt; [&quot;items&quot;:protected]=&gt;   array(0) {   }  
    &gt; [&quot;escapeWhenCastingToString&quot;:protected]=&gt;   bool(false) }
    &gt; object(Illuminate\Database\Eloquent\Collection)#315 (2) {  
    &gt; [&quot;items&quot;:protected]=&gt;   array(0) {   }  
    &gt; [&quot;escapeWhenCastingToString&quot;:protected]=&gt;   bool(false) }
    &gt; object(Illuminate\Database\Eloquent\Collection)#314 (2) {  
    &gt; [&quot;items&quot;:protected]=&gt;   array(0) {   }  
    &gt; [&quot;escapeWhenCastingToString&quot;:protected]=&gt;   bool(false) }
    &gt; object(Illuminate\Database\Eloquent\Collection)#1274 (2) {  
    &gt; [&quot;items&quot;:protected]=&gt;   array(1) {
    &gt;     [0]=&gt;
    &gt;     object(App\Models\Seen)#1275 (30) {
    &gt;       [&quot;connection&quot;:protected]=&gt;
    &gt;       string(5) &quot;mysql&quot;
    &gt;       [&quot;table&quot;:protected]=&gt;
    &gt;       string(5) &quot;seens&quot;
    &gt;       [&quot;primaryKey&quot;:protected]=&gt;
    &gt;       string(2) &quot;id&quot;
    &gt;       [&quot;keyType&quot;:protected]=&gt;
    &gt;       string(3) &quot;int&quot;
    &gt;       [&quot;incrementing&quot;]=&gt;
    &gt;       bool(true)
    &gt;       [&quot;with&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;withCount&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;preventsLazyLoading&quot;]=&gt;
    &gt;       bool(false)
    &gt;       [&quot;perPage&quot;:protected]=&gt;
    &gt;       int(15)
    &gt;       [&quot;exists&quot;]=&gt;
    &gt;       bool(true)
    &gt;       [&quot;wasRecentlyCreated&quot;]=&gt;
    &gt;       bool(false)
    &gt;       [&quot;escapeWhenCastingToString&quot;:protected]=&gt;
    &gt;       bool(false)
    &gt;       [&quot;attributes&quot;:protected]=&gt;
    &gt;       array(7) {
    &gt;         [&quot;id&quot;]=&gt;
    &gt;         int(3)
    &gt;         [&quot;message&quot;]=&gt;
    &gt;         string(9) &quot;IcHshOP2V&quot;
    &gt;         [&quot;user&quot;]=&gt;
    &gt;         string(1) &quot;3&quot;
    &gt;         [&quot;seen&quot;]=&gt;
    &gt;         int(0)
    &gt;         [&quot;seen_at&quot;]=&gt;
    &gt;         string(1) &quot;0&quot;
    &gt;         [&quot;created_at&quot;]=&gt;
    &gt;         string(19) &quot;2023-03-30 15:18:59&quot;
    &gt;         [&quot;updated_at&quot;]=&gt;
    &gt;         string(19) &quot;2023-03-30 15:18:59&quot;
    &gt;       }
    &gt;       [&quot;original&quot;:protected]=&gt;
    &gt;       array(7) {
    &gt;         [&quot;id&quot;]=&gt;
    &gt;         int(3)
    &gt;         [&quot;message&quot;]=&gt;
    &gt;         string(9) &quot;IcHshOP2V&quot;
    &gt;         [&quot;user&quot;]=&gt;
    &gt;         string(1) &quot;3&quot;
    &gt;         [&quot;seen&quot;]=&gt;
    &gt;         int(0)
    &gt;         [&quot;seen_at&quot;]=&gt;
    &gt;         string(1) &quot;0&quot;
    &gt;         [&quot;created_at&quot;]=&gt;
    &gt;         string(19) &quot;2023-03-30 15:18:59&quot;
    &gt;         [&quot;updated_at&quot;]=&gt;
    &gt;         string(19) &quot;2023-03-30 15:18:59&quot;
    &gt;       }
    &gt;       [&quot;changes&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;casts&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;classCastCache&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;attributeCastCache&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;dates&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;dateFormat&quot;:protected]=&gt;
    &gt;       NULL
    &gt;       [&quot;appends&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;dispatchesEvents&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;observables&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;relations&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;touches&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;timestamps&quot;]=&gt;
    &gt;       bool(true)
    &gt;       [&quot;hidden&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;visible&quot;:protected]=&gt;
    &gt;       array(0) {
    &gt;       }
    &gt;       [&quot;fillable&quot;:protected]=&gt;
    &gt;       array(4) {
    &gt;         [0]=&gt;
    &gt;         string(7) &quot;message&quot;
    &gt;         [1]=&gt;
    &gt;         string(4) &quot;user&quot;
    &gt;         [2]=&gt;
    &gt;         string(4) &quot;seen&quot;
    &gt;         [3]=&gt;
    &gt;         string(7) &quot;seen_at&quot;
    &gt;       }
    &gt;       [&quot;guarded&quot;:protected]=&gt;
    &gt;       array(1) {
    &gt;         [0]=&gt;
    &gt;         string(1) &quot;*&quot;
    &gt;       }
    &gt;     }   }   [&quot;escapeWhenCastingToString&quot;:protected]=&gt;   bool(false) } {&quot;message&quot;:&quot;Getting posts  was
    &gt; successful&quot;,&quot;romes&quot;:[{&quot;id&quot;:1,&quot;sender&quot;:7,&quot;room&quot;:&quot;1&quot;,&quot;msgtext&quot;:&quot;agfdasfasdf&quot;,&quot;uuid&quot;:&quot;2tAwioIIu&quot;,&quot;edited&quot;:0,&quot;files&quot;:null,&quot;images&quot;:null,&quot;seen&quot;:null,&quot;participants&quot;:&quot;\&quot;ALL\&quot;&quot;,&quot;created_at&quot;:&quot;2023-03-30T15:00:40.000000Z&quot;,&quot;updated_at&quot;:&quot;2023-03-30T15:00:40.000000Z&quot;},{&quot;id&quot;:2,&quot;sender&quot;:3,&quot;room&quot;:&quot;1&quot;,&quot;msgtext&quot;:&quot;sdfgfdsg&quot;,&quot;uuid&quot;:&quot;ic91IRrqX&quot;,&quot;edited&quot;:0,&quot;files&quot;:null,&quot;images&quot;:null,&quot;seen&quot;:null,&quot;participants&quot;:&quot;\&quot;ALL\&quot;&quot;,&quot;created_at&quot;:&quot;2023-03-30T15:00:43.000000Z&quot;,&quot;updated_at&quot;:&quot;2023-03-30T15:00:43.000000Z&quot;},{&quot;id&quot;:3,&quot;sender&quot;:5,&quot;room&quot;:&quot;1&quot;,&quot;msgtext&quot;:&quot;zxcvxcv&quot;,&quot;uuid&quot;:&quot;x5qXAtRot&quot;,&quot;edited&quot;:0,&quot;files&quot;:null,&quot;images&quot;:null,&quot;seen&quot;:null,&quot;participants&quot;:&quot;\&quot;ALL\&quot;&quot;,&quot;created_at&quot;:&quot;2023-03-30T15:01:56.000000Z&quot;,&quot;updated_at&quot;:&quot;2023-03-30T15:01:56.000000Z&quot;},{&quot;id&quot;:4,&quot;sender&quot;:3,&quot;room&quot;:&quot;1&quot;,&quot;msgtext&quot;:&quot;fsadfasdfasdf&quot;,&quot;uuid&quot;:&quot;MJD6jjfBU&quot;,&quot;edited&quot;:0,&quot;files&quot;:null,&quot;images&quot;:null,&quot;seen&quot;:null,&quot;participants&quot;:&quot;\&quot;ALL\&quot;&quot;,&quot;created_at&quot;:&quot;2023-03-30T15:13:34.000000Z&quot;,&quot;updated_at&quot;:&quot;2023-03-30T15:13:34.000000Z&quot;},{&quot;id&quot;:5,&quot;sender&quot;:3,&quot;room&quot;:&quot;1&quot;,&quot;msgtext&quot;:&quot;fsadfasdfasdf&quot;,&quot;uuid&quot;:&quot;fJc4lEHji&quot;,&quot;edited&quot;:0,&quot;files&quot;:null,&quot;images&quot;:null,&quot;seen&quot;:null,&quot;participants&quot;:&quot;\&quot;ALL\&quot;&quot;,&quot;created_at&quot;:&quot;2023-03-30T15:14:12.000000Z&quot;,&quot;updated_at&quot;:&quot;2023-03-30T15:14:12.000000Z&quot;},{&quot;id&quot;:6,&quot;sender&quot;:3,&quot;room&quot;:&quot;1&quot;,&quot;msgtext&quot;:&quot;dxzvfvcxvxcv&quot;,&quot;uuid&quot;:&quot;VVEQHNSm5&quot;,&quot;edited&quot;:0,&quot;files&quot;:null,&quot;images&quot;:null,&quot;seen&quot;:null,&quot;participants&quot;:&quot;\&quot;ALL\&quot;&quot;,&quot;created_at&quot;:&quot;2023-03-30T15:14:33.000000Z&quot;,&quot;updated_at&quot;:&quot;2023-03-30T15:14:33.000000Z&quot;},{&quot;id&quot;:7,&quot;sender&quot;:3,&quot;room&quot;:&quot;1&quot;,&quot;msgtext&quot;:&quot;vcmbnxcvn&quot;,&quot;uuid&quot;:&quot;IcHshOP2V&quot;,&quot;edited&quot;:0,&quot;files&quot;:null,&quot;images&quot;:null,&quot;seen&quot;:null,&quot;participants&quot;:&quot;\&quot;ALL\&quot;&quot;,&quot;created_at&quot;:&quot;2023-03-30T15:18:59.000000Z&quot;,&quot;updated_at&quot;:&quot;2023-03-30T15:18:59.000000Z&quot;}]}



</details>


# 答案1
**得分**: 1

以下是代码的翻译部分:

```php
public function getromes(Request $request)
{
    $user = Auth::id();
    $romes = Rome::where('room', $request->id)->get();
    foreach($romes as $rome){
        $seen = Seen::where('message', $rome->romeid)->where('user', $user)->first();
        if($seen && !$seen->seen){
            $seen->seen = 1;
            $seen->seen_at = Carbon::now();
            $seen->save();
        }
    }
    return response()->json([
        'message' => 'Getting posts was successful',
        'romes' => $romes,
    ], 200);
}

请注意,我只翻译了代码部分,不包括注释或其他文本。

英文:

Thanks, Tim Lewis

This is the working code!

public function getromes(Request $request)
{
$user = Auth::id();
$romes = Rome::where(&#39;room&#39;, $request-&gt;id)-&gt;get();
foreach($romes as $rome){
$seen = Seen::where(&#39;message&#39;, $rome-&gt;romeid)-&gt;where(&#39;user&#39;, $user)-&gt;first();
if($seen &amp;&amp; !$seen-&gt;seen){
$seen-&gt;seen = 1;
$seen-&gt;seen_at = Carbon::now();
$seen-&gt;save();
}
}
return response()-&gt;json([
&#39;message&#39; =&gt; &#39;Getting posts was successful&#39;,
&#39;romes&#39; =&gt; $romes,
], 200);
}

huangapple
  • 本文由 发表于 2023年3月31日 21:40:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定