i have created the form normally, but after I want to parse the data into index, there is data is not showed normally..
This a code for model Form
protected $guarded = [];
public function user(){
return $this->hasOne('App\User', 'id', 'user_id'); //Product Model Name
}
public function location(){
return $this->hasOne('App\Locations', 'id', 'location_id');
}
public function purpose(){
return $this->hasOne('App\Purpose', 'id', 'purpose_id');
}
public function connection(){
return $this->hasMany('App\FormConnection', 'id', 'form_connection'); //Product Model Name
}
This a code for model FormConnection
public function form(){
return $this->belongsTo('App\Form', 'id', 'form_id'); //Product Model Name
}
This is for Views of Modal
<div class="modal fade" id="exampleModal{{$form->id}}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Form Information</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p class="badge badge-pill badge-dark">Name: {{$form->user->name}}</p>
<p>Purpose : {{$form->purpose->name}}</p>
<p>Location : {{$form->location->location}}</p>
<!-- <table class="table">
<tbody>
<tr>
<th scope="row">Connection</th>
<td><input type="checkbox" name="connection[]" value="request">Request</td>
<td><input type="checkbox" name="connection[]" value="connection">Connection</td>
</tr>
</tbody>
</table> -->
#foreach()
<div class="col-md-6 mt-2">
<label for="">waw</label>
</div>
#endforeach
<p>Division : {{$form->division}}</p>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
I Hope someone can help me, I really appreciate that:))
output: The connection should show all connection that has been checked before
enter image description here
Related
I got an error that says:
Error Number: 1048
Column 'ket' cannot be null
INSERT INTO tb_uploadv (ket, tgl, video) VALUES (NULL, NULL, NULL)
Filename: C:/xamppV5/htdocs/sicams/system/database/DB_driver.php
Line Number: 691
can all master help me pls.
my controller: controllers/Uploadv.php
<?php
class Uploadv extends CI_Controller{
public function index(){
$data['uploadv'] = $this->m_uploadv->tampil_data()->
result();
$this->load->view('templates/header');
$this->load->view('templates/sidebarupv');
$this->load->view('uploadv', $data);
$this->load->view('templates/footer');
}
public function tambah_aksi()
{
$data = [
'ket' => $this->input->post('ket'),
'tgl' => $this->input->post('tgl'),
'video' => $this->input->post('video'),
];
$this->m_uploadv->input_data($data, 'tb_uploadv');
redirect('uploadv/index');
}
}
?>
my view: views/uploadv.php
<div class="content-wrapper">
<section class="container-fluid">
<h5>
Data Rekaman
<small>Silahkan Upload Data Rekaman</small>
</h5>
</section>
<section class="content">
<button class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"><i class="fa fa-book"></i>Tambah Data</button>
<table class="table">
<tr>
<th>No</th>
<th>Keterangan</th>
<th>Tanggal Rekaman</th>
<th>File Rekaman</th>
</tr>
<?php
$no = 1;
foreach ($uploadv as $upv) : ?>
<tr>
<td><?php echo $no++ ?></td>
<td><?php echo $upv->ket ?></td>
<td><?php echo $upv->tgl ?></td>
<td><?php echo $upv->video ?></td>
</tr>
<?php endforeach; ?>
</table>
</section>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">FORM UPLOAD REKAMAN</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="post" action="<?php echo base_url().'uploadv/tambah_aksi';?>">
<div class="form-group">
<label>Keterangan</label>
<input type="text" name="keterangan" class="form-control">
</div>
<div class="form-group">
<label>Tanggal Rekaman</label>
<input type="date" name="tanggal_rekam" class="form-control">
</div>
<div class="form-group">
<label>File Rekaman</label>
<input type="text" name="bukti" class="form-control">
</div>
<button type="reset" class="btn btn-danger"
data-dismiss="modal">Batal</button>
<button type="submit" class="btn btn-primary">Simpan</button>
</form>
</div>
</div>
</div>
</div>
</div>
My Model: models/M_uploadv.php
<?php
class M_uploadv extends CI_Model {
public function tampil_data()
{
return $this->db->get('tb_uploadv');
}
public function input_data($data)
{
$this->db->insert('tb_uploadv', $data);
}
}
You're submitting input field with another names, you input names should be match.
Try this code.
public function tambah_aksi()
{
$data = [
'ket' => $this->input->post('keterangan'),
'tgl' => $this->input->post('tanggal_rekam'),
'video' => $this->input->post('bukti'),
];
$this->m_uploadv->input_data($data, 'tb_uploadv');
redirect('uploadv/index');
}
Im new to laravel eloquent and i want to know if that loading times are normally with only five entries
Even a simple select all query has ~720 ms specially on edit button that is first photo (modal) the data comes after one second that is noticeable,
Im using Livewire i dont know if affect that too much
The main question that affects extra loading time is how to have access (Aggregate) on child table that counts only the sum of the services, i wrote a simple count on my html {{ $customer->services->count()}} table as you can see runs on each row that is wrong because adds extra loading time.
On php i make something like :
$sql = "SELECT name,vat,id";
$sql .= " ,(select sum(taskcharges) from charges where charges.customers_id=customers.id) as taskcharges_sum";
$sql .= " ,(select sum(payment) from charges where charges.customers_id=customers.id) as payment_sum";
$sql .= " ,(select sum(taskcharges-payment) from charges where charges.customers_id=customers.id) as balance_sum";
$sql .= " ,(select name WHERE customers.id=$id) ";
$sql .= " FROM customers ";
I want to have access from Customers on specific child(services) columns to make functions like max,sum,count the tables are binded on model so i want to avoid extra code of join queries
plus as you can see on sorting the "services" isnt column of Customers table so they dont recognize the column services on customers table, if i had something like "select count(services) as servicecount then i will able to recognize the sorting as new column of the table customers
Above you can see my code:
customers/show.blade.php:
<div class="container">
<p>Sort column:{{$selectedItems}}</p>
<p>Selected Direction:{{$action}}</p>
<!-- Button trigger modal -->
<button wire:click.prevent="addNew" type="button" class="btn btn-primary">
Add New User
</button>
<!-- button triger Modal -->
<div class="modal fade" id="form" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Insert</h5>
<button type="button" wire:click.prevent="close"class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
#livewire('customers.form')
</div>
</div>
</div>
</div>
<div class="modal fade" id="delete" tabindex="-1" role="dialog" aria-labelledby="delete" aria-hidden="true" wire:ignore>
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="delete">Delete</h5>
<button type="button" wire:click.prevent="close" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h3>Do you wish to continue?</h3>
</div>
<div class="modal-footer">
<button type="button" wire:click="close" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button wire:click="delete" class="btn btn-primary">Delete</button>
</div>
</div>
</div>
</div>
<h1 class="text-center">Customers {{count($customers)}}</h1>
<div>
<div class="w-full flex pb-10">
<div class="w-3/3 mx-1">
<input wire:model.debounce.300ms="search" type="text" class="form-control" placeholder="Search users...">
</div>
<div class="row mb-4">
<div class="col form-inline">
Per Page:
<select wire:model="perPage" class="form-control">
<option>5</option>
<option>10</option>
</select>
</div>
</div>
</div>
<table class="table-auto w-full mb-6">
<thead>
<tr>
<th wire:click="sortBy('id')" style="cursor: pointer;" class="px-4 py-2">ID
#include('layouts.partials.sort_icons',['field'=>'id'])
</th>
<th wire:click="sortBy('name')" style="cursor: pointer;" class="px-4 py-2">Name
#include('layouts.partials.sort_icons',['field'=>'id'])</th>
<th wire:click="sortBy('plate')" style="cursor: pointer;" class="px-4 py-2">Plate
#include('layouts.partials.sort_icons',['field'=>'id'])</th>
<th wire:click="sortBy('services')" style="cursor: pointer;" class="px-4 py-2">Services
#include('layouts.partials.sort_icons',['field'=>'services'])</th>
<th class="px-2 py-2">Action</th>
</tr>
</thead>
<tbody>
#foreach($customers as $customer)
<tr>
<td class="border px-4 py-2">{{ $customer->id }}</td>
<td class="border px-4 py-2">{{ $customer->name }}</td>
<td class="border px-4 py-2">{{ $customer->plate }}</td>
<td class="border px-4 py-2">{{ $customer->services->count()}}</td>
<td class="border px-2 py-2">
<button wire:click="selectItem({{$customer->id}},'update')" class="btn btn-info"><i class="fa fa-edit"></I></button></a>
<button wire:click="selectItem({{$customer->id}},'delete')" class="btn btn-danger"><i class="fa fa-trash"></I></button></a>
</td>
</tr>
#endforeach
</tbody>
</table>
<div class="paginate">{{$customers->links()}}</div>
</div>
</div>
customers/Show.php :
<?php
namespace App\Http\Livewire\Customers;
use App\Models\Customer;
use Livewire\Component;
use App\Page;
use Illuminate\Support\Str;
use App\Http\Livewire\Column;
use Livewire\WithPagination;
class Show extends Component
{
public $sortBy= 'name';
public $sortDirection = 'asc';
public $headers;
public $perPage ='5';
public $search;
public $action;
public $selectedItems;
public function sortBy($field){
if ($this->sortDirection =='asc'){
$this ->sortDirection ='desc';
}
else{
$this->sortDirection ='asc';
}
return $this ->sortBy = $field;
}
public function selectItem($itemId,$action){
$this->selectedItems=$itemId;
$this->action=$action;
if ($action=='delete'){
$this->dispatchBrowserEvent('show-deletemodal');
} else{
$this->emit('getcustomerID', $this->selectedItems);
$this->dispatchBrowserEvent('show-modal');
}
}
public function delete(){
Customer::destroy($this->selectedItems);
$this->dispatchBrowserEvent('hide-modal');
}
public function addNew()
{
$this->dispatchBrowserEvent('show-modal');
}
public function close()
{
$this->dispatchBrowserEvent('hide-modal');
}
protected $listeners = [
'deleteconfirmed'=>'$selectedItems',
'refreshParent'=>'$refresh'
];
public function render()
{
$customers = Customer::query()
->search($this->search)
->orderBy($this->sortBy,$this->sortDirection)
->paginate($this->perPage);
return view('livewire.customers.show',['customers'=>$customers
])
->extends('admin.dashboard')
->section('content');
}
}
customers/form.php :
<?php
namespace App\Http\Livewire\Customers;
use App\Models\Customer;
use Livewire\Component;
class Form extends Component
{
public $name ='';
public $plate = '';
public $customerId ;
protected $listeners = [
'getcustomerID'
];
protected function rules() {
return [
'name' => 'required',
'plate' => ['required', 'regex:/[A-Z]{3}\d{4}$/','unique:customers,plate,' . $this->customerId],
];
}
public function updated($propertyName)
{
$this->validateOnly($propertyName);
}
public function getcustomerID($customerId){
$this->customerId=$customerId;
$customer= customer::find ($this->customerId);
$this->name=$customer->name;
$this->plate= $customer->plate;
}
public function save()
{
$this->validate();
$data = [
'name' => $this->name,
'plate' => $this->plate,
];
if ($this->customerId){
customer::find ($this->customerId)->update($data);
session()->flash('message', 'User successfully updated.');
} else{
customer::create($data);
}
$this->emit('refreshParent');
$this->dispatchBrowserEvent('hide-modal');
$this ->ClearVars();
}
public function ClearVars(){
$this->name=null;
$this->plate=null;
}
public function render()
{
return view('livewire.customers.form');
}
}
Models/Customer.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
use HasFactory;
protected $fillable = [
'name',
'plate'
];
public function services()
{
return $this->hasMany(Service::class);
}
public function parts()
{
return $this->hasMany(Part::class, 'customer_id');
}
public function scopeSearch($query , $val){
return $query
->where('name','like','%'.$val.'%')
->Orwhere('plate','like','%'.$val.'%');
}
public function user()
{
return $this->belongsTo(User::class);
}
}
Models/Services.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Service extends Model
{
protected $fillable = [
'service_type',
'km',
];
public function customer()
{
return $this->belongsTo(Customer::class);
}
public function parts()
{
return $this->hasMany(part::class);
}
}
It was so simple.. just added ->withCount('services') above of query
Still have that strange delay
Update :
The problem with slow response was that trying to retrieve data from remote sql server, I change to local and I have less than 100ms!
I want to check whether the particular id has password or not.
view user fuction in controller
public function viewUser(){
$users = User::orderBy('user_id','ASC')->get();
return view('admin.user.view_user')->with(compact('users'));
}
in viewuser.blade.php
<div class="container">
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Actual Password</h4>
</div>
<div class="modal-body">
<p><?php if(!empty($User->actual_password)) echo "Actual Password:"." $User->actual_password " ; else echo "No Password" ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
but its not working
Please check the password not to be hidden in the model
protected $hidden = ['password', 'remember_token' ];
actual_password !? Did you select the field name yourself? if no! change it "password"
You can use php in the blade as follows
#if($user->actual_password == null)
<p>your text..</p>
#else
<p>Actual Password: {{ $user->actual_password }}</p>
#endif
Note that variables are case sensitive
so $User is not equal to $user
is it possible to use the index view to perform update/edit at the same time using modal form? i have index view with table displaying the data,and inside the table it has button named [edit and delete] now i want to perform edit/update once the edit button is click then modal form will come out..? but whenever is use the modal form it will show error like this image.
This is my Controller:
public function show_setup()
{
$batch=Batch::all();
return view('setup.show_batch',compact('batch'));
}
public function edit_batch(request $request)
{
$batch = Batch::find ($request->id);
$batch->batch_name = $request->batch_name;
$batch->save();
return redirect()->back();
}
My view
<form>
<div class="form-group">
<table class="table table-hover table-bordered" id="table">
<tr>
<th>Batch</th>
<th>Action</th>
</tr>
#foreach($batch as $bt)
<tr>
<td>{{$bt->batch_name}}</td>
<td>
Edit
Delete
</td>
</tr>
#endforeach
</table>
</div>
</form> <!-- Modal-->
<div id="edit_batch" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" class="modal fade text-left">
<div role="document" class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 id="exampleModalLabel" class="modal-title">Edit Batch</h5>
<button type="button" data-dismiss="modal" aria-label="Close" class="close">
<span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<form action="setup/batch/edit" method="POST">
{{csrf_field()}}
{{ method_field('PUT') }}
<div class="form-group">
<label>School Year</label>
<input type="text" placeholder="School Year" name="batch_name" value="{{$batch->batch_name}}" class="form-control" autocomplete="off">
</div>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn btn-secondary">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
<!--End batch Modal-->
The problem is that you want to get the batch_name from the collection $batch you have to pass the data you want using the javaScript the data you need are the id and the batch_name
Here is an example :
<form>
<div class="form-group">
<table class="table table-hover table-bordered" id="table">
<tr>
<th>Batch</th>
<th>Action</th>
</tr>
#foreach($batch as $bt)
<tr>
<td>{{$bt->batch_name}}</td>
<td>
Edit
Delete
</td>
</tr>
#endforeach
</table>
</div>
</form>
<!-- Modal-->
<div id="edit_batch" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" class="modal fade text-left">
<div role="document" class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 id="exampleModalLabel" class="modal-title">Edit Batch</h5>
<button type="button" data-dismiss="modal" aria-label="Close" class="close">
<span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<form action="setup/batch/edit" method="POST">
{{csrf_field()}}
{{ method_field('PUT') }}
<input id="batch_id" type="hidden" name="id">
<div class="form-group">
<label>School Year</label>
<input id="batch_name" type="text" placeholder="School Year" name="batch_name" class="form-control" autocomplete="off">
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn btn-secondary">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!--End batch Modal-->
<script type="text/javascript">
$('.editBtn').on('click',function(e)
{
var link = $(this);
batch_id = link.data("id");
batch_name = link.data("batch_name");
$("#batch_id").val(batch_id);
$("#batch_name").val(batch_name);
});
</script>
i want to edit table row using bootstrap model....when i click on edit button it open up a model box with a name of designttion which i want to edit
..but when when i click on button only modalbox show but no record is shown in it...
please solve my problem
Controller
public function edit_desig(){
$this->load->model('designation_model');
$this->designation_model->edit_designation($id);
}
model
public function edit_designation($id){
$query=$this->db->get_where('designation',array('desig_id'=>$id));
return $query->result();
}
My view
<div id="myModal" class="modal fade" id="editdesigymodal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Designation</h4>
</div>
<div class="modal-body">
<input id="editdesignnation" type="text" class="form-control" placeholder="Enter here" />
</div>
<div class="modal-footer">
<button type="button" onclick="edit_designation()" class="btn btn-success" data-dismiss="modal">Edit</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-12 col-sm-12">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>Designations</th>
</tr>
</thead>
<tbody>
<?php print_r($designation); $i=1;foreach($designation as $desig): ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $desig['name']; ?></td>
<td><input type="button" value="Delete" class="btn btn-danger"/>
<td class="numeric"><a id= "<?php echo $desig['dkey']; ?>" onclick="edit_designation('<?php echo $desig['dkey']; ?>')" data-target="#myModal" data-toggle="modal" href="<?php echo base_url().'index.php/designation/edit_desig'; ?>"><button class="btn blue" type="button">Edit</button>
</tr>
<?php $i++;endforeach;?>
You need to pass your query result to your view
Controller
public function edit_desig(){
$this->load->model('designation_model');
$designation=$this->designation_model->edit_designation($id);// assing your query result to your variable
$data['designation'] = designation;// pas your variavle to data arrray
$this->load->view('view_name', $data);// load your view with array
}
As per your view code you need to fetch data in array form in your model
Models
public function edit_designation($id){
$query=$this->db->get_where('designation',array('desig_id'=>$id));
return $query->result_array();// change this to array
}