'I am not getting the properly i get "0" instead even though it is stored properly in the database,

here is the controller i am creating new room,i dd($attr)and i am having the proper id there

 public function create(Request $request)
    {
        $attr = Validator($request->all(), [
            'topic' => ['string'],
            'size'  => ['required','integer'],
            'language_id'   => ['required', 'exists:languages,id']
        ]);

        if($attr->fails()){
            return response($attr->errors(), Response::HTTP_BAD_REQUEST);
        }
        
        $attr = $attr->validated();

        $attr = Arr::add($attr, 'id', uniqid());
        $attr = Arr::add($attr, 'owner_id', auth()->id());

        $data =  Room::create($attr)->toArray();
        // fire room created event
        broadcast(new NewRoomCreatedEvent($data));

        return response($data, Response::HTTP_CREATED);
    }

I am getting this error in postman enter image description here and also here is the event

<?php

namespace App\Events;

use App\Models\Room;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class NewRoomCreatedEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $room;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($room)
    {
        $this->room = $room;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('practice4free');
    }
}

I do not know how to fix it, and also why I am not getting the proper id that is stored in the database instead I am getting "0".



Solution 1:[1]

I solved it by using the refresh eloquent function like that

 public function create(Request $request)
    {
        $attr = Validator($request->all(), [
            'topic' => ['string'],
            'size'  => ['required','integer'],
            'language_id'   => ['required', 'exists:languages,id']
        ]);

        if($attr->fails()){
            return response($attr->errors(), Response::HTTP_BAD_REQUEST);
        }
        
        $attr = $attr->validated();

        $attr = Arr::add($attr, 'id', uniqid());
        $attr = Arr::add($attr, 'owner_id', auth()->id());

        $data =  Room::create($attr);

        $data = $data->refresh();
        $data = $data->load('language');
        // fire room created event
        broadcast(new NewRoomCreatedEvent($data));

        return response($data, Response::HTTP_CREATED);
    }

but is that alright, coz it is weird that I am not getting the id even though it is stored in the database.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mahmoud Ashraf