lundi 17 août 2020

how to get second relation reccord for ajax request in laravel?

I have Model Ticket in which user explain issue.

Ticket.php

 protected $table = 'tickets';
    /**
     * @var array
     */
    protected $guarded = [];

    /**
     * @var array
     */
    protected $hidden = [
        'created_at', 'updated_at'
    ];


    public function ticket_replies()
    {
        return $this->hasMany(TicketReply::class, 'ticket_id');
    }

    public function ticket_assigned_agents()
    {
        return $this->hasMany(TicketAssignedAgent::class, 'ticket_id');
    }

There is another model TicketReply TicketReply.php

class TicketReply extends Model
{
    protected $table = 'ticket_replies';
    /**
     * @var array
     */
    protected $guarded = [];

    /**
     * @var array
     */
    protected $hidden = [
        'created_at', 'updated_at'
    ];

    public function staffs(){
        return $this->belongsTo(Staff::class,'user_id');
    }
    public function ticket()
    {
        return $this->belongsTo(Ticket::class, 'ticket_id');
    }
}

Now i want to get staff name from ticket reply

Query

public  function getReplies($ticket_id)
    {
        $ticket =  Ticket::where('id',$ticket_id)->with('ticket_replies')->first();

       return response()->json($ticket);
    }

Now what i want is . I want to get staff name from ticketreply model in ajax success.

$.each(ticket.ticket_replies, function(index, reply) {

console.log(reply.staffs.name);
}

But it us not working what i can do about it.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire