Skip to content

Instantly share code, notes, and snippets.

@stas-dovgodko
Last active June 9, 2024 21:10
Show Gist options
  • Save stas-dovgodko/a251cbe9b37b4c981ec679820ef983c0 to your computer and use it in GitHub Desktop.
Save stas-dovgodko/a251cbe9b37b4c981ec679820ef983c0 to your computer and use it in GitHub Desktop.
Laravel test1
<?php
class Post extends Model
{
public function comments()
{
return $this->hasOne(Comment::class);
}
}
class Comment extends Model
{
public function post()
{
return $this->belongsTo(Post::class);
}
}
$posts = Post::where(['type' => 1])->get();
$comments = [];
foreach ($posts as $post) {
foreach ($post->comments as $comment) {
comments[] = $comment;
}
}
$post_title = Post::find(42)->title;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment