I've got 2 models, User, Role and a pivot table. In the pivot table are visible all appointed roles to the users(user_id | role_id) Currently in controller it's made that every line has it's own submit button for submitting roles to the database. I made only one button but it does not work, it throws exception "No query results for model [App\User]". Also I'm using ajax, but all that you will see in the code below.
I made it separate because generateUserTable also connects to the destroy and action method. Button is in generateUserTable -
<td><button type="button" id="potvrdi" class="potvrdi-button btn btn-primary" data-id="'.$row->id.'">
<div class="potvrdi">Potvrdi</div>
</button></td>
Controller -
public function postUserRole(Request $request)
{
if ($request->ajax()) {
$loged_in_user = User::findOrFail(Auth::user()->id);
$user = User::findOrFail($request->get('id'));
$r1 = Role::find(1);
$r2 = Role::find(2);
if ($loged_in_user->IsAdmin()) {
if ($request->get('admin_role')==1) {
$user->roles()->attach($r1);
} else {
$user->roles()->detach($r1);
}
if ($request->get('korisnik_role')==1) {
$user->roles()->attach($r2);
} else {
$user->roles()->detach($r2);
}
}
$data = User::all();
return json_encode($this->generateUserTable($data));
}
}
public function generateUserTable($data)
{
// return User::find(5)->roles;
$total_row = $data->count();
$output = "";
if ($total_row > 0) {
foreach ($data as $row) {
$roleNames = '';
$userRoles = $row->roles()->pluck('id')->toArray();
foreach (Role::all() as $roles1) {
$checked = '';
// var_dump($userRoles);
// var_dump($roles1->id);
// var_dump($roles1);
if (in_array($roles1->id, $userRoles, true)) {
$checked = 'checked="checked"';
}
$roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" '.$checked.' name="role" value="'.$roles1->id.'" class="checkbox'.$roles1->id.'" id="'.$roles1->id.'">'.' ' : '';
}
$output .= '
<tr>
<td>'.$row->surname.'</td>
<td>'.$row->name.'</td>
<td>'.$row->phone.'</td>
<td>'.$roleNames.'</td>
<td><button type="button" id="potvrdi" class="potvrdi-button btn btn-primary" data-id="'.$row->id.'">
<div class="potvrdi">Potvrdi</div>
</button></td>
<td><button type="button" id="rowId" class="remove-button btn btn-danger" data-id="'.$row->id.'">
<div class="close">x</div>
</button></td>
</tr>
';
}
} else {
$output = '
<tr>
<td align="center" colspan="5">Nema podataka</td>
</tr>
';
}
return array(
'table_data' => $output,
'total_data' => $total_row,
);
}
}
View - here is where I wish to "transfer" the submit button. I've put it after the table. Script is in view and ajax for submitting user roles to the database is the second one.
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">Pretraži korisnike</div>
<div class="panel-body">
<div class="form-group">
<input type="text" name="search" id="search" class="form-control" placeholder="Pretraži korisnike" />
</div>
<div class="table-responsive">
<h3 align="center">Broj korisnika: <span id="total_records"></span></h3>
<table id="users" class="table table-striped table-bordered">
<thead>
<tr>
<th>Prezime</th>
<th>Ime</th>
<th>Telefon</th>
<th>Rola</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<button type="button" id="potvrdi" class="potvrdi-button btn btn-primary">
<div class="potvrdi">Potvrdi</div>
</button>
</div>
</div>
</div>
<script>
$(document).ready(function(){
fetch_user_data();
function fetch_user_data(query = ''){
$.ajax({
url:"{{ route('live_search.action') }}",
method:'GET',
data:{query:query},
dataType:'json',
success:function(data)
{
$('tbody').html(data.table_data);
$('#total_records').text(data.total_data);
}
})
}
$(document).on('click', '.potvrdi-button', function(){
var id = $(this).data('id');
var admin_role = $(this).parent().parent().find('td:nth-child(4) .checkbox1:checkbox:checked').length;
console.log(admin_role);
var korisnik_role = $(this).parent().parent().find('td:nth-child(4) .checkbox2:checkbox:checked').length;
console.log(korisnik_role);
$.ajax({
url: "{{ route('live_search.postUserRole') }}",
method: "post",
data: {
id:id,
admin_role: admin_role,
korisnik_role: korisnik_role,
_token:'{{ csrf_token() }}'
},
dataType: 'json',
success: function(data) {
$('.checkbox1').prop('checked', false);
$('.checkbox2').prop('checked', false);
$('tbody').html(data.table_data);
$('#total_records').text(data.total_data);
}
})
})
EDIT Routes
Route::group(['middleware' => ['web', 'auth']], function () {
Route::get('/admin', 'PagesController@admin');
Route::post('/adminForma', 'PagesController@adminForma')->name('adminForma');
Route::get('/users', 'PagesController@users');
Route::get('/settings', 'PagesController@settings');
Route::post('/settings', 'PagesController@settings');
Route::get('/generateUserTable', 'PagesController@generateUserTable');
Route::post('/generateUserTable', 'PagesController@generateUserTable');
Route::get('/live_search/action', 'PagesController@action')->name('live_search.action');
Route::post('/live_search/action', 'PagesController@action');
Route::get('/live_search/postUserRole', 'PagesController@postUserRole')->name('live_search.postUserRole');
Route::post('/live_search/postUserRole', 'PagesController@postUserRole');
Route::get('/live_search/destroy', 'PagesController@destroy')->name('live_search.destroy');
Route::post('/live_search/generateUserTable', 'PagesController@generateUserTable');
Route::get('/live_search/generateUserTable', 'PagesController@generateUserTable')->name('live_search.generateUserTable');
});
I'm still a beginner so I apologize for spaghetti code, I'm trying my best.
Thank you