英文:
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->seen` inside of a for-each loop but I am getting:
> "Attempt to read property "seen" on null"
When I do it outside of the for-each loop it works fine
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' => 'Getting posts was successful',
'romes' => $romes,
'wasseen' => $wasseen
], 200);
}
#### Rome model
class Rome extends Model
{
use HasFactory;
protected $fillable = [
'sender',
'room',
'msgtext',
'uuid',
'edited',
'files',
'images',
'seen'
];
}
#### Seen Model
class Seen extends Model
{
use HasFactory;
protected $fillable = [
'message',
'user',
'seen',
'seen_at'
];
}
*> var_dump($seen) inside for-each loop looks like this*
> 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":"Getting posts was
> successful","romes":[{"id":1,"sender":7,"room":"1","msgtext":"agfdasfasdf","uuid":"2tAwioIIu","edited":0,"files":null,"images":null,"seen":null,"participants":"\"ALL\"","created_at":"2023-03-30T15:00:40.000000Z","updated_at":"2023-03-30T15:00:40.000000Z"},{"id":2,"sender":3,"room":"1","msgtext":"sdfgfdsg","uuid":"ic91IRrqX","edited":0,"files":null,"images":null,"seen":null,"participants":"\"ALL\"","created_at":"2023-03-30T15:00:43.000000Z","updated_at":"2023-03-30T15:00:43.000000Z"},{"id":3,"sender":5,"room":"1","msgtext":"zxcvxcv","uuid":"x5qXAtRot","edited":0,"files":null,"images":null,"seen":null,"participants":"\"ALL\"","created_at":"2023-03-30T15:01:56.000000Z","updated_at":"2023-03-30T15:01:56.000000Z"},{"id":4,"sender":3,"room":"1","msgtext":"fsadfasdfasdf","uuid":"MJD6jjfBU","edited":0,"files":null,"images":null,"seen":null,"participants":"\"ALL\"","created_at":"2023-03-30T15:13:34.000000Z","updated_at":"2023-03-30T15:13:34.000000Z"},{"id":5,"sender":3,"room":"1","msgtext":"fsadfasdfasdf","uuid":"fJc4lEHji","edited":0,"files":null,"images":null,"seen":null,"participants":"\"ALL\"","created_at":"2023-03-30T15:14:12.000000Z","updated_at":"2023-03-30T15:14:12.000000Z"},{"id":6,"sender":3,"room":"1","msgtext":"dxzvfvcxvxcv","uuid":"VVEQHNSm5","edited":0,"files":null,"images":null,"seen":null,"participants":"\"ALL\"","created_at":"2023-03-30T15:14:33.000000Z","updated_at":"2023-03-30T15:14:33.000000Z"},{"id":7,"sender":3,"room":"1","msgtext":"vcmbnxcvn","uuid":"IcHshOP2V","edited":0,"files":null,"images":null,"seen":null,"participants":"\"ALL\"","created_at":"2023-03-30T15:18:59.000000Z","updated_at":"2023-03-30T15:18:59.000000Z"}]}
</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('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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论