How to showing Data On Modal By ID - laravel

I have table data , its showing data from database .
i want to showing data with Action and its will showing Modal and data .
my view like this :
<div class="table-responsive">
<table class="table table-striped jambo_table bulk_action" id="table2">
<thead>
<tr class="headings">
<th class="column-title" style="display: table-cell;">No </th>
<th class="column-title" style="display: table-cell;">Nama Pegawai </th>
<th class="column-title" style="display: table-cell;">Lihat Data Keluarga </th>
</tr>
</thead>
<tbody>
#php
$no=0;
#endphp
#foreach ($keluarga as $i)
<tr class="even pointer">
<td class="a-center ">{{ ++$no }}</td>
<td class=" ">{{ $i->users->nama}} </td>
<td class=" "><a href="/project/d_keluarga/show-keluarga/{{$i->id}}"><button type="button" class="btn btn-danger" data-toggle="modal" data-target=".bs-example-modal-lg"> Show Data</button> // to showing data By Id on table
</td>
</tr>
#endforeach
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">Data Keluarga</h4>
</div>
<div class="modal-body">
<h4></h4>
//here to paste data
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</tbody>
</table>
</div>
My Controller :
public function keluarga()
{
$keluarga = Data_keluarga::with('users')->get();
return view('admin.keluarga',['keluarga' => $keluarga ]);
}
i usually parse it like $i->name and its in another page . not in Modal .
but , how to show data in modal by id ?

Here's an answer for your exact question.
Add data-title="{{ $i->users->nama }}" to your button.
<button type="button" class="btn btn-danger" data-toggle="modal" data-target=".bs-example-modal-lg" data-title="{{ $i->users->nama }}"> Show Data</button>
Add this jQuery snippet.
$('.bs-example-modal-lg').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var title = button.data('title')
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
// Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
var modal = $(this)
modal.find('.modal-title').text(title)
})

Related

How to hide a button inside a foreach with AlpineJS and Laravel

I am starting to use AlpineJS and I have a table that I fill with fetch.
The question is that I have tried to place x-if and x-show conditionals but they either hide all the buttons or show me all of them.
How could I do it?
<div class="iq-card-body" x-data="serviceTenants()" x-init="loadingTable()">
<table class="table table-bordered table-responsive-md table-striped_ text-center" id="laravel_crud">
<thead>
<tr>
<th>Company</th>
<th>Plan</th>
<th>created at</th>
<td colspan="3"></td>
</tr>
</thead>
<tbody>
<template x-for="tenant in tenants">
<tr>
<td x-text="tenant.name"></td>
<td x-text="tenant.plan"></td>
<td x-text="tenant.created"></td>
<td width="180">
<button class="btn iq-bg-info btn-rounded btn-sm my-0 float-left">Upgrade</button>
<button class="btn iq-bg-danger btn-rounded btn-sm my-0 float-left ml-2" #click="deleteTenant(tenant.id)">{{__('Delete')}}</button>
</td>
</tr>
</template>
</tbody>
</table>
</div>
function serviceTenants(){
return {
tenants: [],
loadBtn: false,
loadingTable(){
fetch('list')
.then(response => response.json())
.then(data => {
this.tenants = data.data;
});
}
}
}
You can use if condition inside the loop.
<template x-for="tenant in tenants">
<div>
<template x-if="tenant.showField === true">
<div>
<button class="btn iq-bg-info btn-rounded btn-sm my-0 float-left">Upgrade</button>
<button class="btn iq-bg-danger btn-rounded btn-sm my-0 float-left ml-2" #click="deleteTenant(tenant.id)">{{__('Delete')}}</button>
</div>
</template>
<template x-if="tenant.showField !== true">
<div>
<button class="btn iq-bg-info btn-rounded btn-sm my-0 float-left">Upgrade</button>
<button class="btn iq-bg-danger btn-rounded btn-sm my-0 float-left ml-2" #click="deleteTenant(tenant.id)">{{__('Delete')}}</button>
</div>
</template>
</div>
</template>

Trix editor doesn't show up when used inside a #if condition

In larave8 livewire I want to use the trix editor to create an bladeview on the livewire component InfoletterCrud is the following
<div class="container ml-auto mr-auto p-10 min-h-screen">
<div class="page-title ">{{ $page_title }}</div>
<div class="button-line flex flex-col">
#if ($isEditMode)
#if ($showMode)
#include('livewire.show-infoletter')
#else
#include('livewire.create-infoletter')
#endif
#else
<div>
<button wire:click="create()" class="button-cancel text-white font-bold py-2 px-4 rounded my-3">Ajouter une info-lettre</button>
</div>
#if (count($infoletters) > 0)
<table class="w-full table-fixed admin-stripped-table">
<thead>
<tr class="w-full">
<th class="w-1/12">Id</th>
<th class="w-10/12">Titre</th>
<th class="w-1/12">Actions</th>
</tr>
</thead>
<tbody>
#foreach ($infoletters as $infoletter)
<tr>
<td>{{ $infoletter->id }}</td>
<td>{{ $infoletter->title }}</td>
<td>
<div style="display:flex">
<button
wire:click="show({{ $infoletter->id }})"
class="fontawesome-icon">
<span data-toggle="tooltip" title="Voir" class="fa fa-paper-plane fa-xs "></span>
</button>
<button
wire:click="edit({{ $infoletter->id }})"
class="fontawesome-icon">
<span data-toggle="tooltip" title="Modifier" class="fa fa-edit fa-xs "></span>
</button>
<button
wire:click="$emit('openModal', 'livewire-modal', {{ json_encode(['type' => 'L\'infolettre', 'ident' => $infoletter->id]) }})"
class="fontawesome-icon">
<span data-toggle="tooltip" title="Supprimer" class=" fa fa-trash fa-xs "></span>
</button>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>,
#else
<p>Il n'y a aucune info-lettre</p>
#endif
#endif
</div>
</div>
When I place the trix component with #livewire('trix') just above the #if (isEditMode) test, it shows up. But it is not what I want because I am not in the Edit mode.
If I place it in the livewire.create-infoletter include, it doesn't show up.
Even when I place it just after the #if (isEditMode) test, it doesn't show up.
What is the trouble
You don't show your component and how you're initializing Trix, so this is just a guess. I'm assuming you're doing it with Alpinejs, and that Alpine has already made its DOM updates by the time the #if condition executes. Try calling your initialization function using $nextTick - see here: https://alpinejs.dev/magics/nextTick.

Update Data Using Modal on Laravel

I want to create edit data on laravel using a modal for view .
but i dont know how to catch one data on on my edit form .
i build controller like edit data on another page ( not using modals )
public function index()
{
$ipsrs = DB::table('ipsrs')->pluck('nama_bagian','id');
$belum_kerjakan = Aduan::with('users')->where('status','Belum Dikerjakan')->get();
$dalam_proses = Aduan::with('users')->where('status','Sedang Dikerjakan')->get();
$selesai = Aduan::with('users')->where('status','Selesai')->get();
return view('admin.admin_dashboard',[
'belum_dikerjakan' => $belum_kerjakan,
'dalam_proses' => $dalam_proses,
'selesai' => $selesai,
'ipsrs' => $ipsrs,
]);
}
public function change_aduan($id)
{
$aduan = Aduan::findOrFail($id);
if (!$aduan)
abort(404);
return view('admin.admin_dashboard',[
'aduan' => $aduan
]);
}
this route . ( but i dont know where i used this route ?
Route::get('admin_dashboard', 'AdminController#index')->name('admin_dashboard'); // this route for view table
Route::get('change/{id}','AdminController#change_aduan')->name('change_aduan'); // this route "will" showing data on this modals
my View
<div class="card-body" align='center'>
<div class="card-body p-0">
<table class="table table-condensed">
<thead align="center">
<tr class="satu">
<th style="width: 3%">#</th>
<th style="width: 10%">Nama Unit</th>
<th style="width: 15%">Tanggal Aduan</th>
<th style="width: 8%">Nama Pengadu</th>
<th style="width: 8%">Tujuan Aduan</th>
<th style="width: 30%">Isi Aduan </th>
{{-- <th>Label</th> --}}
<th style="width: 5%">Status</th>
<th style="width: 5%">Action</th>
</tr>
</thead>
<tbody>
#php
$no=0;
#endphp
#foreach ($belum_dikerjakan as $i)
<tr>
<td> {{++$no}} </td>
<td> {{$i->users->nama_unit}}</td>
<td> {{$i->created_at}} </td>
<td> {{$i->nama_pengadu}} </td>
<td> {{$i->ipsrs->nama_bagian}} </td>
<td> {{$i->aduan}}</td>
<td><span class="badge bg-danger"> Belum Dikerjakan</span></td>
<td><button type="button" class="btn btn-default" data-toggle="modal" data-id="{{$i->id}}" data-target="#modal-lg" > // modals button
Action
</button></td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
<div class="modal fade" id="modal-lg">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title"> Change Status </h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{-- Here this Form --}}
{{-- {{$aduan->nama_pengadu}} --}} // this Line is failur
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
i dont know how to next step , on another tutorial this data is can showed using {{ $data->name }} , but in my view cant do this . whats wrong ,and how i can solved this ?
I think you are not far off. I believe the issue you are having is that you are trying to pull the name field from a collection instead of an object.
In your Blade file, this line:
<td> {{$i->users->nama_unit}}</td>
is essentially saying: for this iteration ($i) of $belum_dikerjakan, give me ALL the users and tell me the name of the collection of users. The collection doesn't have a name field. Because you have many users for this $belum_dikerjakan, you will either need to loop on all of them, identify the specific user by id (example $i->users->where('id', 1)->first()->nama_unit), or take the first one randomly:
$i->users->first()->nama_unit
What these examples do, is it pulls a single object user instead of a collection, and thus you have the field name on that object.
This line:
<td> {{$i->ipsrs->nama_bagian}} </td>
doesn't look like a relationship at all, and you may need to refactor this into either a relation on $belum_dikerjakan, or remove it from the loop and use it independently, but this is a separate issue I think.
Hope this helps.

I have a problem with my laravel pagination

I am new to Laravel. I am trying to create pagination with the data in my bootstrap table.
This is my index Controller function:
$Courses=Courses::orderBy('name','desc')->paginate(1);
return view('Courses.index')->with('Courses',$Courses);
I am getting this error:
Call to undefined method App\Courses::links() (View:
C:\laragon\www\TharakaCollege\resources\views\Courses\index.blade.php)
The information provided is not enough, however I think the problem is in your view. You have to have a code like this in your blade file:
<div class="pages">
<ul class="pagination ">
<li>{{$Courses->appends(request()->query())->links()}}</li>
</ul>
</div>
Moreover you'd better edit your returning view like this:
return view('Courses.index', array('Courses'=> $Courses));
This is the code in Courses/index.blade.php
<div id="page-wrapper">
#include('includes.message');
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="row">
<div class="col-lg-12">
<div class="panel panel-info">
<div class="panel-heading">
Courses
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div class="table-responsive">
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th class="text-center">#</th>
<th class="text-center"> code</th>
<th class="text-center">Course Name</th>
<th class="text-center">Category</th>
<th class="text-center">Requirements</th>
</tr>
</thead>
<tbody>
#if(count($Courses)>0)
#foreach($Courses as $Courses)
<tr class="item{{$Courses->id}}">
<td>{{$Courses->id}}</td>
<td>{{$Courses->code}}</td>
<td>{{$Courses->name}}</td>
<td>{{$Courses->category}}</td>
<td>{{$Courses->requirements}}</td>
<td><a class="btn btn-info btn-sm" href="/Courses/{{$Courses->id}}/edit">Edit</a>
</td>
<td>
<form method="POST" action="{{route('Courses.destroy',$Courses->id)}}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" class="btn btn-danger btn-sm" value="Delete">
</form>
</td>
</tr>
#endforeach
#else
<p>No Courses</p>
#endif
</tbody>
</table>
{{ $Courses->links() }}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
You should try this:
<div class="pull-left">
<div class="row">
<div class="col-xs-12">
<div class="explore-pagination">
<nav>
<div class="pagination"> {{ $Courses->render() }}</div>
</nav>
</div>
</div>
</div>
</div>
Try this code instead (modified version of yours):
<div id="page-wrapper">
#include('includes.message');
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="row">
<div class="col-lg-12">
<div class="panel panel-info">
<div class="panel-heading">
Courses
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div class="table-responsive">
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th class="text-center">#</th>
<th class="text-center"> code</th>
<th class="text-center">Course Name</th>
<th class="text-center">Category</th>
<th class="text-center">Requirements</th>
</tr>
</thead>
<tbody>
#if(count($Courses)>0)
#foreach($Courses as $Course)
<tr class="item{{$Course->id}}">
<td>{{$Course->id}}</td>
<td>{{$Course->code}}</td>
<td>{{$Course->name}}</td>
<td>{{$Course->category}}</td>
<td>{{$Course->requirements}}</td>
<td><a class="btn btn-info btn-sm" href="/Courses/{{$Course->id}}/edit">Edit</a>
</td>
<td>
<form method="POST" action="{{route('Courses.destroy',$Course->id)}}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" class="btn btn-danger btn-sm" value="Delete">
</form>
</td>
</tr>
#endforeach
#else
<p>No Courses</p>
#endif
</tbody>
</table>
{{$Courses->appends(request()->query())->links()}}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
I have modified your foreach loop as well as ->links().
When calling the paginate method, you will receive an instance of Illuminate\Pagination\LengthAwarePaginator. In addition to these helpers methods, the paginator instances are iterators and may be looped as an array. So, once you have retrieved the results, you may display the results and render the page links using Blade:
Your blade should contain:
<div class="container">
#foreach ($courses as $course)
{{ $course->fieldName }}
#endforeach
</div>
{{ $courses->links() }}

Bootstrap data table search and pagination not working when data is load from ajax call

I am trying to working on bootstrap data table in Laravel. My data is load successfully but data table search and pagination is not working.so please help me.
my Html code on page is like that
<table id="example111" class="table table-striped nowrap www" cellspacing="0" width="100%">
<thead>
<tr>
<th class="col-sm-2 col-lg-1">Sr.No.</th>
<th>Department Name</th>
<th class="col-sm-3 col-lg-2">Edit/Delete</th>
</tr>
</thead>
<tbody class="departmentlist" id="departmentlist111"></tbody>
</table>
my ajax call on page like that
<script type="text/javascript" language="javascript" class="init">
$(document).ready(function() {
table = $('#example111').DataTable({
paging: false,
});
table.destroy();
$('#example111').DataTable({
"serverSide": true,
"ajax": {
"url": "<?= URL::to('show_department')?>",
"dataType": "json",
"type": "post",
"data":{ _token: "{{csrf_token()}}"},
"dataSrc": function (e) {
$("#departmentlist111").html(e);
}
}
});
});
</script>
my route call on page is like
Route::post('show_department','admin\DepartmentController#show_department');
my controller call
public function show_department()
{
$cartdata = '<tr>
<td class="col-sm-2 col-lg-1"> 1</td>
<td >South Indian</td>
<td class="col-sm-3 col-lg-2">
<button type="button" class="btn btn-info btn-sm"><i class="glyphicon glyphicon-edit"></i></button>
<button type="button" class="btn btn-danger btn-sm"><i class="glyphicon glyphicon-remove"></i></button>
</td>
</tr>
<tr>
<td class="col-sm-2 col-lg-1"> 1</td>
<td >South </td>
<td class="col-sm-3 col-lg-2">
<button type="button" class="btn btn-info btn-sm"><i class="glyphicon glyphicon-edit"></i></button>
<button type="button" class="btn btn-danger btn-sm"><i class="glyphicon glyphicon-remove"></i></button>
</td>
</tr>
<tr>
<td class="col-sm-2 col-lg-1"> 1</td>
<td> Indian</td>
<td class="col-sm-3 col-lg-2">
<button type="button" class="btn btn-info btn-sm"><i class="glyphicon glyphicon-edit"></i></button>
<button type="button" class="btn btn-danger btn-sm"><i class="glyphicon glyphicon-remove"></i></button>
</td>
</tr>
<tr>
<td class="col-sm-2 col-lg-1"> 1</td>
<td >South Indian</td>
<td class="col-sm-3 col-lg-2">
<button type="button" class="btn btn-info btn-sm"><i class="glyphicon glyphicon-edit"></i></button>
<button type="button" class="btn btn-danger btn-sm"><i class="glyphicon glyphicon-remove"></i></button>
</td>
</tr>';
echo json_encode($cartdata);
}
so please solve my error please.

Resources