I have an HQL query from my repository and I want to get information from my class where condition class type = string. The error is field is not present.
View:
<div layout:fragment="content">
<div class="col-sm-12">
<div class="panel panel-primary">
<div class="panel-heading">Mes demandes</div>
<div class="panel-body">
<form th:action="#{/demande}" method="post">
<div>
<label>Type de la demandes:</label>
<select name="field">
<option th:value="Demande_absence">Demande d'absence</option>
<option th:value="Demande_conge">Demande de congé</option>
<option th:value="Demande_document">Demande de documtents</option>
<option th:value="Demande_pret">Demande de prêt</option>
</select>
<button type="submit" class="btn btn-primary">Chercher</button>
</div>
</form>
<form th:action="#{/AddDemande}" method="post">
<button type="submit" class="btn btn-primary">Ajouter une demande</button>
</form>
<div class="container-fluid spacer50">
<h3 class="text-danger">Liste des demandes</h3>
<table class="table">
<thead>
<tr>
<th>Code</th><th>date</th><th>TYPE_DEM</th>
</tr>
</thead>
<tbody>
<tr th:each="p:${listdemandes}">
<td th:text="${p.codeDemande}"></td>
<td th:text="${p.dateDemande}"></td>
<td th:text="${p.class.simpleName}"></td>
<td><a th:href="#{/afficher(id=${p.codeDemande})}">Informations sur la demande</a></td>
</tr>
</tbody>
</table>
</div>
<div class="text-danger" th:if="${exception}" th:text="${exception.message}">
</div>
<div class="container col-sm-8">
<ul class="nav nav-pills">
<li th:class="${pageCourante}==${status.index}?'active':''" th:each="pa,status:${pages}" >
<a th:href="#{demande(page=${status.index},size=${size})}" th:text="${status.index}"></a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
Controller :
#RequestMapping(value="/demande")
public String dem(Model model,
#RequestParam(name="page",defaultValue="0")int p,
#RequestParam(name="size",defaultValue="4")int s,
#RequestParam(name="field",defaultValue="4")String type) {
String pr_emp;
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Long matricule =Long.parseLong(auth.getName());
pr_emp=employeRepository.prenom(matricule);
Page<Demande> PageDemande=demandeRepository.listdemande(matricule,"valide",type,new PageRequest(p,s));
int[] pages = new int[PageDemande.getTotalPages()];
model.addAttribute("pages", pages);
model.addAttribute("size", s);
model.addAttribute("pagecourante", p);
model.addAttribute("prenom", pr_emp);
model.addAttribute("matricule", matricule);
model.addAttribute("listdemandes",PageDemande.getContent());
return "demandes";
}
Repository Query:
#Query("select d from Demande d where d.employe.matricule=:x and d.valide=:isvalide and d.class=:type order by dateDemande")
public Page<Demande> listdemande(#Param("x") Long matricule,
#Param("isvalide") String isvalide,
#Param("type") String type,
Pageable pageable);
Related
When I trigger foreach to show the products registered in each process, separated by line and column, it returns the result all in just one line.
Sorry about my English
Example:
ID
header 2
1525, 1523
Item 1, Item 2
what i want to show
ID
Name
1525
Item 1
1523
Item 2
Back End:
`
public function viewLicitacao($id) {
$licitacao = Licitacao::find($id); $this->showModalLicitacao();
$this->licitacao_id_processo = $licitacao->id;
$this->licitacao_destino = $licitacao->cliente_nome;
$this->licitacao_date_entrega = $licitacao->date_entrega;
}
Front End:
#if($isModal == true)
#foreach($licitacao as $processo)
<div class="modal fade" id="ModalLicitacao">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Licitação - {{ $processo->id; }}</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h4>Informações das licitações</h4>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label>N° do Processo: </label>
<input class="form-control" type="number" wire:model="licitacao_id_processo" placeholder="Informe o N° do processo" readonly/>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="destino">Destinatário: </label>
<input type="text" class="form-control" wire:model="licitacao_destino" placeholder="Informe o nome do destinatário" readonly/>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Data da entrega:</label>
<input type="text" wire:model="licitacao_date_entrega" class="form-control" placeholder="Data de entrega da mercadoria" readonly>
</div>
</div>
</div>
<h4>Produtos da licitação</h4>
<div class="row">
<table class="table table-hover text-nowrap">
<thead>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Preço Unit</th>
<th>Quantidade</th>
<th>Qt. Disponivel</th>
<th>Ações</th>
</tr>
</thead>
<tbody>
#forelse($processo->products as $key => $products)
<td>{{ $products->id_product }}</td>
#empty
<td>Nenhum produto foi cadastrado nessa licitação</td>
#endforelse
</tbody>
</table>
</div>
</div>
<div class="modal-footer justify-content-between">
<button type="submit" wire:click="closeModalLicitacao()" class="btn btn-default">Fechar</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
#endforeach
<!-- /.modal -->
#endif
The two tables have relationships
Modal ProductLicitacao:
class ProductLicitacao extends Model
{
use HasFactory;
protected $fillable = [
'id_product', 'licitacao_id', 'name', 'qty', 'price_unit',
];
public function licitacao()
{
return $this->belongsTo(Licitacao::class);
}
}
Modal Licitacao:
class Licitacao extends Model
{
use HasFactory;
protected $fillable = [
'processo_number', 'cliente_nome', 'status', 'user_id', 'date_entrega', 'alert',
];
protected $dates = [
'date_entrega'
];
public function products()
{
return $this->hasMany(ProductLicitacao::class, 'licitacao_id');
}
}
Help me to solve this problem that seems to be simples, but it is being monstrous for my iniation in web development.
Try adding the tag <tr> before the <td>
<tbody>
#forelse($processo->products as $key => $products)
<tr>// <-- here
<td>{{ $products->id_product }}</td>
//other columns(td), you may have
</tr>// <-- and here
#empty
<td>Nenhum produto foi cadastrado nessa licitação</td>
#endforelse
</tbody>
Good day everyone. I'm quite new to new to Laravel and was doing some basic CRUD coding, I was able to code the add and view function, but I'm having a hard time with the edit and delete function.
I have 4 files, the master blade file, the web.php (routing), the blade file and the Form Controller.
This is the promo.blade.php file:
<table class="table table-striped" id="table1" >
<thead>
<tr>
<th class="text-center">ACTION</th>
<th class="text-center">Offer ID</th>
<th class="text-center">Promo Name</th>
<th class="text-center">Promo Price</th>
<th class="text-center">Status</th>
</tr>
</thead>
<tbody>
#foreach ($data as $key => $item)
<tr>
<td class="text-center">
<a href="#" data-bs-toggle="modal" data-bs-target="#show" data-myofferid="{{$item->offerId}}" data-mytitle="{{$item->promoName}}" data-myprice="{{$item->promoPrice}}">
<span class="badge bg-success"><i class="bi bi-eye-fill"></i></span>
</a>
<a href="#" data-bs-toggle="modal" data-bs-target="#edit" data-mainid="{{$item->id}}" data-myofferid="{{$item->offerId}}" data-mytitle="{{$item->promoName}}" data-myprice="{{$item->promoPrice}}">
<span class="badge bg-primary"><i class="bi bi-pencil-square"></i></span>
</a>
<a href="#" data-bs-toggle="modal" data-bs-target="#delete" data-myofferid="{{$item->offerId}}" data-mytitle="{{$item->promoName}}" data-myprice="{{$item->promoPrice}}">
<span class="badge bg-danger"><i class="bi bi-trash"></i></span>
</a>
</td>
<td class="date text-center">{{ $item->offerId }}</td>
<td class="date text-center">{{ $item->promoName }}</td>
<td class="number text-center">{{ $item->promoPrice}}</td>
<td class="number text-center">{{ $item->isActive }}</td>
</tr>
#endforeach
</tbody>
</table>
<!--Start Modal Edit -->
<div class="modal fade text-left" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel160" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable" role="document">
<div class="modal-content">
<div class="modal-header bg-primary">
<h5 class="modal-title white" id="add">
Edit Promo
</h5>
<button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
<i data-feather="x"></i>
</button>
</div>
<div class="modal-body">
<form action="{{ route('promo.edit') }}" method="POST">
#csrf
<div class="form-group">
<label for="">ID</label>
<input type="text" class="form-control" name="id" id="id" value="">
<span style="color:red">#error('id'){{$message}} #enderror</span>
</div>
<div class="form-group">
<label for="">Offer ID</label>
<input type="text" class="form-control" name="offerId" id="offerId" value="">
<span style="color:red">#error('offerId'){{$message}} #enderror</span>
</div>
<div class="form-group">
<label for="">Promo Name</label>
<input type="text" class="form-control" name="promoName" id="promoName" value="">
<span style="color:red">#error('promoName'){{$message}} #enderror</span>
</div>
<div class="form-group">
<label for="">Promo Price</label>
<input type="number" class="form-control" name="promoPrice" id="promoPrice" value="">
<span style="color:red">#error('promoPrice'){{$message}} #enderror</span>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light-secondary" data-bs-dismiss="modal">
<i class="bx bx-x d-block d-sm-none"></i>
<span class="d-none d-sm-block">CANCEL</span>
</button>
<button type="submit" class="btn btn-primary ml-1">
<i class="bx bx-check d-block d-sm-none"></i>
<span class="d-none d-sm-block">SAVE</span>
</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- End Modal Edit-->
Then this is the web.php file, for the routing:
Route::get('promo.promo', [App\Http\Controllers\FormControllerPromo::class, 'viewRecord'])->middleware('auth')->name('promo.promo');
Route::post('promo.add', [App\Http\Controllers\FormControllerPromo::class, 'addPromo'])->name('promo.add');
Route::post('promo.delete/{id}', [App\Http\Controllers\FormControllerPromo::class, 'viewDelete'])->middleware('auth');
Route::get('promo.edit', [App\Http\Controllers\FormControllerPromo::class, 'viewRecord'])->middleware('auth')->name('promo.edit');
Route::get('promo.edit/{id}', [App\Http\Controllers\FormControllerPromo::class, 'viewDetail'])->middleware('auth');
Route::post('promo.edit', [App\Http\Controllers\FormControllerPromo::class, 'edit'])->name('promo.edit');
This is the master.blade.php file:
<!--Start Modal edit for Promo-->
<script type="text/javascript">
$('#edit').on('show.bs.modal', function (event){
var button = $(event.relatedTarget)
var mainid = button.data('mainid')
var id = button.data('myofferid')
var title = button.data('mytitle')
var price = button.data('myprice')
var modal = $(this)
modal.find('.modal-body #id').val(mainid);
modal.find('.modal-body #offerId').val(id);
modal.find('.modal-body #promoName').val(title);
modal.find('.modal-body #promoPrice').val(price);
})
</script>
<!--End Modal edit for Promo-->
I think this is the part where the code wont execute properly.
This is the FormControllerPromo.php file:
// view form
public function index()
{
return view('promo.promo');
}
// view record
public function viewRecord()
{
$data = DB::table('promo')->get();
return view('promo.promo',compact('data'));
}
// view detail
public function viewDetail($id)
{
$data = DB::table('promo')->where('id',$id)->get();
return view('promo.promo',compact('data'));
}
// edit promo
public function edit(Request $request){
$id = $request->input('id');
$offerId = $request->input('offerId');
$promoName = $request->input('promoName');
$promoPrice = $request->input('promoPrice');
DB::table('promo')
->where('id', $id) // find your user by their email
->limit(1) // optional - to ensure only one record is updated.
->update(array('offerId' => $offerId, 'promoName' => $promoName, 'promoPrice' => $promoPrice)); // update the record in the DB.
$data = DB::table('promo');
return view('promo.promo',compact('data'));
}
I've been trying to code this for almost a week now with no success, any help is highly appreciated. :)
the update seems right, should work. but when you pass the $data variable to your view, you should call ->get(), because otherwise you return a query builder instance, that later raises the Undefined property error when trying to access {{$item->offerId}}.
change second last line in your example
//from this:
$data = DB::table('promo');
//to this:
$data = DB::table('promo')->get();
//or to this if you want to show only one record:
$data = DB::table('promo')->where('id', $id)->get();
I am using livewire components but when begin typing in the search input field, the checkboxes and icons in the component disappear, and they only reappear after refreshing the page. What could be the cause of this behaviour?
Blade view
<div>
<div class="row">
<div class="col-md-12 grid-margin stretch-card">
<div class="card">
<div class="card-header">
<div class="d-flex justify-content-between align-items-center flex-wrap grid-margin">
<div>
<h4 class="mb-3 mb-md-0">User Roles</h4>
</div>
<div class="d-flex align-items-center flex-wrap text-nowrap">
<div class="form-inline">
<div class="input-group mr-2 mb-2 mb-md-0 d-md-non d-xl-flex">
<input type="text" wire:model="search" class="form-control" placeholder="Search role...">
</div>
<div class="input-group mr-2 mb-2 mb-md-0 mt-3 d-md-non d-xl-flex">
<select wire:model="sortAsc" class="form-control form-control-s mb-3">
<option value="1">Ascending</option>
<option value="0">Descending</option>
</select>
</div>
<div class="input-group mr-2 mb-2 mb-md-0 mt-3 d-md-non d-xl-flex">
<select wire:model="perPage" class="form-control form-control-s mb-3">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
</div>
</div>
<a href="{{ route('create-role') }}" class="btn btn-success btn-icon-text mr-2 mb-2 mb-md-0">
<i class="btn-icon-prepend" data-feather="plus"></i>
Add Role
</a>
<button wire:click="deleteRoles" type="button" class="btn btn-danger btn-icon-text mb-2 mb-md-0">
<i class="btn-icon-prepend" data-feather="trash-2"></i>
Delete Role
</button>
</div>
</div>
</div>
<div class="card-body">
#if (count($roles) > 0)
<div class="table-responsive">
<table id="dataTableExample" class="table">
<thead>
<tr>
<th></th>
<th>#</th>
<th>Display Name</th>
<th>Description</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach ($roles as $role)
<tr wire:key="{{ $role->name }}">
<td>
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" wire:model="selectedRoles.{{ $role->id }}" class="form-check-input">
</label>
</div>
</td>
<td>{{ $role->id }}</td>
<td>{{ $role->display_name }}</td>
<td>{{ $role->description }}</td>
<td>{{ $role->created_at->diffForHumans() }}</td>
<td>
<a href="{{ route('edit-role', $role->name) }}" class="btn btn-primary btn-icon-text mr-2 mb-2 mb-md-0">
<i class="btn-icon-prepend" data-feather="edit-2"></i>
Edit
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
<div>{{ $roles->links() }}</div>
</div>
#else
<p>No user roles found.</p>
#endif
</div>
</div>
</div>
</div>
</div>
Corresponding livewire component
<?php
namespace App\Http\Livewire\Roles;
use Livewire\Component;
use App\Models\Role;
use Livewire\WithPagination;
class Index extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $selectedRoles = [];
public $search = '';
public $perPage = 10;
public $sortField = 'id';
public $sortAsc = true;
public function render()
{
return view('livewire.roles.index', [
'roles' => Role::search($this->search)
->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')
->simplePaginate($this->perPage)
])
->extends('layout.master');
}
public function createRole(){
return view('livewire.roles.create')
->extends('layout.master');
}
public function deleteRoles(){
Role::destroy($this->selectedRoles);
}
}
What could be causing this issue?
try changing this lines, actually I use this like livewire documentation
'roles' => Role::where('someColumn','like','%'.$this->search.'%')
orWhere('anotherColumn','like','%'.$this->searchTerm.'%')
->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')
->paginate($this->perPage)
I tried fetching the data after selecting an accountnumber using ajax it did work but after selecting once I can't select again. I've been trying some solutions but it didn't work.
AccountController.php
public function index()
{
$AM= new AM();
$AM->setConnection('AM');
$key= '';
$getData = $AM->where('Accountnumber',Auth::user()->AccountNum)->get();
foreach($getData as $row){
$key= $row->key;
}
$getAMData = $AM->where('key',$key)
->select('Accountnumber')
->get();
$ci= $AM->where('Accountnumber',Auth::user()->AccountNum)
->select('Accountnumber', 'cn', 'ca','ct','accountstatus')
->get();
return view('account')->with('getAMData',$getAMData)->with('ci',$ci);
}
// Methods
public function fetch(Request $request){
if($request->ajax())
{
$AM= new AM();
$AM->setConnection('AM');
$getData = $AM->where('Accountnumber',$request->Accountnumber)->get();
foreach($getData as $row){
$key= $row->key;
}
$getAMData = $AM->where('key',$key)
->select('Accountnumber')
->get();
$ci= $AccountMaster->where('Accountnumber',$request->Accountnumber)
->select('Accountnumber', 'cn', 'ca','ct','accountstatus')
->get();
return view('partials._fetch')->with('getAMData',$getAMData)->with('ci',$ci);
}
}
ajax.js
$(document).ready(function(){
$('#accountnum').change(function(){
var Accountnumber = $(this).val();
$.ajax({
url:"/fetch",
method: "GET",
data: {Accountnumber:Accountnumber},
success:function(data){
$('#showbill').html(data);
}
});
});
});
account.blade.php
<div class="container-fluid col-sm-12">
<h3><b> Account </b></h3>
</div>
</div>
<div class="container-fluid py-4 col-sm-12" id="showbill">
#include('partials._fetch')
</div>
I think my problem would be on my controller, maybe the fetch method is not right. anyone can help me with this? Thank you!
_fetch.blade.php
<div class="row">
<div class="col-md-3 col-sm-12">
#if(count($getAMData) > 0)
<select class="form-control form-control-md" id="accountnum" name="accountnum">
<option value="" selected></option>
#foreach($getAMData as $row)
<option value="{{ $row->Accountnumber }}">{{ $row->Accountnumber }}</option>
#endforeach
</select>
#else
<p></p>
#endif
</div>
<div class="col-md-12 col-sm-12 py-4">
<div class="card">
<div class="card-body">
#if(count($ci) > 0 )
#foreach ($ci as $row )
<div class="col-md-6 col-sm-12">
<label>Account Number:</label> <label><strong>{{ $row->Accountnumber }}</strong></label>
</div>
<div class="col-md-6 col-sm-12">
<label>Name:</label> <label><strong>{{ $row->cn}}</strong></label>
</div>
<div class="col-md-6 col-sm-12">
<label> Address:</label> <label><strong>{{ $row->ca}}</strong></label>
</div>
<div class="col-md-6 col-sm-12">
<label>Type:</label> <label><strong>{{ $row->ct}}</strong></label>
</div>
<div class="col-md-6 col-sm-12">
<label>Account Status:</label> <label><strong>{{ $row->ct}}</strong></label>
</div>
#endforeach
#else
<p>NO DATA FOUND</p>
#endif
</div>
</div>
</div>
<div class="col-md-12 col-sm-12">
<table class="table table-responsive-md table-hover ">
<thead style="text-align: center;" class="bg-warning ">
<tr>
<th scope="col">(MW)</th>
<th scope="col">Morning (0001H to 1200H)</th>
<th scope="col">Afternoon (1201H to 1800)</th>
<th scope="col">Evening (1801 to 2400)</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"></th>
</tr>
</tbody>
</table>
</div>
</div>
I tried using the same concept with the paginate using ajax well it didn't turn out to be what I expected haha
Okay so here is my solution to this problem. Instead of including the Select Option I put it inside my account.blade.php so that it wont regenerate two select options.
I have a piece of HTML where I need to multiply (or perform arithmetic operations) two values using thymeleaf.
Let's say my price is double and quantity is an integer.
Here's my sample code:
<fieldset class="form-inline" disabled >
<label for= "prodAmount">AMOUNT: </label>
<input type="number" class="form-control" th:text="${#aggregates.sum(product.![price * qty])}">
</fieldset>
Here's the error I'm getting:
Exception evaluating SpringEL expression: "#aggregates.sum(product.![price * qty])"
Also , I would also like to ask how to perform this when there's two or more values.
Here's my html form
<div class="container">
<div class="row">
<div class="col-lg-4 col-sm-12 col-xs-12">
<form th:action="#{/product/product-list}" method="post" th:object="${productEntity}">
<input type="hidden" th:field="*{pid}" th:value="${pid}" >
<fieldset class="form-group">
<label for="product_name">Product Name</label>
<input type="text" class="form-control" th:field="*{pName}" th:value="${pName}">
</fieldset>
<fieldset class="form-group">
<label for="price">Price</label>
<input type="number" class="form-control" th:field="*{price}" th:value="${price}" placeholder="0">
</fieldset>
<fieldset class="form-group">
<label for="pQty">Quantity</label>
<input type="number" class="form-control" th:field="*{qty}" th:value="${qty}" placeholder = "0">
</fieldset>
<fieldset class="form-group">
<label for="status">Status</label>
<select class="form-control" id="select_status" th:field="*{status}" th:value="${status}">
<option value="0">AVAILABLE</option>
<option value ="1">NOT AVAILABLE</option>
</select>
</fieldset>
<fieldset class="form-group">
<label for="image">Image</label>
<input id="input-b2" name="input-b2" type="file" class="file"
data-show-preview="false" th:field="*{image}" th:value="${image}">
</fieldset>
</form>
</div>
<div class="col-lg-8 col-sm-12 col-xs-12">
<table class="table table-inverse table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Status</th>
<th>Image</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr th:each="product : ${productTable} ">
<td th:text="${product.pid}"></td>
<td th:text="${product.pName}"></td>
<td th:text="${product.price}"></td>
<td th:text="${product.qty}"></td>
<td th:text="${product.status == '0'} ? 'Available' : 'Not Available'"></td>
<td th:text="${product.image}"></td>
<td>
<a th:href="#{/productlist/{id}(id = ${product.pid})}" class="btn btn-primary">Update</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="row float-right">
<div class="col-xd-12">
<div>
<fieldset class="form-inline" disabled >
<label for= "prodAmount">AMOUNT: </label>
<input type="number" class="form-control" th:text="${#aggregates.sum(product.{price * qty})}">
</fieldset>
<button type="button" class="btn btn-primary">Checkout</button>
</div>
</div>
</div>
</div>
Product Controller:
#RequestMapping(value="/product-list")
public ModelAndView shopView() {
ModelAndView mav = new ModelAndView();
mav.addObject("productTable", pRepo.findAll());
mav.addObject("productEntity",new ProductEntity());
mav.setViewName("/productlist");
return mav;
}
#RequestMapping(value = "/{id}",method = RequestMethod.GET)
public ModelAndView getProduct(#ModelAttribute ProductEntity productEntity, #PathVariable int id) {
ModelAndView mav = new ModelAndView();
mav.addObject("productTable",pRepo.findAll());
mav.addObject("productEntity", pRepo.findOne(id));
mav.setViewName("/productlist");
return mav;
}