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"}]}