I have HR dashboard display all forms (The applicants form)
I want to display it order by pending forms
I wrote this code but it doesn't work
public function index(){
$pending = PersonalInfo::doesntHave('hraction')->first();
$infos = PersonalInfo::orderBy($pending, 'desc')->latest()->simplePaginate();
$rank = $infos->firstItem();
return view('HR/HrEmployee',["infos",$infos,"rank"=>$rank]);
}
And this error appeared
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.jpg","personlPhoto":"resources\/images\/$2y$10$XoxGRUQiFBZgroZfj0R5ef8LnSro...' at line 1 (SQL: select * from personal_infos` order by ... limit 16 offset 0)
To be clear for you i have tow tables (personalInfos & Hractions)
prsonalInfos:
| id | firstname | fathername | ...
Hractions:
| id | user_id | action | comment | personal_id | created_at |updated_at
your variable $pending is a whole collection, you can not pass it as is, you need a column name there instead.
$infos = PersonalInfo::orderBy('firstname', 'desc')->latest()->simplePaginate();
firstname is just an example, put the appropriate field of the table you want to order there.
So basically you don't need to use the same model 2 times, you can simply do:
$infos = PersonalInfo::doesntHave('hraction')->orderBy('firstname', 'desc')->latest()->simplePaginate();
This will return you all the data from PersonalInfo model without hraction relationship and use ordering + pagination on them in one step.
orderBy()'s first argument needs to be a field in the database.
If you want to order them from latest to most recent you can use either the id field, or created_at.
Something like this:
$infos = PersonalInfo::orderBy('id', 'desc')->latest()->simplePaginate();
$infos = PersonalInfo::orderBy('created_at', 'desc')->latest()->simplePaginate();
in the end the Eloquent model queries are being transformed into plain SQL. So if you had that in mind, it will be much simpler to build the eloquent queries.
Related
I'd like to query from the reviewed_by table below where the "company" is "AAA" and "review" is "Need Review"
Here's mysql table :
+-----------+
| DATA_TYPE |
+-----------+
| text |
+-----------+
+-------------------------+
| reviewed_by |
+-------------------------+
|[{"company":"AAA","review":"OK","reviewed_at":"2021-01-26 08:59:26"}]|
|[{"company":"BBB","review":"OK","reviewed_at":"2021-01-26 08:59:26"}]|
|[{"company":"AAA","review":"Need Review","reviewed_at":"N\/A"}]|
+-------------------------+
Here's the #1 query i've tried :
SELECT * FROM `t_transaction`
WHERE `reviewed_by`
LIKE '%`"company":"AAA","review":"Need Review"`%'
Here's the #2 query i've tried :
SELECT * FROM `t_transaction`
WHERE `reviewed_by`
LIKE '%"company":"AAA","review":"Need Review"%'
ci3 query :
$like = ['reviewed_by','"company":"AAA","review":"Need Review"'];
$this->db->select('*')
->from('t_transacion')
->group_by('id')
->like($like[0],$like[1]);
The result i've got from those 2 queries was nothing,
How can i do this type of query (and also if using codeigniter 3) ?
MySql has some functions that allow you to do search over a json field. See documentation.
The reviewed_by column is a json array and you want to seach the first element of that array. Using the function JSON_EXTRACT you can extract data from the json field. In your case to get the json in the first position in the array so we execute JSON_EXTRACT(reviewed_by, '$[0]') which will return {"company":"...","review":"..","reviewed_at":"..."}. From the returned json we can call again the JSON_EXTRACT function to get a value given a key. If we select JSON_EXTRACT(JSON_EXTRACT(reviewed_by, '$[0]'), "$.company") this will return the company value from inside the json.
There are different ways to select what you want. I will give you two option and they have pros and cons. Take a look at this stackoverflow.
First approach using the where clause:
SELECT reviewed_by
FROM t_transaction
WHERE JSON_EXTRACT(JSON_EXTRACT(reviewed_by, '$[0]'), "$.company") = "AAA"
AND JSON_EXTRACT(JSON_EXTRACT(reviewed_by, '$[0]'), "$.review") = "Need Review";
Second approach using the having clause:
SELECT JSON_EXTRACT(reviewed_by, '$[0]') AS json
FROM t_transaction
HAVING json -> "$.company" = "AAA"
AND json -> "$.review" = "Need Review";
I'm trying to get the sum of two different columns using Laravel query builder, the plain SQL Query below works just fine, but I can't get it to work with the Laravel Query.
SELECT SUM(logins_sun + logins_mon) FROM users_stats WHERE id = 7; // returns: 1034
Here's what I have tried.
$stats = DB::table('users_stats')->where('id', '=', '7')->sum('logins_sun', '+', 'logins_mon'); // returns: 587.0
And here is my DB structure.
+----+------------+------------+
| id | logins_sun | logins_mon |
+----+------------+------------+
| 7 | 587 | 447 |
+----+------------+------------+
It was supposed to return 1034 but the Laravel Query is returning only the last value 587.0 .
How can I get it working?
You can try with the sum() method like:
DB::table('users_stats')
->where('id', '7')
->sum(\DB::raw('logins_sun + logins_mon'));
sum is an aggregate function and only takes one argument. It will sum the values of each row in a column. In your case, the query only returns one row, so the sum is just the value of that one column (the first argument passed to sum()). There may be some better way to do it, but I think you should be able to use a raw expression to return the sum of the two columns.
$stats = DB::table('users_stats')
->select(DB::raw('logins_sun + logins_mon'))
->where('id', '=', '7');
Try passing a callback to the sum() and do the addition there like:
$stats = DB::table('users_stats')->where('id', '=', '7')->sum(function ($row) {
return $row->logins_sun + $row->logins_mon;
});
You can run direct raw sql in laravel with the following way :
$sql = "SELECT SUM(logins_sun + logins_mon) FROM users_stats WHERE id = :ID";
$result = DB::select($sql,['ID'=>7]);
My Problem: Getting query results from 2 db tables with PROPEL2 even when second table has no corresponding entries. If the second has corresponding entries than it is no problem.
I have 3 tables: Entry, Contingent and Favorit.
The schema is as follow:
Entry.id [PK]
Entry.contingent_id [FK]
Entry.expert_id
Contingent.id [PK]
Contingent.name
Favorit.id [PK]
Favorit.contingent_id [FK]
Favorit.expert_id
Favorit.pos
I want to get for a specified expert_id ($id) all entries from Entry with contingent-name and if exists the favorit.pos for this expert and contingent. I get the wanted with:
$result = EntryQuery::create()
->filterByExpertId($id)
->join('Entry.Contingent')
->withColumn('Contingent.name','_contingentName')
->join('Contingent.Favorit')
->where('Favorit.expert_id = ?', $id)
->find();
This works only if there exists such a favorit.pos . In some cases this element doesn’t exists (what is wanted from the system). In these cases I want to get the result too just with favorit.pos as empty, null or 0. But Propel doesn’t return me these records.
With MySQL I have no problem to get the desired result:
SELECT entry.* ,
(SELECT favorit.position
FROM contingent, favorit
WHERE
favorit.expert_id = entry.expert_id
AND entry.contingent_id = contingent.id
AND contingent.id = favorit.contingent_id
)
FROM `entry`
JOIN contingent
ON entry.contingent_id = contingent.id
WHERE
entry.expert_id=1;
Use Join left in code:
->join('Contingent.Favorit','selection conditon','left' )
This left work when empty database when condition is false
in condition like 'id'=$id
I've tried to query using laravel eloquent where user is following a specific brand.
The problem: How to query listing of brand and let me know if current login user is following the brand?
got 3 tables:
Brand:
Id | name | logo
User:
Id | name | email | password
Followers:
brand_id | user_id
Now i tried to query all brand and inside of the collection i want to add
is_follow = 1 or is_follow = 0
if the user already follow or 0 if not exists.
I'm using fractal so maybe it can be easier to query. but i don't really get it how to query it out with check the user_id first.
Thanks
*Update
I manage to solve it. But i think its a bad practice.
$user_id = Request::get('user_id');
foreach($brands->followers as $value){
$array[] = $value->user_id;
}
if(in_array($user_id, $array)){
$is_follow = 1;
}
You can check if the authenticated User follows a specific Brand with:
$user = Auth::user();
$exists = $user->brands->contains($brand_id);
You can also do it with a raw query which will be better in terms of performance:
$exists = DB::table('user_brand')
->whereBrandId($brand_id)
->whereUserId(Auth::user()->id)
->count() > 0;
I am trying to display records of a particular job that has already been done by someone else before a new provider sees it. If the status is open, there should not be any information to be displayed as supposedly, no one has made any report about it. If the status is awarded, then necessary data should be displayed. Right now, the information to be shown are viewable. The problem the data is displayed in every job post even it is not the report for such a job.
Example,
Job ID | Title | Description | Subject | Job Status
2 | Math Tutor | I need Math tutor! I need Math tuto... | Mathematics | Open
1 | English Tutor | Edited... | French | Awarded
If I click "Open", I should not be able to see any record because it is still not done. If I click "Awarded", I should see details about the job. Right now, the data is showing properly for JOB ID 1 which was already awarded. However, the same data is shown as well in JOB ID 2.
How do I properly display the data in its proper place? I've been trying everything to do it. I included the JOB ID to be displayed to see if there's something wrong with it. But there's none, it shows JOB ID 1 in both jobs 1 and 2. How do I display it just in job 1 where it belongs?
Here's my code in controller:
public function view_tutors_tutorials()
{
$this->validateRole('provider');
$this->load->model('tutorial_model');
$this->load->model('auth_model');
$data['subject_list'] = $this->array_to_select( $this->tutorial_model->get_all_subjects(), 'id','name');
$my_preference = $this->tutorial_model->get_tutors_tutorials(isset($_GET['subject_id'])?$_GET['subject_id']:'0', isset($_GET['sort_by'])?$_GET['sort_by']:'');
$data['my_preference'] = $my_preference;
$this->load->view('provider/view_tutors_tutorials', $data);
}
and this in my model:
public function get_tutors_tutorials($subject_id = NULL, $sort_by = NULL)
{
//responsible for displaying job contracts for provider user.
$this->db->select('tutorial.status as status, tutorial.client_id as client_id, tutorial.id as tutorial_id, subject.name as name, tutorial.title as title, tutorial.description as description, tutorial.start_date as start_date, tutorial.update_date_time as update_date_time,tutorial_proposal.provider_id as provider_id,provider.first_name as first_name,provider.last_name as last_name,tutorial.contract_status as contract_status,tutorial.provider_feedback as provider_feedback,tutorial.client_notetoself as client_notetoself,tutorial.client_feedback as client_feedback,tutorial.provider_notetoself as provider_notetoself,tutorial.material_used,tutorial.recommendation')->from('tutorial');
$this->db->join('subject', 'subject.id = tutorial.subject_id');
$this->db->join('tutorial_proposal', 'tutorial_proposal.provider_id = tutorial.provider_id');
$this->db->join('provider', 'provider.id = tutorial_proposal.provider_id');
$this->db->where('tutorial.status', 'Awarded');
if ( ! empty($subject_id) )
{
$this->db->where('subject_id', $subject_id);
}
//if there's no sort selection made, the jobs will be sorted from newest to oldest
if ( empty($sort_by))
{
$sort_by = "update_date_time desc";
}
$this->db->order_by($sort_by);
$query = $this->db->get();
return $query->result_array();
}
I look forward to getting any help.
You need to remove where condition for status in you model's query :
$this->db->where('tutorial.status', 'Awarded'); // this should be removed
Also make sure, your subject_id passed properly.