I'm having a problem I've never experienced before, I'm doing a fix function for a prescription.
I already added it but one problem when I fixed it.
I use livewire so that when I click on the drug it will show the potion by drug
Livewire Class
<?php
namespace App\Http\Livewire;
use App\Models\MedicineCategory;
use App\Models\Medicine;
use App\Models\Prescription;
use Livewire\Component;
class SelectedOldMedicineCategoryTable extends Component
{
public $columns = ['STT', 'Name', 'Quantity', 'Unit'];
public $medicines;
public $oldMedicines;
public $oldMedicineCategory;
public $prescription;
public array $selectedMedicineArray = [];
public $getAllMedicineCategory;
public $selectMedicineCategoryId = null;
public $quantity = [];
public $checkbox = false;
public function render()
{
$this->getAllMedicineCategory = MedicineCategory::where('status', MedicineCategory::ACTIVE)->get(['id', 'name_medicine']);
if ($this->selectMedicineCategoryId != '') {
$this->medicines = Medicine::where('status', Medicine::ACTIVE)->Where('medicine_category_id', $this->selectMedicineCategoryId)->get(['id', 'name', 'unit']);
} else {
$this->medicines = [];
}
return view('livewire.selected-old-medicine-category-table');
}
}
View Blade
<div>
<select name="medicine_category_id" id="" wire:model="selectMedicineCategoryId" class="form-select io-select2">
<option value="">Select Medicine Category</option>
#foreach ($getAllMedicineCategory as $row)
<option value="{{ $row->id }}"#if($prescription->medicines->containsStrict('id', $row->id)) selected="selected" #endif>
{{ $row->name_medicine }}</option>
#endforeach
</select>
#if (!empty($medicines))
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th></th>
#foreach (collect($columns) as $column)
<th>{{ $column }}</th>
#endforeach
</tr>
</thead>
<tbody>
#forelse ($medicines as $item)
<tr>
<td><input class="form-check-input" type="checkbox" name=""
wire:model="selectedMedicineArray" id="" value='{{ $item }}'>
<td>{{ $loop->iteration }}</td>
</td>
<td>{{ $item->name }}</td>
<td><input type="number" wire:model="quantity.{{ $item->id }}" id=""
min="1" value="1" class="form-control">
</td>
<td>{{ $item->unit }}</td>
</tr>
#empty
<tr>
<td>Not exists medicine</td>
</tr>
#endforelse
</tbody>
</table>
View
enter image description here
enter image description here
i want when i fix it, it will show up like that and check the checkbox for me
Related
Help, I work with laravel and spatie for roles and permissions.
I want to edit my permissions
with a checkbox list
from a window Attached code:
En el componente:
public $role;
public $rol_id, $name;
public $chek = [];
public function mount(Role $role)
{
$this->role = $role;
$this->data = $role->toArray();
$this->chek = $this->role->permissions()->pluck('id')->toArray();
}
protected $rules =[
'role.name' => 'required'
];
public function render()
{
$permisos = Permission::all();
return view('roles.edit-rol-component', compact('permisos'));
}
public function update(){
$role = $this->role;
$this->validate([
'role.name' => 'required',
]);
$this->role->save();
$role->syncPermissions(['permission'=> $this->chek]);
$this->emit('alert','success','El rol se editó correctamente.');
$this->closeModalWithEvents([
RolComponent::getName() => 'renderEvent']);
}
In the view, the list generator shows me but it marks the checks wrong
#foreach($permisos as $key => $value)
<label class="inline-flex items-center">
<input wire:model.defer="chek.{{$value->id}}"
type="checkbox" value="{{ $value->id }}"
class="form-checkbox h-4 w-4 text-green-500"
#if($role->permissions->contains($value->id)) checked #endif >
<span class="ml-3 text-sm">{{ $value->name }} </span>
</label>
#endforeach
enter image description here
I am doing the edit / delete this way (as shown in the image)
Blade
<table class="table card-table table-vcenter text-nowrap table-hover table-striped datatable fs-5">
<thead>
<tr>
<th>#</th>
<th></th>
<th>Role</th>
<th></th>
<th>Permissions</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach ($roles as $id => $single)
#php
$badgeColor = ['badge bg-blue-lt',
'badge bg-azure-lt',
'badge bg-purple-lt',
'badge bg-pink-lt',
'badge bg-indigo-lt',
'badge bg-red-lt',
'badge bg-orange-lt',
'badge bg-cyan-lt',
'badge bg-yellow-lt',
'badge bg-green-lt'
];
#endphp
<tr>
<td>{{ $id + 1 }}.</td>
<td></td>
<td>{!! strtoupper($single->label) !!}</td>
<td></td>
<td>
<div class="btn-list">
#foreach ($single->permissions as $permission)
<span class="{{ $badgeColor[rand(0, 9)] }} fs-5">{{ $permission->label }} <span class="ps-1" style="cursor: pointer" wire:click='revokePermission({{ $single->id }}, {{ $permission->id }})'>x</span></span>
#endforeach
</div>
</td>
<td class="text-end">
<button class="btn btn-danger p-1 fs-6"><i class="fas fa-trash-alt"></i></button>
</td>
</tr>
#endforeach
</tbody>
</table>
Component:
public function revokePermission($roleId, $permissionId)
{
$role = Role::findOrFail($roleId);
$permission = ModelsPermission::findOrFail($permissionId);
$role->revokePermissionTo($permission->name);
$this->render();
}
I have the following code, you can go through Class Livewire and View Livewire, I got the desired result but I want to add 1 key to the half to collect, the key I want is quantity, when entering the quantity it will add to collect for me
Class Livewire
<?php
namespace App\Http\Livewire;
use App\Models\Medicine;
use App\Models\MedicineCategory;
use Livewire\Component;
class SelectMedicineCategoryTable extends Component
{
public $columns = ['STT', 'Name', 'Qty', 'Unit'];
public $medicines;
public array $selectedMedicineArray = [];
public $getAllMedicineCategory;
public $selectMedicineCategoryId = null;
public $quantity = [];
public function render()
{
$this->getAllMedicineCategory = MedicineCategory::where('status', MedicineCategory::ACTIVE)->get(['id', 'name_medicine']);
if ($this->selectMedicineCategoryId != '') {
$this->medicines = Medicine::where('status', Medicine::ACTIVE)->Where('medicine_category_id', $this->selectMedicineCategoryId)->get(['id', 'name', 'unit']);
} else {
$this->medicines = [];
}
return view('livewire.select-medicine-category-table');
}
}
View livewire
#forelse ($medicines as $item)
<tr>
<td><input class="form-check-input" type="checkbox" wire:model="selectedMedicineArray"
id="" value="{{ $item }}">
<td>{{ $loop->iteration }}</td>
</td>
<td>{{ $item->name }}</td>
<td><input type="number" wire:model="quantity.{{ $item->id }}" min="1"
id="" value="1">
</td>
<td>{{ $item->unit }}</td>
</tr>
#empty
<tr>
<td>Not found result</td>
</tr>
#endforelse
Output current
array ( 0 => '{"id":1,"name":"Padamon","unit":"V\\u0129"}', )
I want Output
array ( 0 => '{"id":1,"name":"Padamon","unit":"V\\u0129", 'quantity:...'}', )
In my Laravel-5.8 project, I have this codes:
Model:
class HrEmployee extends Model
{
protected $table = 'hr_employees';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'department_id',
'first_name',
'last_name',
];
public function department()
{
return $this->belongsTo('App\Models\Hr\HrDepartment','department_id','id');
}
}
deptfilters in the controller filters the hr_employees table using department_id
Controller:
public function index(Request $request)
{
$employeedatas = HrEmployee::where('status', 1)->get();
$deptfilters = HrDepartment::where('status', 1)->pluck('department_name','id');
return view('hr.employees.index')
->with('deptfilters', $deptfilters)
->with('employeedatas', $employeedatas);
}
view
<div class="container">
<br>
<form action="ViewPages" method="POST" enctype="multipart/form-data">
#csrf
<div class="container">
<div class="row">
<label for="from" class="col-form-label">Department</label>
<div class="col-md-2">
<select id="department" name="department" class="form-control">
<option value="">--- Select department ---</option>
#foreach ($department as $key => $value)
<option value="{{ $key }}" {{ old( 'department')==$ key ? 'selected' : ''}}>{{ $value }}</option>
#endforeach
</select>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-primary btn-sm" name="search">Search</button>
</div>
</div>
</div>
</form>
<br>
<table class="table table-dark">
<tr>
<th>id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
</tr>
#foreach ($employeedatas as $employeedata)
<tr>
<td>{{ $employeedata->id }}</td>
<td>{{ $employeedata->first_name }}</td>
<td>{{ $employeedata->last_name }}</td>
<td>{{ $employeedata->department->department_name }}</td>
</tr>
#endforeach
</table>
</div>
From the code each department has many employees. What I want to achieve is that:
By default, the dropdown should load department where current_year = 1
When the search button is submitted, I want the table to be fields with records based on the department_id in the dropdown.
How do I modify my controller and view code to achieve this?
Thanks
In general you don't need for $table and $id property when you are using Laravel conventions
class HrEmployee extends Model
{
//protected $table = 'hr_employees';//Laravel by default expect table name 'hr_employees'
//protected $primaryKey = 'id';//Laravel by default expect primary_key on table is id column
protected $fillable = [
'id',
'department_id',
'first_name',
'last_name',
];
public function department()
{
//return $this->belongsTo('App\Models\Hr\HrDepartment','department_id','id');
return $this->belongsTo('App\Models\Hr\HrDepartment');//No Need for override foreign key and owner key when you are using laravel conventions
}
}
In Controller
public function index(Request $request)
{
$employees = HrEmployee::where('status', 1);
if($request->input('department_id')){
$employees->where('department_id',$request->input('department_id'))
}
$employees=$employees->get();
$departments = HrDepartment::where('current_year',1)->where('status', 1)->pluck('department_name','id');
return view('hr.employees.index')
->with('departments', $departments)
->with('employees', $employees);
}
In View For filter use GET NOT POST METHOD
<div class="container">
<br>
<form method="get">
<div class="container">
<div class="row">
<label for="from" class="col-form-label">Department</label>
<div class="col-md-2">
<select id="department" name="department_id" class="form-control">
<option value="">--- Select department ---</option>
#foreach ($departments as $id => $name)
<option value="{{ $id }}" {{ request('depratment_id')==$id ? 'selected' : ''}}>{{ $name }}</option>
#endforeach
</select>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-primary btn-sm" name="search">Search</button>
</div>
</div>
</div>
</form>
<br>
<table class="table table-dark">
<tr>
<th>id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
</tr>
#foreach ($employees as $employee)
<tr>
<td>{{ $employee->id }}</td>
<td>{{ $employee->first_name }}</td>
<td>{{ $employee->last_name }}</td>
<td>{{ optional($employee->department)->department_name }}</td>
</tr>
#endforeach
</table>
</div>
You have pass the data using Ajax to the controller based on your filter values you should return the results of the filter without refreshing the page
Refer this
https://www.webslesson.info/2019/07/implement-custom-search-filter-in-laravel-58-datatable-using-ajax.html?m=1
public function index(Request $request)
{
//Build initial query to fetch employee data
$employeeQuery = HrEmployee::query()->where('status', 1);
//If a department has been selected by user
//filter the employee data by the selected department_id
if(!empty($request->department)) {
$employeeQuery->where('department_id', $request->department);
}
//Get the employee data by executing the $employeeQuery
$employeedatas = $employeeQuery->get();
//Get all the departments where current_year = 1
//if current_year is not a column on the departments table
//replace where('current_year', 1) with whereYear('created_at',now()->format('Y'))
//replace 'created_at' with whichever date column you want to compare with current year
$deptfilters = HrDepartment::where('status', 1)
->where('current_year', 1)
->pluck('department_name','id');
return view('hr.employees.index')
->with('deptfilters', $deptfilters)
->with('employeedatas', $employeedatas);
}
I'm trying to get the user role with permissions and compare them with all permissions that are in the database, if they match then a checkbox should be checked... I'm not sure if I'm doing it correctly
RoleController :
public function edit($id)
{
$role =Role::where('id', $id)->with('permissions')->first();
$permissions = Permission::all();
return view('admin.roles.edit', compact('role', 'permissions'));
}
edit.blade.php :
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">TYPE</th>
<th scope="col">CREATE</th>
<th scope="col">VIEW</th>
<th scope="col">UPDATE</th>
<th scope="col">DELETE</th>
</tr>
</thead>
<tbody>
#foreach($permissions as $permission)
<tr>
<td>
<div class="custom-control custom-checkbox ml-3">
<input type="checkbox" class="custom-control-input" id="{{ $permission->name }}" value="{{ ($permission->name) }}" {{ ($permission->name == "$role->permissions->name") ? "checked" : "" }}>
<label class="custom-control-label" for="{{ $permission->name }}">{{ $permission->display_name }}</label>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
I'm not getting any errors but I cant make the checkbox to be checked if they match... I'm using Laratrust 5.2
You can do it like this,
#foreach($permissions as $permission)
#php
$attribute = ($permission->id === $role->permissions->id)
? 'checked="checked"'
: '';
#endphp
<label for="permission-{{ $permission->id }}">
<input type="checkbox" {{ $attribute }} class="custom-control-input" id="permission-{{ $permission->id }}" value="{{ ($permission->id) }}">
<span>{{ ($permission->name) }}</span>
</label>
#endforeach
$permissions and $role->permissions should share the id field with same integer value where applicable.
I've added some extra html as it's considered as a good practice and changed your id & value attributes so it becomes more precise & standard.
I can assign Role to User within a team and can get team list attached to that user also can get list of Roles to same user, but when I put together to get the Role of each Team attached to that user in the Edit blade, the results get duplicated...
My UserController:
public function edit(User $user)
{
$teams = Team::all();
$roles = Role::all();
return view('admin.users.edit', compact('user','roles','teams'));
}
my edit blade:
<table id="multi-select" class="display">
<thead>
<tr>
<th>
<label>
<input type="checkbox" class="select-all" />
<span></span>
</label>
</th>
<th>Customer/Team</th>
<th>Role</th>
<th>Description</th>
</tr>
</thead>
<tbody>
#foreach($user->rolesTeams as $team)
<tr>
<td>
<label>
<input type="checkbox" />
<span></span>
</label>
</td>
<td> {{ $team->display_name }}</td>
#foreach($user->roles as $role)
<td> {{ $role->display_name }}</td>
<td> {{ $role->description }}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
Edit User
from Data-base
Sql
expected : Team Id 2 to display Role Id1
Team Id 3 to display Role Id 2
any help will be appreciated..
I figured it out by using for instead of foreach inside foreach loop,
#for ($i=0; $i<count ($user->rolesTeams) ; $i++)
<tr>
<td>
<label>
<input type="checkbox" />
<span></span>
</label>
</td>
<td> {{ $user->rolesTeams[$i]->display_name }}</td>
<td> {{ $user->roles[$i]->display_name }}</td>
<td> {{ $user->roles[$i]->description }}</td>
</tr>
#endfor