select news.* from news left join
news_trademark on news_trademark.news_id = news.id
and news_trademark.trademark_id = 747
left join trademark on trademark.id = news_trademark.trademark_id
and trademark.photo is null
where trademark.id is null
public function getTrademarks()
{
return $this->hasMany(Trademark::class, ['id' => 'trademark_id'])->viaTable('news_trademark', ['news_id' => 'id']);
}