I'm fairly new to Laravel and query builder I'm trying to do something I thought would be pretty easy, but I must be missing something really obvious.
To strip it back to it's simplest
I have 3 models
- PaperFormat, morphs to other models (e.g. Topic), and has many papers
- Paper, belongs to PaperFormat
- Topic , morphOne to PaperFormat and has a method of papers(), intended to retrieve all papers from that topic using query builder.
For the purposes of this example I'm trying to exclude where papers.id = 2 or 3 using whereNotIn()
I have 20 papers in the table (all with a PaperFormat ID of 1 that morphs to Topic which also has an ID of one). There's currently only 1 Topic and 1 Paperformat in their respective tables.
And the query returns 18 results, as I expected as I'm excluding 2 using whereNotIn().
However all 18 results in the array are Paper ID 1 !!!!!
Same if I change it whereIn(1, 2 ,3, 4)
I get 4 results in the array are Paper ID 1 !!!!! again!
I'm guessing I've got something wrong that's causing it to mess up.
(first post here please be kind!)
Please help!
class PaperFormat extends Model
{
public function paperFormatable()
{
return $this->morphTo();
}
public function papers()
{
return $this->hasMany(Paper::class);
}
}
class Paper extends Model
{
public function paperFormat()
{
return $this->belongsTo(PaperFormat::class);
}
}
class Topic extends Model
{
public function paperFormat()
{
return $this->morphOne(PaperFormat::class, 'paper_formatable');
}
public function papers()
{
$papers = Paper::
whereNotIn('papers.id', [2, 3])
->join('paper_formats', 'papers.paper_format_id', '=', 'paper_formats.id')
->where('paper_formatable_id', $this->id)
->where('paper_formatable_type', 'App\Models\Topic')
->get();
return $papers;
}
}