Answer the question
In order to leave comments, you need to log in
How to return an array of IDs in Laravel?
Tell me please. How can I return an array of IDs in the model? I have a Ticket model. It has a link to messages:
public function messages()
{
return $this->hasMany('App\Message');
}
Answer the question
In order to leave comments, you need to log in
$message->tikets->lists('id')->toArray()
It was like that, as far as I remember.
Try like this:
public function messagesIds()
{
$this->messages->pluck('id')->toArray();
}
It is necessary to specify the with("messages") method when calling the model.
But the array count:
In the model itself, you can specify:
protected $casts = [
'options' => 'array',
];
return $this->hasMany('App\Message')->select(['id', 'message_id']);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question