How to check Null values in mysql - php

I am using laravel framework to develop web application ,i am using soft-deletes so while fetching the data from database i have to check weather the column is null or not ,for that i wrote a following query
QueryBuilder
$today= "2022-09-23 00:00:00";
$this->repository->pushCriteria(new WhereCriteria('date_of_leaving', $today));
querylog
array (
'query' => 'select * from `employees` where `date_of_leaving` = ? and `employees`.`deleted_at` is null',
'bindings' =>
array (
0 => '2022-09-23 00:00:00',
),
'time' => 2.36,
),
table structure
+-----------------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-----------------+------+-----+---------+----------------+
| id | bigint unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
| joining_date | timestamp | NO | | NULL | |
| manager_id | bigint unsigned | NO | MUL | NULL | |
| image_path | varchar(255) | YES | | NULL | |
| date_of_leaving | timestamp | YES | | NULL | |
| still_working | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+-----------------+-----------------+------+-----+---------+----------------+
data
+---------------------+---------------------+
| date_of_leaving | deleted_at |
+---------------------+---------------------+
| 2022-09-23 00:00:00 | 2022-09-23 11:47:11 |
| 2022-09-23 00:00:00 | 2022-09-23 12:36:46 |
| 2022-09-23 00:00:00 | 2022-09-23 13:09:55 |
| NULL | NULL |
| 2022-09-06 00:00:00 | NULL |
| NULL | NULL |
| NULL | NULL |
| NULL | NULL |
| NULL | 2022-09-23 11:45:01 |
| NULL | NULL |
| NULL | NULL |
+---------------------+---------------------+
Actually in database three matching records are there with the above condition but this query is not fetching the data ,i was suspecting deteled_at was considering NULL as string

Try This.
array (
'query' => 'select * from employees where date_of_leaving Is Null and employees.deleted_at is null',
'bindings' =>
array (
0 => '2022-09-23 00:00:00',
),
'time' => 2.36,
),

Try doing this, i believe the package you are using does not support Laravel's softDelete() feature.
$this->repository->pushCriteria(new WhereCriteria(['date_of_leaving',=> $today,'deleted_at'=>Null]));

Related

get users that did not buy a product

I have three tables
users table
+--------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+--------------+------+-----+---------+----------------+
| Id | bigint | NO | PRI | NULL | auto_increment |
| AccountNumber | varchar(50) | YES | | NULL | |
| DisplayName | varchar(255) | YES | MUL | NULL | |
| ParentAccountId | bigint | YES | MUL | NULL | |
| AccountTypeRef | bigint | YES | MUL | NULL | |
| PhoneNo | varchar(255) | YES | | NULL | |
| FaxNo | varchar(50) | YES | | NULL | |
| Website | varchar(200) | YES | | NULL | |
| Email | varchar(100) | YES | | NULL | |
| StateRef | bigint | YES | MUL | NULL | |
| Address | longtext | YES | | NULL | |
| PostalCode | varchar(50) | YES | | NULL |
products table
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| Id | bigint | NO | PRI | NULL | auto_increment |
| ProductId | bigint | YES | MUL | NULL | |
| PackName | varchar(255) | YES | | NULL | | |
| Price | double | YES | | NULL | |
| OnlineInfoAddress | varchar(255) | YES | | NULL | |
| Description | longtext | YES | | NULL | |
| IsDelete | tinyint | YES | | NULL | |
| CreateUserId | bigint | YES | | NULL | |
| CreateDate | datetime | YES | | NULL | |
| EditDate | datetime | YES | | NULL | |
| EditUserId | bigint | YES | | NULL | |
| Tag | int | YES | | NULL | |
| Ref | varchar(50) | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
users_buy table
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| Id | bigint | NO | PRI | NULL | auto_increment |
| users_id | bigint | YES | MUL | NULL | |
| product_id | bigint | YES | MUL | NULL | |
| BuyDate | datetime | YES | | NULL | |
| CountBuy | int | YES | | NULL | |
| TotalPrice | double | YES | | NULL | |
| Description | longtext | YES | | NULL | |
| invoiceNumber | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
I want to get users who did not buy a specific product
for example, user number 20 bought 5 product with ids [1,2,3,4,5]
and user number 19 bought 3 product with ids [1,2,3]
And user number 18 bought 2 product with ids [1,3]
I want to get users who did not buy any product with the id 5
ie I want user 18 and 19
How can I do this?
This will list of all users that did not buy product id 5:
SELECT *
FROM users
WHERE
Id NOT IN (
SELECT users_id
FROM users_buy
WHERE product_id = 5
)
One more solution used LEFT JOIN:
SELECT users.*
FROM users
LEFT JOIN users_buy
ON users_buy.users_id = users.id AND users_buy.product_id = 5
WHERE users_buy.user_id IS NULL;
The above query will return records from users table have not matched rows in users_buy table with product_id 5

Yii 1.1: Fetching HAS_MANY between two models

Our platform is build using Yii 1.1. We have users with staff role, and they have permissions granted to them.
client_staff
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| code | varchar(20) | YES | | NULL | |
| clientId | int(11) unsigned | NO | MUL | NULL | |
| emailAddress | text | NO | | NULL | |
| firstName | varchar(45) | YES | | NULL | |
| lastName | varchar(45) | YES | | NULL | |
| archivedAt | datetime | YES | | NULL | |
| createdAt | datetime | YES | | NULL | |
| updatedAt | datetime | YES | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
staff_permissions
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| code | varchar(20) | YES | UNI | NULL | |
| staffId | int(11) unsigned | NO | MUL | NULL | |
| permissionId | int(11) unsigned | NO | MUL | NULL | |
| createdAt | datetime | YES | | NULL | |
| updatedAt | datetime | YES | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
permissions
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| code | varchar(20) | YES | UNI | NULL | |
| description | varchar(100) | NO | | NULL | |
| createdAt | datetime | YES | | NULL | |
| updatedAt | datetime | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
A client_staff has many staff_permissions, which belongs to permission.
Using the framework, I wanna to fetch the staff_permissions along with the client_staff, so this is how I tried configured the relations in ClientStaff:
return [
'client' => [self::BELONGS_TO, User::class, 'clientId'],
'staffPermission' => [self::HAS_MANY, StaffPermission::class, 'id'],
]
in StaffPermission:
return [
'staff' => [self::BELONGS_TO, ClientStaff::class, 'staffId'],
'permissions' => [self::HAS_MANY, Permission::class, 'permissionId'],
]
and in Permission:
return [
'staffPermissions' => [self::BELONGS_TO, StaffPermission::class, 'id'],
]
We have a toAPICopy method to bring the data to the client in JSON, and in this method, for classes BELONGS_TO, we make use of each classes' toAPICopy within the returned array. Below's the case of ClientStaff
public function toAPICopy()
{
return [
'id' => $this->id,
'client' => $this->client->toAPICopy(),
...,
'staffPermission' => $this->staffPermission->toAPICopy()
];
}
But nothing happens and the application hangs. No data is fetched and no error is shown, not a single thing in api.log file to help me. I do not know what I could've done wrong.
I think you are having some issue in making relations.
these are the relations as per your table structure.
ClientStaff should be:
return [
'client' => [self::BELONGS_TO, User::class, 'clientId'],
'staffPermission' => [self::HAS_MANY, StaffPermission::class, 'staffId'],
]
in StaffPermission should be:
return [
'staff' => [self::BELONGS_TO, ClientStaff::class, 'staffId'],
'permissions' => [self::BELONGS_TO, Permission::class, 'permissionId'],
]
and in Permission should be:
return [
'staffPermissions' => [self::BELONGS_TO, StaffPermission::class, 'permissionId'],
]
So, HAS_MANY will return an array of objects, therefore, you cannot call a method directly on it. The solution that worked for me is:
public function toAPICopy() {
foreach ($this->staffPermissions as $p) {
array_push($permissions, $p->toAPICopy();
}
return [ ... 'staffPermissions' => $permissions, ... ];
}

why i am getting an error of incorrect integer value on age? while i am using the correct data type and values in it?

insert into `patient` ( `pat_f_name`, `pat_l_name`, `pat_gender`, `pat_age`, `pat_dob`, `pat_blood_grp`, `pat_weight`, `pat_contact_no`, `pat_address` )
-> VALUES
-> ('$user_fname','$user_lname','$user_gender','$user_age','$user_dob','$user_bg','$user_weight','$user_contact_no','$user_address');
ERROR 1366 (HY000): Incorrect integer value: '$user_age' for column 'pat_age' at row 1
the table looks like
+----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+----------------+
| pat_id | int(50) | NO | PRI | NULL | auto_increment |
| dr_id | int(50) | NO | | NULL | |
| pat_f_name | char(20) | NO | | NULL | |
| pat_l_name | char(20) | NO | | NULL | |
| pat_gender | char(20) | NO | | NULL | |
| pat_age | int(20) | NO | | NULL | |
| pat_dob | date | NO | | NULL | |
| pat_blood_grp | varchar(10) | NO | | NULL | |
| pat_weight | int(10) | NO | | NULL | |
| pat_contact_no | varchar(12) | NO | | NULL | |
| pat_address | varchar(20) | NO | | NULL | |
+----------------+-------------+------+-----+---------+----------------+
$sql = "INSERT INTO patient(pat_f_name, pat_l_name, pat_gender, pat_age, pat_dob, pat_blood_grp, pat_weight, pat_contact_no, pat_address) VALUES ('$user_fname','$user_lname','$user_gender','$user_age','$user_dob','$user_bg','$user_weight','$user_contact_no','$user_address')";

Laravel eloquent many to many returns empty

So i'm trying to get my head around using eloquent for many to many relationships in my application.
I have three tables as followed
user
+----------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| first_name | varchar(255) | NO | | NULL | |
| last_name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | UNI | NULL | |
| password | varchar(60) | NO | | NULL | |
| remember_token | varchar(100) | YES | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| active | enum('yes','no') | NO | | NULL | |
| last_login | timestamp | NO | | 0000-00-00 00:00:00 | |
+----------------+------------------+------+-----+---------------------+----------------+
user_has_address
+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| address_id | int(10) unsigned | YES | MUL | NULL | |
| users_id | int(10) unsigned | YES | MUL | NULL | |
+------------+------------------+------+-----+---------+-------+
address
+---------------+----------------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name_number | varchar(45) | NO | | NULL | |
| first_line | varchar(45) | NO | | NULL | |
| second_line | varchar(45) | NO | | NULL | |
| town_city | varchar(45) | NO | | NULL | |
| state_country | varchar(45) | NO | | NULL | |
| post_zip | varchar(45) | NO | | NULL | |
| type | enum('delivery','billing') | NO | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| deleted_at | timestamp | YES | | NULL | |
+---------------+----------------------------+------+-----+---------------------+----------------+
in my user repository i have the following
namespace App\Libraries\Repositories\Core\Users;
use Schema;
use App\Models\Core\User;
use Bosnadev\Repositories\Eloquent\Repository;
use Symfony\Component\HttpKernel\Exception\HttpException;
class UserRepository extends Repository
{
public function getUsersAddresses()
{
return $this->userModel->hasManyThrough('App\Models\Bundle\Addresses\Address','App\Models\Bundle\Addresses\UserHasAdress','id','address_id');
}
}
Im returned an object that shows parent and related classes but im not actually returned my users address. Is there something im missing?
Alexrussell made some good points in his comment that you could possibly address, however I believe your immediate problem is a missing ->get() at the end of your return line.
Without it, you would be required to call your method like:
$repository->getUsersAddresses()->get();
As hasManyThrough will return an instance of Illuminate/Database/Eloquent/Relations/HasManyThrough not the actual results
For reference:
https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Relations/HasManyThrough.html
Note in the example here: https://laravel.com/docs/5.1/eloquent-relationships#many-to-many
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
The example usage includes a get(): $roles = App\User::find(1)->roles()->orderBy('name')->get();
Hope this helps

Laravel Validation Unique -- Ignore condition

INITIAL NOTE: I AM WILL AWARE OF HOW TO IGNORE ROWS BY PRIMARY KEY! THAT IS NOT THE ISSUE AT HAND!
First, let me give you the structure of my database table...
+-------------+------------+------+-----+---------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | text | NO | | NULL | |
| description | text | NO | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | on update CURRENT_TIMESTAMP |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| created_by | int(11) | NO | | NULL | |
| public | tinyint(1) | NO | | 1 | |
+-------------+------------+------+-----+---------------------+-----------------------------+
I want it so that if I am storing a new topic(row) in the database, it will only check rows where public = 1 so if a post is "deleted"(or public = 0), it will not count in the unique verification.
This is how I currently handle NEW posts:
'title' => 'required|unique:topics',
This is how I currently handle UPDATING posts:
'title' => 'required|unique:topics,title,'.Request::input('id'),
Any help is appricatied. Thank you!
Based on the Validation documentation at Laravel:
unique:table,column,except,idColumn
Also, from: http://brianretterer.com/quick-tip-laravel-unique-validation/
'title' => 'required|unique:articles,title,NULL,NULL,public,0'

Categories