Привет, у меня есть таблица с тремя соединениями, которая, естественно, будет иметь повторяющееся имя столбца, например id. Короче говоря, у меня есть 3 таблицы:
bas_role
id
name
bas_app
id
app_name
bas_role_app
id
role_id
app_id
My Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use Illuminate\Support\Facades\Route;
use App\User;
use App\Role;
use App\App;
use App\ActivityLog;
use App\RoleApp;
use DataTables;
use Illuminate\Support\Facades\Input;
use Illuminate\Database\Eloquent\JsonEncodingException;
use DB;
class RoleAppController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function display(Request $request)
{
// $role = \App\Role::first();
// $app = \App\App::first();
// // $role->apps()->attach($app);
// $app->roles()->attach($role);
$routes = preg_match('/([a-z]*)@([a-z]*)/i', Route::currentRouteAction(), $matches);
$routes = $matches[0];
$action = $matches[2];
if (Auth::check()) {
$id = Auth::id();
DB::beginTransaction();
try {
$profile_data = User::find($id);
ActivityLog::create([
'inserted_date' => Carbon::now()->TimeZone('asia/jakarta'),
'username' => $profile_data->username,
'application' =>$routes,
'creator' => "System",
'ip_user' => $request->ip(),
'action' => $action,
'description' => $profile_data->username. " is looking at roleApp",
'user_agent' => $request->server('HTTP_USER_AGENT')
]);
// $pagination = TRUE;
$app =DB::table('bas_role_app')
->join('bas_role_app','bas_app.id','=','bas_role_app.app_id')
->join('bas_role','bas_role_app.role_id','=','bas_role.id')
->from('bas_app')
// ->where('role_user.role_id', 4)
->select('*')
// ->from('bas_app')
// // ->where('role_user.role_id', 4)
->Orderby('bas_role_app.id')
->get();
// dd($app);
DB::commit();
} catch (\Exception $ex) {
DB::rollback();
return response()->json(['error' => $ex->getMessage()], 500);
}
// dd($app);
return view('/roleapp', ['app' => $app]);
}else {
return view("login");
}
}
public function getroleapp(){
$app = DB::table('bas_role_app')
->join('bas_role_app','bas_app.id','=','bas_role_app.app_id')
->join('bas_role','bas_role_app.role_id','=','bas_role.id')
->from('bas_app')
->select('*')
->get();
return Datatables::of($app)->make(true);
}
public function update(Request $req, $id)
{
// if(request()->ajax())
// {
// $data = DB::table('bas_role_app')->findOrFail($id);
// return response()->json(['result' => $data]);
// }
}
public function delete(Request $request, $id)
{
try {
$role = RoleApp::findOrFail($id);
// // $app->roles()->detach();
$role->apps()->detach();
// // $app->delete();
$role->delete();
// $app_id = (int)Request('visit_id');
// $role_id = (int)Request('product_id');
DB::commit();
} catch (\Exception $ex) {
DB::rollback();
return response()->json(['error' => $ex->getMessage()], 500);
}
return redirect('/roleapp');
}
public function insert(Request $request)
{
}
public function descriptionLog($id, $temp, $oldData, $newData)
{
$newString = '<div><table class="table table-striped"><tr><td scope="col"><b>ID:</b> </td><td><b>' . $id . '</b></td><td></td></tr><tr><td><b>Field</b></td><td><b>Old Data</b></td><td><b>New Data</b></td></tr>';
$arr = '';
for ($k = 0; $k < count($oldData); $k++) {
if ($oldData[$k] != $newData[$k]) {
$arr = '<tr><td>' . $temp[$k] . '</td><td>' . $oldData[$k] . '</td><td>' . $newData[$k] . '</td></tr>';
}
}
$newString = $newString . $arr . '</table></div>';
return $newString;
}
}
My View
@extends('layouts.master')
@section('title','App')
@section('content')
<div class="container mt-5">
<div class="row">
<div class="col-12">
<!-- Modal -->
<div class="float-left">
<div class="form-group mx-sm-3 mb-2">
<form class="form-inline">
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#exampleModalCenter">
<i class="ni ni-fat-add"></i>
</button>
</div>
</form>
</div>
{{-- <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">New Role App</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="roleapp/create" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="username">Role Name</label>
<input type="text" class="form-control" name="app_name" required>
</div>
<div class="form-group">
<label for="Type">App Name</label>
<input type="text" class="form-control" name="app_type" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal"><i class="ni ni-fat-remove"></i></button>
<button type="submit" class="btn btn-primary"><i class="ni ni-check-bold"></i></button>
</div>
</form>
</div>
</div>
</div> --}}
<table class="table table-striped" id="app">
<thead>
<tr>
<th scope="col">Role Name</th>
<th scope="col">App Name</th>
<th scope="col">Action</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody id="dynamic-row">
</tbody>
</table>
{{-- @foreach ($app as $p)
<div class="modal fade" id="modal_edit_{{$p->id}}" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Edit App</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="app_form_{{$p->id}}" action="{{ url('update', [$p->id]) }}" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}" form="app_form_{{$p->id}}" />
<div class="form-group">
<label for="username">Name App</label>
<input type="text" class="form-control" name="app_name" value="{{$p->app_name}}">
</div>
<div class="form-group">
<label for="Type">Type App</label>
<input type="text" class="form-control" name="app_type" value="{{$p->app_type}}">
</div>
<div class="form-group">
<label for="name">Description</label>
<input type="text" class="form-control" name="description" value="{{$p->description}}">
</div>
<div class="form-group">
<label for="text">Nama Menu</label>
<input type="text" class="form-control" name="menu_name" value="{{$p->menu_name}}">
</div>
<div class="form-group">
<label for="phone">URL Menu</label>
<input type="text" class="form-control" name="menu_url" value="{{$p->menu_url}}">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal"><i class="ni ni-fat-remove"></i></button>
<button type="submit" form="app_form_{{$p->id}}" class="btn btn-primary"><i class="ni ni-check-bold"></i></button>
</div>
</form>
</div>
</div>
</div>
@endforeach --}}
@foreach ($app as $p)
<div class="modal fade" id="modal_hapus_{{$p->id}}" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Data akan dihapus</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Data yang dihapus tidak dapat dikembalikan!
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" data-dismiss="modal"><i class="ni ni-fat-remove"></i></button>
<a href="{{ url('roleapp/delete', [$p->id]) }}" class="btn btn-danger"><i class="ni ni-check-bold"></i></a>
</div>
</div>
</div>
</div>
@endforeach
</div>
</div>
</div>
<link rel="stylesheet" href="{{ asset('css/breadcrumb.css') }}">
<script>
//load search, individual or not
var row = 0;
$(document).ready(function() {
$('#app thead tr').clone(true).appendTo( '#app thead' );
$('#app thead tr:eq(1) th').each( function (i) {
if (row < 2) {
var title = $(this).text();
$(this).html('<input type="text" placeholder="Search ' + title + '" />');
} else {
$(this).html('');
}
row++;
$( 'input', this ).on( 'keyup change', function () {
if ( table.column(i).search() !== this.value ) {
table
.column(i)
.search( this.value )
.draw();
}
} );
} );
//load datatable processing, using serverside yajra, with pagination option, also action button as well
var table = $('#app').DataTable({
"processing": true,
"serverSide": true,
"ajax": "{{ route('ajaxdata.getroleapp') }}",
"lengthMenu" : [[20, 50, 100, 500, 1000, -1],[20, 50, 100, 500, 1000, "All"]],
"columns":[
{ "data": "name" },
{ "data": "app_name" },
{
sortable: false,
"render": function ( data, type, full, meta ) {
return '<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal_edit_'+full.id+'"><i class="ni ni-single-02"></i></button>';
}
},
{
sortable: false,
"render": function ( data, type, full, meta ) {
return '<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#modal_hapus_'+full.id+'"><i class="ni ni-fat-delete"></i></button>';
}
},
],
});
} );
</script>
@endsection
Вот уловка, я хочу для загрузки идентификатора столбца bas_role_app, чтобы мне было легче делать это. Но когда я делаю dd ($ app);, он всегда возвращает результат, например, так:
+"id": 4
+"app_name": "app"
+"app_type": null
+"description": "display app"
+"menu_name": "app"
+"menu_url": "/app"
+"menu_parent_id": null
+"role_id": 4
+"app_id": 7
+"priv_access": "N"
+"priv_insert": "N"
+"priv_delete": "N"
+"priv_update": "N"
+"priv_export": "N"
+"priv_print": "N"
+"name": "siswa"
+"remark": "study"
из этого, я знаю, что запрос загружает неправильный идентификатор для процесса, потому что в bas_role_app , он достиг около 1100 идентификаторов (появляются фиктивные данные ....)
Итак, мой вопрос: как мне загрузить идентификатор bas_role_app, чтобы я мог использовать его при просмотре foreach l oop (вместо идентификатор bas_role и bas_app), и я тоже могу делать crud?