$post = new Post();
$post->create($request->all());
foreach($request->get('categories') as $categoryId){
$category = Categories::find($categoryId);
$post->categories()->save($category);
}
class User extends Authenticatable
{
public function comments()
{
return $this->hasMany(Comment::class);
}
}
class Comment extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
class CommentController extends BaseController
{
public function index()
{
return Comment::with('user')->get();
}
}