I have a Posts
object, that may or may not have a Link
object (Link belongsTo Post and Post hasOne Link).
I'm trying to find Posts that were created after a certain date that don't have a Link created yet.
I have the following but it doesn't work:
$posts = Post::where('post_date', '>', 'xxxx')->whereIsNull('link.id'); // which is how I would write it with an OUTER JOIN.
What am I missing here?
Use Laravel eager loading and loop through the results to find posts without links.
$posts = Post::with('link') -> get();
// loop with foreach and extract posts with no links
$not_linked_posts = array();
foreach ($posts as $k => $post){
// if link is not set
if (!isset($post -> link)){
// add it to array
$not_linked_posts[$k] = $post -> toArray(); //convert post to array
}
}
Hope this help.