I have a query where I show all the products that are in that warehouse, but if it has more than one it shows me the x quantity but with the same data, thats because I have ->first(); but If I remove ->first(); it says an error whit the traceability.
$Input = $request->key;
$almacen = $request->almacen;
$name = DB::table('products')
->leftJoin('inventories', 'inventories.product_id', '=', 'products.id')
->whereRaw("(products.reference LIKE '%$Input%' OR products.name LIKE '%$Input%') AND products.deleted_at is null")
->leftJoin('warehouses', 'inventories.warehouse_id', '=', 'warehouses.id')
->where('warehouses.id', $almacen)
->whereNull('products.deleted_at')
->whereNull('inventories.deleted_at')
->select(
'products.reference',
'products.name',
'products.sku',
'products.id',
'inventories.lot',
'inventories.expirationDate',
'inventories.traceability',
'inventories.warehouse_id'
)
->get();
$array = [];
foreach ($name as $key) {
//$key->traceability
array_push($array, $key->reference);
}
//$array = array_unique($array);
$html = '';
if ($name != '[]') {
foreach ($array as $value) {
$prodName = DB::table('products')
->leftJoin('inventories', 'inventories.product_id', '=', 'products.id')
->where('products.reference', $value)
->whereNull('products.deleted_at')
->whereNull('inventories.deleted_at')
->select(
'products.reference',
'products.name',
'products.sku',
'products.id',
'inventories.lot',
'inventories.expirationDate',
'inventories.traceability'
)
->first();
//return $value;
$html .= '<div><a style="color: #000000" class="suggest-element" traceability="'.$prodName->traceability.'" reference="' . $value . '" sku="' . $prodName->sku . '" name="' . $prodName->name . '" lot="' . $prodName->lot . '" expirationDate="' . $prodName->expirationDate . '" data="' . ($value) . " " . ($prodName->name) . '" id="' . $prodName->id . '">' . ($value) . " " . ($prodName->name) ." " . ($prodName->lot) ." " . ($prodName->expirationDate) ." " . ($prodName->traceability) .'</a></div>';
}
} else {
$html .= '<div><a style="color: #000000" class="suggest-element" exist="0" data="Sin coincidencias." id="0">Sin coincidencias.</a></div>';
}
return $html;
});`
I would like to see the products whit the correct data,
Your code is currently only returning the first row in the database because you are using first().
If you remove first(), you will just be left with a Query Builder and will get an error like "Undefined property: Illuminate\Database\Query\Builder::$traceability" - because you haven't got the data yet.
You can use get() instead of first() to get all of the rows, and this will return a Collection.
You can also use ->whereIn('products.reference', $array) instead of looping through all of the elements in $array
You are also comparing $name to the string '[]' instead of an empty array. You can use empty() instead: if (!empty($name)) {
And then you can simply loop through the Collection and add the html:
// Use empty() - This checks if an array is empty or not
if (!empty($name)) {
$products = DB::table('products')
->leftJoin('inventories', 'inventories.product_id', '=', 'products.id')
// Use whereIn - This checks if any of the values in $array match products.reference
->whereIn('products.reference', $array)
->whereNull('products.deleted_at')
->whereNull('inventories.deleted_at')
->select(
'products.reference',
'products.name',
'products.sku',
'products.id',
'inventories.lot',
'inventories.expirationDate',
'inventories.traceability'
)
// Return all rows
->get();
foreach ($products as $product) {
$html .= '<div><a style="color: #000000" class="suggest-element" traceability="' . $product->traceability . '" reference="' . $product->reference . '" sku="' . $product->sku . '" name="' . $product->name . '" lot="' . $product->lot . '" expirationDate="' . $product->->expirationDate . '" data="' . ($product->reference) . " " . ($product->->name) . '" id="' . $product->->id . '">' . ($product->reference) . " " . ($product->name) . " " . ($product->lot) . " " . ($product->expirationDate) . " " . ($product->traceability) . '</a></div>';
}
} else {
Related
I have created a multiform search in laravel & it's not working properly
I have a table name candidates & there is a row named salary in which it has value like shown the the below image from 1 lac(s) to 39 lac(s)
The meaning it's not working properly is if i search min salary = 0 & max salary = 4 it's showing data between 0 to 4 but it's also showing data like 10 18 22
controller code
public function advance(Request $request)
{
$data = \DB::table('candidates');
if( $request->name){
$data = $data->where('name', 'LIKE', "%" . $request->name . "%");
}
if( $request->location){
$data = $data->where('location', 'LIKE', "%" . $request->location . "%");
}
if( $request->key_skills){
$data = $data->where('key_skills', 'LIKE', "%" . $request->key_skills . "%");
}
if( $request->gender){
$data = $data->where('gender', 'LIKE', "%" . $request->gender . "%");
}
if( $request->pref_loc){
$data = $data->where('pref_loc', 'LIKE', "%" . $request->pref_loc . "%");
}
if( $request->phoneno){
$data = $data->where('phoneno', 'LIKE', "%" . $request->phoneno . "%");
}
if( $request->email){
$data = $data->where('email', 'LIKE', "%" . $request->email . "%");
}
$min_ctc = $request->min_ctc;
$max_ctc = $request->max_ctc;
if ($min_ctc || $max_ctc) {
$data = $data->where('salary', '>=', $min_ctc);
$data = $data->where('salary','<=',$max_ctc);
}
$data = $data->paginate(10);
$data2 = $data->total();
return view('search', compact('data2'))->with('data',$data);
}
Thanks in advance
You are trying to search on string not on integer. In order to get the required result you have to type cast the string to integer or float and then perform the operation.
Here is the script that might help you.
if ($min_ctc || $max_ctc) {
$data = $data->whereRaw("CONVERT(SUBSTRING_INDEX(salary,' ', 1),UNSIGNED INTEGER) >= {$min_ctc}");
$data = $data->whereRaw("CONVERT(SUBSTRING_INDEX(salary,' ', 1),UNSIGNED INTEGER) <= {$max_ctc}");
}
Hi there I have this problem on Laravel, I don't know how to explain it but I'll give my best, so forgive me if I am not to clear.
I have this on my controller for uploading images:
public function setUniqueNameAttribute(){
$this->unique_name = md5(Auth::user()->id . time());
}
public function getUniqueNameAttribute(){
return $this->unique_name;
}
public function uploadImage($request, $element, $id) {
if ($request->hasFile('question_image') || $request->hasFile('image_path') || $request->hasFile('avatar') || $request->hasFile('contact_image')) {
$thumbnailsRules = require_once __DIR__ . '/ThumbnailRules.php';
$this->setUniqueNameAttribute();
if($element == 'questions'){
$ImageToUpload = $request->file('question_image');
}elseif($element == 'answers'){
$ImageToUpload = $request->file('image_path');
}elseif($element == 'users'){
$ImageToUpload = $request->file('avatar');
}elseif($element == 'contacts'){
$ImageToUpload = $request->file('contact_image');
}
//create folders if they do not exist
foreach ($thumbnailsRules as $key => $rule) {
if (File::exists(storage_path('/app/public/institutions/' . $this->institution_storage . '/images/' . $element . '/' . $id . '/' . $rule['name'] . '/')) == false) {
Storage::disk('public')->makeDirectory('/institutions/' . $this->institution_storage . '/images/' . $element . '/' . $id . '/' . $rule['name']);
Storage::disk('public')->makeDirectory('/institutions/' . $this->institution_storage . '/images/' . $element . '/' . $id . '/original/');
}
}
//save the photos to the server
$image = Image::make($ImageToUpload);
$image->save(storage_path('/app/public/institutions/' . $this->institution_storage . '/images/' . $element . '/' . $id . '/original/' . $this->getUniqueNameAttribute() . '.jpeg'));
foreach ($thumbnailsRules as $key => $rule) {
$image = Image::make($ImageToUpload)->resize($rule['width'], $rule['height'], function ($c) {
$c->aspectRatio();
$c->upsize();
});
$image->save(storage_path('/app/public/institutions/' . $this->institution_storage . '/images/' . $element . '/' . $id . '/' . $rule['name'] . '/' . $this->getUniqueNameAttribute() . '.jpeg'));
}
}
}
This works fine for the question_image, image_path and avatar because this I use after I am logged in so I can use this:
$this->setUniqueNameAttribute();
But for contact image I have to use this before logged in so I don't need to use this:
$this->setUniqueNameAttribute();
This gives me this error:
Trying to get property 'id' of non-object
Is there a way to pass through this without getting an error..?
I am using an export function in my controller inorder to export the details of 4000+ members in doc and pdf format. At first the error shown as memory exhausted. So I wrote ini_set('memory_limit', '-1'); at the beginning of my function. Now the document works fine, but still export to pdf not working. The below is my function I wrote in laravel 5.2 controller.
protected function export() {
ini_set('memory_limit', '-1');
$members = MembersModel::select();
$sortBy = Request::get('sort');
switch ($sortBy) {
case 'name':
$members->orderBy('name');
break;
case 'member_num':
$members->orderBy('member_num');
break;
case 'address':
$members->orderBy('address');
break;
case 'road':
$members->orderBy('road');
break;
case 'pincode':
$members->orderBy('pincode');
break;
case 'city':
$members->orderBy('city');
break;
default:
# code...
break;
}
$filter = Request::get('filter');
if($filter != '') {
$members->where('name', 'like', '%' . $filter . '%')->orWhere('member_num', 'like', '%' . $filter . '%')->orWhere('address', 'like', '%' . $filter . '%')->orWhere('address2', 'like', '%' . $filter . '%')->orWhere('road', 'like', '%' . $filter . '%')->orWhere('city', 'like', '%' . $filter . '%')->orWhere('pincode', 'like', '%' . $filter . '%')->orWhere('blood_group', 'like', '%' . $filter . '%')->orWhere('land_num', 'like', '%' . $filter . '%')->orWhere('mob_num', 'like', '%' . $filter . '%')->orWhere('occupation', 'like', '%' . $filter . '%');
}
$members = $members->get();
$data = array('members' => $members);
$type = Request::get('type');
switch ($type) {
case 'doc':
$view = View::make('allmembers.exportdoc', $data);
$contents = $view->render();
// or
$contents = (string) $view;
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=allmembers.doc");
echo $contents;
break;
case 'pdf':
$pdf = PDF::loadView('allmembers.exportdoc', $data);
return $pdf->download('allmembers.pdf');
break;
default:
# code...
break;
}
}
What might be the issue. Thanks in advance.
I want to disable the anchor tag of two subcategories. I used a free extension for topmenu. The code for subcategory menu is:
public function drawMenuItem($children, $level = 1)
{
$html = '<div class="itemMenu level' . $level . '">';
$keyCurrent = $this->getCurrentCategory()->getId();
foreach ($children as $child) {
if (is_object($child) && $child->getIsActive()) {
// --- class for active category ---
$active = '';
if ($this->isCategoryActive($child)) {
$active = ' actParent';
if ($child->getId() == $keyCurrent) $active = ' act';
}
// --- format category name ---
$name = $this->escapeHtml($child->getName());
if (Mage::getStoreConfig('custom_menu/general/non_breaking_space')) $name = str_replace(' ', ' ', $name);
$html.= '<a class="itemMenuName level' . $level . $active . '" href="' . $this->getCategoryUrl($child) . '"><span>' . $name . '</span></a>';
$activeChildren = $this->_getActiveChildren($child, $level);
if (count($activeChildren) > 0) {
$html.= '<div class="itemSubMenu level' . $level . '">';
$html.= $this->drawMenuItem($activeChildren, $level + 1);
$html.= '</div>';
}
}
}
$html.= '</div>';
return $html;
}
I tried to disable the two subcategories. But it didn't work. How can I give proper condition to disable particular two subcategories link?
I am facing a strange error. It is related to updating the php session.
I have a site whose login/logout works perfectly in chrome,safari and IE etc. But in firefox it is not working. When I login using firefox, the logout button seems to be dead or something. It just doesnt let me logout. Even if I manually type the logout link, it still doesnt do anything and keeps me logged in. I am not doing anything complex at all.
Any ideas why the same code works in chrome, IE and safari but not in firefox?
Let me know,
Thanks.
============================LOGOUT CODE===============================
public function logout() {
$this->session->unset_userdata('smallpoint_username');
$this->session->sess_destroy();
redirect(base_url() . 'forum/update_session.php?hasher = ' . time() . time() . time() . time() . time() . time() . '&&nohasher=' . time() . time() . time() . time() . time() . time() . '&&op=2', 'location', 301);
}
================SESSION SETTING CODE in LOGIN===============================
$session_array = array(
'smallpoint_username' => $insertData['username'],
'smallpoint_full_name' => $insertData['full_name'],
'smallpoint_user_type' => $insertData['user_type'],
'smallpoint_user_id' => $userData['id'],
'smallpoint_user_snap' => '',
'is_logged_in' => 1,
);
$this->session->set_userdata($session_array);
redirect(base_url() . 'forum/update_session.php?hasher = ' . time() . time() . time() . time() . time() . time() . '&&nohasher=' . time() . time() . time() . time() . time() . time() . '&&thisisid=' . $userData['id'] . '&&thisisrole=' . $insertData['role'] . '&&thisisname=' . $insertData['username'] . '&&op=1', 'location', 301);
=====================update_session.php======================
session_start();
require 'includes.php';
if ($_GET['op'] == 1) {
$_SESSION[TABLES_PREFIX . 'sforum_logged_in'] = true;
$_SESSION[TABLES_PREFIX . 'sforum_user_id'] = $_GET['thisisid'];
$_SESSION[TABLES_PREFIX . 'sforum_user_role'] = $_GET['thisisrole'];
$_SESSION[TABLES_PREFIX . 'sforum_user_username'] = $_GET['thisisname'];
} else {
unset($_SESSION[TABLES_PREFIX . 'sforum_logged_in']);
unset($_SESSION[TABLES_PREFIX . 'sforum_user_id']);
unset($_SESSION[TABLES_PREFIX . 'sforum_user_role']);
unset($_SESSION[TABLES_PREFIX . 'sforum_user_username']);
setcookie(TABLES_PREFIX . COOKIE_NAME, "", time() - 3600);
}
header('Location: ' . $_SERVER['HTTP_REFERER']);
Try to add the following to your code.
session_save_path("/tmp");
session_start();
I got the same problem in firefox and it works fine
session_save_path("/tmp"); // CREATE this Folder in the root
session_start();
require 'includes.php';
if ($_GET['op'] == 1) {
$_SESSION[TABLES_PREFIX . 'sforum_logged_in'] = true;
$_SESSION[TABLES_PREFIX . 'sforum_user_id'] = $_GET['thisisid'];
$_SESSION[TABLES_PREFIX . 'sforum_user_role'] = $_GET['thisisrole'];
$_SESSION[TABLES_PREFIX . 'sforum_user_username'] = $_GET['thisisname'];
} else {
unset($_SESSION[TABLES_PREFIX . 'sforum_logged_in']);
unset($_SESSION[TABLES_PREFIX . 'sforum_user_id']);
unset($_SESSION[TABLES_PREFIX . 'sforum_user_role']);
unset($_SESSION[TABLES_PREFIX . 'sforum_user_username']);
setcookie(TABLES_PREFIX . COOKIE_NAME, "", time() - 3600);
}
header('Location: ' . $_SERVER['HTTP_REFERER']);