Skip to content

Instantly share code, notes, and snippets.

@heryvandoro
Last active May 28, 2018 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heryvandoro/aa3616889503dc9a657368931a300a32 to your computer and use it in GitHub Desktop.
Save heryvandoro/aa3616889503dc9a657368931a300a32 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Category;
class CategoryController extends Controller
{
public function index()
{
return Category::where("parent_id", null)->get();
}
public function show($id)
{
$category = Category::with("parent")->find($id);
$parents = [];
$current = $category->parent;
while($current!=null){
$parent = Category::find($current->id);
array_unshift($parents, $parent);
$current = $current->parent;
}
$category["parent"] = $parents;
return $category;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment