Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Barley's avatar

Segmentation fault on Eloquent query

Hi, I get a segmentation fault ("Segmentation fault: 11") when I run my Eloquent query. I don't know the cause and I'm wondering why I get this error?

This is my query that triggers the error:

$category_ids = [19,20,21];

$messages = Message::whereHas('categories', function($query) use ($category_ids) { $query->whereIn('categories.id', $category_ids); })->get();

The idea is that I want to get all messages that have at least 1 of the categories in the $category_ids array.

The Message model has a many-to-many relationship with the Category model. So the Message model contains:

public function categories() { return $this->belongsToMany('App\Category'); }

Whereas the Category model contains:

public function messages() { return $this->belongsToMany('App\Message'); }

There is a migration file for creating the pivot table.

What I find strange is that depending on the content of the $category_ids array the query succeeds or triggers a segmentation fault:

$category_ids = [19] // expected result, no seg-fault $category_ids = [19,20] // expected result, no seg-fault $category_ids = [19,20,21] // seg fault $category_ids = [19,20,21,22] // expected result, no seg-fault

I'm wondering what I could be doing wrong?

0 likes
3 replies
Barley's avatar

A friend of mine ran my project on his machine (Windows) and it runs without seg fault errors. On my machine (MacOS) the same code produces seg faults...

Sinnbeck's avatar

A little googling seems to suggest the error lies in the mysql installation. Have you tried reinstalling it?

Barley's avatar

Hi, thanks for your reply. I upgraded my php from 7.2.15 to 7.3.8 and the issue is solved!

Please or to participate in this conversation.