Commit a97be435 by Aan Choesni Herlingga

optmized query backend menu

parent 72ab1c1a
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
namespace App\Helpers; namespace App\Helpers;
use App\Models\Webprofile\Menu;
class InseoHelper class InseoHelper
{ {
public static function tglbulanindo2($waktu, $tipe = '') public static function tglbulanindo2($waktu, $tipe = '')
...@@ -18,13 +16,13 @@ class InseoHelper ...@@ -18,13 +16,13 @@ class InseoHelper
switch ($tipe) { switch ($tipe) {
case 1: case 1:
$full = $tgl . ' ' . $bulan[(int) $bln - 1] . ' ' . $thn; $full = $tgl.' '.$bulan[(int) $bln - 1].' '.$thn;
break; break;
case 2: case 2:
$full = $tgl . '/' . $bln . '/' . $thn; $full = $tgl.'/'.$bln.'/'.$thn;
break; break;
default: default:
$full = "$tgl " . $bulan[(int) $bln - 1] . " $thn"; $full = "$tgl ".$bulan[(int) $bln - 1]." $thn";
} }
return $full; return $full;
...@@ -36,22 +34,8 @@ class InseoHelper ...@@ -36,22 +34,8 @@ class InseoHelper
$bln = substr($waktu, 5, 2); $bln = substr($waktu, 5, 2);
$thn = substr($waktu, 0, 4); $thn = substr($waktu, 0, 4);
$full = $tgl . ' - ' . $bln . ' - ' . $thn; $full = $tgl.' - '.$bln.' - '.$thn;
return $full; return $full;
} }
public static function maxmenu($lvl, $parentid)
{
$hnewmenu = Menu::where('parent', $parentid)->where('level', $lvl)->max('urutan');
return $hnewmenu;
}
public static function jumchild($lvl, $parentid)
{
$hnewmenu = Menu::where('parent', $parentid)->where('level', $lvl)->count();
return $hnewmenu;
}
} }
...@@ -41,24 +41,9 @@ class MenuController extends Controller ...@@ -41,24 +41,9 @@ class MenuController extends Controller
public function build_menu() public function build_menu()
{ {
$data = Menu::select('id', 'parent', 'name', 'url', 'level', 'urutan')->where('level', '1')->orderby('urutan', 'asc')->get(); $data = Menu::select('id', 'parent', 'name', 'url', 'level', 'urutan')->where('level', '1')->orderby('urutan', 'asc')->with('rEn', 'rChild')->get()->toArray();
$menu = [];
$i = 0; return $data;
foreach ($data as $item) {
$menu[$i]['id'] = $item->id;
$menu[$i]['parent'] = $item->parent;
$menu[$i]['name'] = $item->name;
$menu[$i]['url'] = $item->url;
$menu[$i]['level'] = $item->level;
$menu[$i]['urutan'] = $item->urutan;
if ($this->menu_has_child($item->parent)) {
$menu[$i]['child'] = $this->menu_get_child($item->id);
}
++$i;
}
return $menu;
} }
public function menu_has_child($parentid) public function menu_has_child($parentid)
...@@ -202,10 +187,14 @@ class MenuController extends Controller ...@@ -202,10 +187,14 @@ class MenuController extends Controller
} }
} }
private function createEn($data, $menu) private function createEn($data, $menu, $translate = 1)
{ {
$trans = new GoogleTranslate(); if ($translate == 1) {
$name = $trans->translate($this->SOURCE, $this->TARGET, $data['name']); $trans = new GoogleTranslate();
$name = $trans->translate($this->SOURCE, $this->TARGET, $data['name']);
} else {
$name = $data['name'];
}
$dataEn['menu_id'] = $menu->id; $dataEn['menu_id'] = $menu->id;
$dataEn['name'] = $name; $dataEn['name'] = $name;
...@@ -236,9 +225,9 @@ class MenuController extends Controller ...@@ -236,9 +225,9 @@ class MenuController extends Controller
$urutan = Menu::where('level', $level)->where('parent', $request->input('parentpage'))->max('urutan'); $urutan = Menu::where('level', $level)->where('parent', $request->input('parentpage'))->max('urutan');
$page = Pages::where('id', $request->input('page'))->first(); $page = Pages::with(['rEn'])->where('id', $request->input('page'))->first();
$data['name'] = $page->title; $data['name'] = $page->rEn->title;
$data['level'] = $level; $data['level'] = $level;
$data['parentlevel'] = $parentlevel; $data['parentlevel'] = $parentlevel;
$data['urutan'] = $urutan + 1; $data['urutan'] = $urutan + 1;
...@@ -250,7 +239,7 @@ class MenuController extends Controller ...@@ -250,7 +239,7 @@ class MenuController extends Controller
$save = Menu::create($data); $save = Menu::create($data);
$this->createEn($data, $save); $this->createEn($data, $save, 'notTranslate');
Alert::success('Data berhasil disimpan')->persistent('Ok'); Alert::success('Data berhasil disimpan')->persistent('Ok');
......
...@@ -29,4 +29,9 @@ class Menu extends Model ...@@ -29,4 +29,9 @@ class Menu extends Model
{ {
return $this->hasOne(EnMenu::class, 'menu_id', 'id'); return $this->hasOne(EnMenu::class, 'menu_id', 'id');
} }
public function rChild()
{
return $this->hasMany(Menu::class, 'parent', 'id')->orderBy('urutan', 'asc')->with(['rChild', 'rEn']);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment