I'm using Laravel as an API, but have some troubles with getting posts with the specific updated value.
Here is my code. Any help to fix the code regarding both syntax and inefficiencies?
use Controller;
use Post;
use User;
use Comment;
class HomeController extends Controller{
public function index(){
$posts = $this->getPosts('home');
return view('template', ['posts' = $posts]);
}
public static function getPosts($slug){
$posts = Post::where('slug', '=', $slug)->get();
foreach($posts as &$post){
$post->user = User::find($post->user_id);
$post->comments = Comment::where('post_id', '=', $post->post_id)->get();
foreach($post->comments as &$comment){
$comment->user = User::find($comment->user_id);
}
}
return $posts;
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire