Laravel 7: How to match two tables column? - laravel

Please see my code first,
Measurement Table:
id
customer_id
service_id
1
1
2
2
1
3
2
2
Controller:
$customer = Customer::findOrFail($id);
$ex = Measurement::where('customer_id', $id)->pluck('service_id');
if(count($ex) == 0){
$service = Service::where('status', 0)->get();
}
else{
$service = Service::where('id', '!=', $ex)->where('status', 0)->get();
}
In Measurement table, if a customer doesn't have any service_id then $service = Service::where('status', 0)->get(); this will pass in view file.
If a customer have a service_id, then in view file all services will pass except that service_id.
Can anyone suggest me a better way for this?

How about this
$customer = Customer::with('services')->where('customer_id', $id)->first();
$serviceRowCount = 0;
if ($customer->services->count() > 0) {
$services = $customer->services->whereNotNull('service_id');
$serviceRowCount = $services->count();
}
if ($serviceRowCount > 0) {
$services = Service::whereNotIn('id', $services->pluck('service_id'))->get();
} else {
$services = Service::where('status', 0)->get();
}
return view('test')->compact('services');
I am assuming that Customer has primary key as customer_id, has relation with Measurement model as services and service_id has default value as null.

Related

Get User Ranking based on number of posts (Codeigniter)

I have a number of users who are posting content to the POSTS Table.
id user_id content
1 1 text
2 3 text
3 1 text
4 1 text
5 2 text
6 3 text
Now, I would like to get a single user rank by highest posts(row). I'm confused about how to get the result!
Asuming that you are using the query builder, from the model, you can use the following statement to obtain the required result:
$this->db->select('user_id, count(content) as total_posts')
->group_by('user_id')
->order_by('total_posts', 'DESC')
->limit(1)
->get('POSTS')
->row();
The issue has been solved. Here is the explanation of how I've done this:
model
public function get_user_ranking()
{
$this->db->select('user_id, count(id) as total_posts');
$this->db->group_by('user_id');
$this->db->order_by('total_posts', 'DESC');
$query = $this->db->get('posts');
return $query->result();
}
controller
$data['user_ranking'] = $this->post_model->get_user_ranking();
view
$rank = 1; foreach($user_ranking as $row)
{
if( $row->user_id == $user->id)
{
echo $rank;
break;
}
$rank++;
}
public function get_user_ranking()
{
$this->db->select('user_id, count(id) as total_postings');
$this->db->from('posts');
$this->db->group_by('user_id');
$this->db->order_by('total_posts', 'DESC');
return $this->db->get()->result();
}

Not getting correct SUM amount for users votes from thread and reply table

I am using codeigniter 3.1.0 and I am trying to get the total SUM of two columns from different tables
If my user_id = 1 it should return the total sum of 421 but returns 431 It is adding the votes from other users on my reply table
Question Using Active Record How can I make sure the SUM is getting the correct amount for the users id.
Model Function
public function thread_reputation($user_id) {
$this->db->select('SUM(reply.votes + thread.votes) AS reputation');
$this->db->from('thread', 'left');
$this->db->join('reply', 'reply.user_id = thread.user_id', 'left');
$this->db->where('thread.user_id', $user_id);
$this->db->where('reply.user_id', $user_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row();
} else {
return 0;
}
}
Controller section where i echo it
$thread_reputation = $this->thread_analytics_model->thread_reputation('1');
echo $thread_reputation->reputation;
Thread Table
Reply Table
Change your query with this
$query = $this->db->query("SELECT SUM(A.votes) as reputation from
(
SELECT reply.votes FROM reply where reply.user_id = $user_id
UNION ALL
SELECT thread.votes FROM thread where thread.user_id = $user_id
)AS A");
Tested and properly working
Check Screenshot:-
Query using ACTIVE REOCRD
$this->db->select("SUM(A.votes) as reputation");
$this->db->from("(
SELECT reply.votes FROM reply where reply.user_id = $user_id
UNION ALL
SELECT thread.votes FROM thread where thread.user_id = $user_id) AS A");
$query = $this->db->get();
Another Solution using $this->db->get_compiled_select();
public function reputation($user_id) {
$this->db->select('thread.votes');
$this->db->from('thread');
$this->db->where('thread.user_id =' . $user_id);
$thread_query = $this->db->get_compiled_select();
$this->db->select('reply.votes');
$this->db->from('reply');
$this->db->where('reply.user_id ='. $user_id);
$reply_query = $this->db->get_compiled_select();
$query = $this->db->query("SELECT SUM(total.votes) as reputation from ($reply_query UNION ALL $thread_query) as total");
if ($query->num_rows() > 0) {
return $query->row();
} else {
return 0;
}
}
When you will use left join, it will return matched column & all other unmatched column from left table. Use inner join & check your result.

codeigniter LEFT JOIN array issue

Can someone tell me how to write this properly?
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$records = $this->db->where('us.group_id', '3');
$data=array();
foreach($records->result() as $row)
{
$data[$row->id] = $row->first_name;
}
return ($data);
}
I'm trying to populate a drop down menu using an array, but i need to only grab users that are part of users_group/group_id = 3
therefore in my very limited knowledge I'm needing:
select X from Users LEFT JOIN users_groups WHERE group_ID = 3
You need to call $this->db->get() in order to actually run your query.
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$this->db->where('us.group_id', '3');
$records = $this->db->get();
$data = array();
foreach($records->result() as $row){
$data[$row->id] = $row->first_name;
}
return $data;
}

get the last row in the db

What is the correct way to retrieve the last row, sorted desc by id, in CodeIgniter?
I've tried select_max but I want the whole row.
function getLastPagination(){
$data = array();
$this->db->query("SELECT id,sort FROM pagination GROUP BY id DESC LIMIT 1");
$query = $this->db->get('pagination');
$data = $query->row_array();
return $data;
}
$data['allpag'] = $this->Model_cats->getLastPagination();
$data['pagin'] = $data['allpag']['sort'];
$per_page = $data['pagin'];
Here I am getting the first value, but I want the last row.
$this->db->select('id')->order_by('id','desc')->limit(1)->get('table_name')->row('id');
Try this.............
function getLastPagination(){
$query ="select * from pagination order by id DESC limit 1";
$res = $this->db->query($query);
if($res->num_rows() > 0) {
return $res->result("array");
}
return array();
}
}
In controller function you need to do following things.......
$allpag = $this->Model_cats->getLastPagination();
$per_page = $allpag[0]['sort'];
$query = $this->db->query("SELECT * FROM pagination ORDER BY id DESC LIMIT 1")->row_array();
Should work?

Codeigniter JOIN dont back db result

Codeigniter dont back database result.
Database table "Category" and "Sub Category"
DB Shema:
Categor
-----------------------------
ID Name
----------------------------
1 Fishing
2 Hunting
3 Test Category
Sub_category
-----------------------------
ID cat_id name
----------------------------
1 1 Fishing rod
2 2 Hunting ammunition
3 3 Test sub category
I want list all sub category for some category. When some1 click on Fishing category i want show all sub category for fishing. My code is this:
Controller:
public function get_sub_category($id = 0)
{
$this->load->model('front_m');
$data['sub_cat'] = $this->front_m->show_sub_cat($id);
$this->template->set_theme('zend')->set_layout('front.html')
->build('sub_category',$data);
}
MODEL:
public function show_sub_cat($id=0)
{
$this->query = $this->db->select('*');
$this->query = $this->db->from('category');
$this->query = $this->db->where('id='.$id');
$this->query = $this->db->join('sub_category', 'sub_category.cat_id=category.id');
$this->query = $this->db->query('SELECT * FROM category');
$this->query = $this->db->get();
if ($this->query->num_rows() > 0) {
$this->query->result();
}
return $this->query ;
}
What is wron i all time have DB error or blanko page.
Based on your question it sounds like your over thinking it, but I'm a bit confused as well. What I'm hearing is: How can I get my sub categories based on someone clicking on a main category id? If that's the case then your making it way more complicated than it needs to be.
MODEL
public function show_sub_cat($catid=NULL)
{
$result = $this->db->get_where('sub_category',array('cat_id'=>$catid));
if ($result->num_rows()>0)
{
return $result->result();
}
}
$sql_query ="SELECT
c.name,
sc.*
FROM Category as c
LEFT JOIN Sub_category as sc ON sc.cat_id = c.ID
WHERE c.ID = $id";
return $this->db->query($sql_query)->result();

Resources