Last active
May 28, 2018 16:53
-
-
Save heryvandoro/aa3616889503dc9a657368931a300a32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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