laravel get data when give id not match with join table id - php

here is my category table data
id
1
2
6
7
when in my post bale I join with this category table
here is my post table sample data
ID = 1,
name = 'hellow'
category id = 4 (i join with category table but selected category is
deleted)
here is my index SQL query (when categy_id match with the category.id) then only its fetch
$post = DB::table('posts)->join('category','posts.category_id','categories.id')-.paginate(10);
for some reason, the selected category can be deleted so I try to get category deleted post data
here is my query
$cpost = DB::table('posts')->join('categories','posts.category_id', '!=' ,'categories.id')->select('post.*')->paginate(5);
but above query duplicate post data based on available category data
i want all post data which are category id is not matched with in category table id how can i get that ?

Try this. Key is the leftJoin instead of default innerJoin (join).
// posts without assigned or existing category
$posts = \DB::table('posts')
->leftJoin('category','posts.category_id','categories.id')
->whereNull('categories.id')
->paginate(10);

why are you doing a join for this? you already have category id stored in your post table.
$cpost = DB::table('posts')->where('category_id','!=', $category_id)->paginate(5);

Just try it:
$cpost = DB::table('posts')
->join('categories','posts.category_id', '=' ,'categories.id')
->select('post.*', 'categories.*')
->whereNotIn('posts.category_id', DB::raw('select id from categories'))
->paginate(5);

Related

laravel orm : where condition on table -> related table -> related table

so here is my database for a book store
books : id ,title
category : id, title
book_category : id , book_id, category_id
book_stock : id , book_id , quantity , price
considering all the relations are defined in the model , i can query book_stock it goes something like this
Stock::with('Book')->get();
but what if i want to get stock of a book in the category = 1
i can use use condition on book
Stock::with('Book' , function($q){
$q->where(['title'=>'abc']);
})->get();
but how can i filter related table of book ?
basically i want to get book_id from book_category where category_id = 1 and then use those ids to filter my books finally get stock
ps : i don't want to use query builder
This will return you all books belonging to category=1 with their stock information:
$categoryId = 1;
$books = Book::with('stock')->whereHas('category', function($query) use ($categoryId) {
return $query->where('id', $categoryId);
})->get();
You can do:
Stock::with('Book.stock', 'Book.category')->get();
You can access any number of nested relations within a with statement.
Related question:
Laravel nested relationships
Armin Sam's answer should also be a viable option.

MySQL Stored functions and php

So i have this query to fetch all posts
select id, title
from posts
where type = 'article'
and i have a stored function that calculate the total of views and return the result
i know i can execute it like
select totalView(id)
but how can i merge the two queries to return all posts with the total view of each post
maybe something like
select id, title, totalView(id) as total
from posts
where type = 'article'
Thank you.
select count(*) as total, id, title from posts where type = 'article'
EDIT : It will count all the rows of $id
select count(*) as total, id, title from posts where type = 'article' AND id = $id;

Display data as menu from 3 tables

I have 3 tables, categories with column names as id and cat_id. Cat_id=0 give me Veg and Cat_id=1 gives me Non veg.
Now i have another table menu(columns me_id,me_name and re_id) having main categories and item(columns item_id,re_id,me_id,cat_id,item_name and item_rate) having sub_ctegories
i want out put like as below
Veg Menu Non Veg Menu
Main_cat1 Main_cat2
--Sub_cat1 --Sub cat1
Main_cat3
--Sub_cat3
Please help
First of all i could not understand why you have kept cat_id in item table, secondly what is the purpose of re_id which is a foreign key in item table whereas you have already kept me_id as foreign key. Finally you need a foreign key in menu table. Some thing as cat_id.
Keeping that in mind your query will be like this
SELECT * FROM categories INNER JOIN menu ON categories.cat_id = menu.cat_id INNER JOIN item ON menu.me_id = item.me_id WHERE cat_id = 0;
You can change the cat_id to 1 here: WHERE cat_id = 1 by some form input, jquery or simply by assigning id and passing parameters via url (GET method).

Where query from other table + Doctrine

I'm stuck with creating a where query. I have table item with item_id, item_title, item_description, item_created, item_approved. I also have a table article with PK item_id (from item table) and article_body. The last table is media with medium_id, item_id (FK), medium_url and medium_type.
Now I would like to select all the data from media where item.item_approved is not NULL and where item.item_id ins't present in the article table.
Now I can select all the data from media where item.item_approved is not NULL. But now I need to do another check that he doesn't select the items that are also in article table. My query so far:
$repository = $entityManager->getRepository('VolleyScoutBundle:Media');
$query = $repository->createQueryBuilder('m')
->join('m.item', 'i')
->where('i.itemApproved is not NULL')
->getQuery();
Most likely that you must use 2 queries. With JOINs it can not be done.

display multiple categories codeigniter

I have a relational database in this format
Table: posts
Columns: post_id,post_title,post_content
Table: categories
Columns: category_id,category_name
Table: posts_categories
Columns: post_id,category_id
Posts can have multiple categories so i store them in posts_categories using post and category id, when i get results from database using below query, it just display the last category, Is it possible to display all categories otherwise i have to run a separate query, here my code.
$this->db->select("p.*,pc.*,c.*");
$this->db->where('post_id', $id);
$this->db->from('posts AS p');
$this->db->join('posts_categories AS pc', 'pc.post_id = p.post_id', 'inner');
$this->db->join('categories AS c', 'pc.category_id = c.category_id', 'inner');
$q = $this->db->get();
Thanks for any help.
You didn't mention what fields you actually select. However, you can SELECT p.title, c.category_name and after doing your query (mentioned in the question), you should have multiple rows in your result, containing the posts title and a category name for that post.
Now if you want you can group these categories by posts in php, building a new array from the db result.

Categories