سلام دوستان وقت بخیر ،
سئوال من اینه که چطور کد های تابع بازگشتی در لاراول رو به php خام تبدیل کنم؟! کد های لاراول رو میزارم :
ابتدا دسته بندی های مادر رو سلکت کردیم :
public function index()
{
$categories = Category::where('parent' , 0)->latest()->paginate(10);
return view('panel.category.index' , compact("categories"));
}
و در صفحه مربوطه دیتا رو دریافت کردیم و
به همراه include مقادیر رو پاس میدیم
@include('panel.category.categories_group' , ['categories' => $categories])
و در این صفحه تمام دسته بندی ها و زیر دسته ها نمایش داده میشه
<ul class="list-group list-group-flush">
@foreach($categories as $cate)
<li class="list-group-item">
<div class="d-flex">
<span>{{ $cate->name }}</span>
<div class="actions mr-2">
<form action="{{ route('admin.categories.destroy', $cate->id) }}" id="cate-{{ $cate->id }}-delete" method="POST">
@csrf
@method('delete')
</form>
<a href="#" onclick="event.preventDefault(); document.getElementById('cate-{{ $cate->id }}-delete').submit()" class="badge badge-danger">حذف</a>
<a href="{{ route('admin.categories.edit' , $cate->id) }}" class="badge badge-primary">ویرایش</a>
<a href="{{ route('admin.categories.create') }}?parent={{ $cate->id }}" class="badge badge-warning">ثبت زیر دسته</a>
</div>
</div>
@if($cate->child->count())
@include('panel.category.categories_group' , [ 'categories' => $cate->child])
@endif
</li>
@endforeach
</ul>
سلام وقتتون بخیر!
<?php
class test
{
public $menus = '[{"slug":"users.list","name":"مدیریت کاربران","url":"users","parent":"users",
"icon":null,"permission":"admin.users","priority":0},{"slug":"shop","name":"فروشگاه","url":"","parent":"","icon":"icon-bag","permission":"shop","priority":0},{"slug":"shop.products","name":"محصولات","url":"shop/product","parent":"shop","icon":null,"permission":"products","priority":0},{"slug":"shop.category","name":"دسته بندی ها","url":"shop/category","parent":"shop","icon":null,"permission":"admin.categories","priority":0},{"slug":"shop.coupons","name":"کوپن ها","url":"shop/coupon","parent":"shop","icon":null,"permission":"admin.coupons","priority":0},{"slug":"orders","name":"سفارشات","url":"","parent":"","icon":"icon-social-dropbox","permission":"","priority":0},{"slug":"shop.orders","name":"پیگیری سفارشات","url":"shop/order","parent":"orders","icon":null,"permission":null,"priority":0},{"slug":"payments.list","name":"کیف پول","url":"shop/payment","parent":"","icon":"icon-wallet","permission":null,"priority":0},{"slug":"affiliate","name":"مشتریان","url":"campaigns/affiliate","parent":"","icon":"icon-people","permission":"marketer","priority":0},{"slug":"automation","name":"اتوماسیون","url":"","parent":"","icon":"fa fa-sliders","permission":"admin.automations","priority":0},{"slug":"automation.index","name":"لیست اتوماسیون ها","url":"http://localhost/automation","parent":"automation","icon":null,"permission":null,"priority":0},{"slug":"service","name":"سرویس ها","url":"http://localhost/automation/service","parent":"automation","icon":null,"permission":"admin.services","priority":0},{"slug":"package","name":"پکیج ها","url":"http://localhost/automation/package","parent":"automation","icon":null,"permission":"admin.packages","priority":0},{"slug":"automation.report","name":"گزارشات","url":"automation/report","parent":"automation","icon":null,"permission":"admin.automation.report","priority":0},{"slug":"automation.sellreport","name":"گزارشات فروش","url":"automation/sellreport","parent":"automation","icon":null,"permission":"admin.automation.report","priority":0},{"slug":"comment.category","name":"دسته بندی","url":"http://localhost/comment/category","parent":"comments","icon":"icon-support","permission":"admin.comments","priority":0},{"slug":"comment.comment","name":"کامنت","url":"http://localhost/comment","parent":"comments","icon":"icon-support","permission":"admin.comments","priority":"1"},{"slug":"comments","name":"کامنت ها","url":"","parent":"","icon":"icon-support","permission":"admin.comments","priority":"3"},{"slug":"users","name":"کاربران","url":"","parent":"","icon":"icon-people","permission":"admin.users","priority":8},{"slug":"profile.profile","name":"پروفایل","url":"user/profile","parent":"","icon":"icon-people","permission":null,"priority":8},{"slug":"Rp","name":"Rps","url":"#","parent":"","icon":"icon-support","permission":"","priority":10},{"slug":"Rp1","name":"Rps","url":"#","parent":"Rp","icon":"","permission":"","priority":10},{"slug":"Rp2","name":"Rps","url":"#","parent":"Rp1","icon":"","permission":"","priority":10}]';
public function __construct()
{
$this->menus = json_decode($this->menus, true);
$this->recursive($this->parents());
}
public function parents()
{
array_map(function ($item) use (&$parents) {
if ($item['parent'] == "")
$parents[] = $item;
}, $this->menus);
return $parents;
}
function recursive($menu)
{
foreach ($menu as $item) {
$children = $this->hasChild($item['slug']);
if (is_array($children)) {
echo "<h1>{$item['name']}</h1>";
$this->recursive($children);
continue;
}
echo $item['name'] . "<br>";
}
}
function hasChild($parent)
{
array_map(function ($item) use ($parent, &$children) {
if ($item['parent'] == $parent)
$children[] = $item;
}, $this->menus);
return $children;
}
}
$a = new test;
بجای منو اطلاعات دیتابیستو بذار
@Rp76
جناب پارسیان ممنونم ازتون.
در رابطه با کد زیر اگر ممکنه راهنمایی کنید...
این کد داخل یک لوپ بی نهایت قرار میگیره و فقط دسته بندی اول رو نمایش میده (شاید چون شی دوباره ساخته میشه!)
کد به این صورت هست :
کلاس Category
public function get(int $id = null)
{
if(! isset($id)){
$stmt = $this->pdo_panel->prepare("SELECT * FROM {$this->table} where parent IS NULL ");
if ($stmt->execute()) {
if ($stmt->rowCount() >= 1) {
return $category = $stmt->fetchAll(PDO::FETCH_OBJ);
}
}
}else{
$stmt = $this->pdo_panel->prepare("SELECT * FROM {$this->table} where parent = :id ");
$stmt->bindParam(":id", $id);
if ($stmt->execute()) {
if ($stmt->rowCount() >= 1) {
return $category = $stmt->fetchAll(PDO::FETCH_OBJ);
}
}
}
}
public function count($id)
{
$stmt = $this->pdo_panel->prepare("SELECT * FROM {$this->table} where parent = :id ");
$stmt->bindParam(":id", $id);
if ($stmt->execute()) {
if ($stmt->rowCount() >= 1) {
return $stmt->rowCount();
}
}
}
و کد های تابع بازگشتی
<?php
foreach ($category->get() as $item) : ?>
<tr>
<?php var_dump($item->id); ?>
<td class="wrap_table"></td>
<td class="wrapper_table_product"><?= $item->id ?></td>
<td class="wrapper_table_product"><?= $item->name ?>
</td>
<td class="wrapper_table_product"><a href="edit-product.php?edit-product=<?= $item->name ?>"><i
class="material-icons">edit</i></a></td>
<td class="wrapper_table_product "><a class="delete delete-product" data-id="<?= $item->name ?>"><i
class="material-icons">delete</i></a>
</td>
</tr>
<?php
if ($category->count($item->id)) {
$category->get($item->id);
include "category-child.php";
}
?>
<?php endforeach; ?>
سلام دوباره فکر کنم مشکل از اخرش باشه!
if ($category->count($item->id)>0) {
$category->get($item->id);
include "category-child.php";
}
اولین تابع رو هم میتونی اینطوری بهینه کنی
public function get(int $id = null)
{
$stmt = $this->pdo_panel->prepare("SELECT * FROM {$this->table} where parent = :id ");
$stmt->bindParam(":id", $id);
if ($stmt->execute()) {
if ($stmt->rowCount() >= 1) {
return $category = $stmt->fetchAll(PDO::FETCH_OBJ);
}
}
}
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟