Select posts based on selected categories - php

I have following scenario:
user selects couple categories
user should see posts which belongs to these categories
post belong to one or more categories
I setup the database like this:
users -> category_user <- categories
posts -> categoriy_post <- categories
I managed to accomplish this, but I had to find all ids from these tables to find relevant posts. I need to make it simpler because this approach is blocking some other actions I need to do. This is my code:
$categoryIds = Auth::user()->categories;
$ids = array();
$t = array_filter((array)$categoryIds);
if(!empty($t)){
foreach ($categoryIds as $key => $value) {
$ids[] = $value->id;
}
}else{
return View::make("main")
->with("posts", null)
->with("message", trans("front.noposts"))->with("option", "Latest");
}
$t = array_filter((array)$ids);
if(!empty($t)){
$p = DB::table("category_post")->whereIn("category_id", $ids)->get();
}else{
return View::make("main")
->with("posts", null)
->with("message", trans("front.noposts"))->with("option", "Latest");
}
$postsIds = array();
foreach ($p as $key => $value) {
$postsIds[] = $value->post_id;
}
$t = array_filter((array)$postsIds);
if(!empty($t)){
$postIds = array_unique($postsIds);
$posts = Post::whereIn("id", $postsIds)
->where("published", "=", "1")
->where("approved", "=", "1")
->where("user_id", "!=", Auth::user()->id)
->orderBy("created_at", "desc")
->take(Config::get("settings.num_posts_per_page"))
->get();
return View::make("main")
->with("posts", $posts)->with("option", "Latest");
}else{
return View::make("main")
->with("posts", null)
->with("message", trans("front.noposts"))->with("option", "Latest");
}
How to do this properly without this bunch code?

Yes, there is Eloquent way:
$userCategories = Auth::user()->categories()
->select('categories.id as id') // required to use lists on belongsToMany
->lists('id');
if (empty($userCategories)) // no categories, do what you need
$posts = Post::whereHas('categories', function ($q) use ($userCategories) {
$q->whereIn('categories.id', $userCategories);
})->
... // your constraints here
->get();
if (empty($posts)) {} // no posts
return View::make() ... // with posts
Or even better with this clever trick:
$posts = null;
Auth::user()->load(['categories.posts' => function ($q) use (&$posts) {
$posts = $q->get();
}]);
if (empty($posts)) // no posts
return View... // with posts
Obviously, you can write joins, or even raw sql ;)

You can take those categories directly from the database from user records:
SELECT ...
FROM posts AS p
INNER JOIN category_post AS cp ON cp.id_post = p.id
INNER JOIN categories AS c on c.id = cp.id_category
INNER JOIN category_user AS cu ON cu.id_category = c.id
WHERE cu.id_user = 123 AND p.published = 1 AND ...
Joins in Laravel can be achieved, see the documentation: laravel.com/docs/queries#joins Maybe there is also an Eloquent way, I don't know, try searching :-)

Related

Laravel whereHas manyToMany exact match

I have a Model with manyToMany relationship:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function categories()
{
return $this->belongsToMany(Category::class, 'product_category', 'product_id', 'category_id');
}
}
So 1 Product can have multiple Categories.
I'm trying to query (fetch) all Products that have exact match of categories:
Example:
$categories = ['category1', 'category2'];
Product::whereHas('categories', function($q) use ($categories){
foreach ($categories as $categoryName) {
$q->where('name', $categoryName);
}
//or $q->whereIn('name', $categories);
})->get();
Also tried:
$categories = ['category1', 'category2'];
Product::whereHas('categories', function($q) use ($categories){
$q->whereIn('name', $categories);
}, '=', count($categories))->get();
Assume my Product has only category1 attached, the query should return this product.
If Product has both categories, but my array contains only category1, then this Product should be ignored.
So I'm trying to achieve: Fetch only products with specific categories. It is doable with Eloquent or DB builder?
Pseudo Code
$allow = for each Product->categories{
category Exist in $categories
}
Based on your requirement your MySQL query would be as follow:
Use LEFT JOIN to get total numbers of mapped categories for a product.
Use GROUP BY on a product to compare its total mapped categories vs matching categories based on user input.
Above comparison can be done using HAVING clause, where total mapped categories should be equal to the count of categories the user has provided. Same matching categories based on user input should also match the exact count of categories which the user has provided.
SELECT p.id
FROM products p
LEFT JOIN product_category pc ON p.id = pc.product_id
LEFT JOIN categories c ON c.id = pc.category_id AND c.name IN ('category1', 'category2')
GROUP BY p.id
HAVING COUNT(0) = 2 -- To match that product should have only two categories
AND SUM(IF(c.name IN ('category1', 'category2'), 1, 0)) = 2; -- To match that product have only those two categories which user has provided.
Same can be achieved by query builder in the following manner:
$arrCategory = ['category1', 'category2'];
$strCategory = "'" . implode("','", $arrCategory) . "'";
$intLength = count($arrCategory);
$products = Product::leftJOin('product_category', 'products.id', '=', 'product_category.product_id')
->leftJoin('categories', function ($join) use ($arrCategory) {
$join->on('categories.id', '=', 'product_category.category_id')
->whereIn('categories.name', $arrCategory);
})
->groupBy('products.id')
->havingRaw("COUNT(0) = $intLength AND SUM(IF(categories.name IN ($strCategory), 1, 0)) = $intLength")
->select(['products.id'])
->get();
dd($products);
Note: If you have only_full_group_by mode enabled in MySQL, then include columns of products table in both group by and select clause which you want to fetch.
Try using this without wherehas
Product::with(array('categories' => function($q) use ($categories){
$query->whereIn('name', $categories);
}))->get();
Thank you for your answers I solved this with: whereExists of sub select fromSub and array_agg, operator <# (is contained by):
$categories = ['category1', 'category2'];
Product::whereExists(function ($q) use ($categories){
$q->fromSub(function ($query) {
$query->selectRaw("array_agg(categories.name) as categoriesArr")
->from('categories')
->join('product_category', 'categories.id', '=', 'product_category.category_id')
->whereRaw('products.id = product_category.product_id');
}, 'foo')->whereRaw("categoriesArr <# string_to_array(?, ',')::varchar[]", [
implode(",", $categories)
]);
})->get();

Laravel pass list parameters (columns) in select query

I use three table spareparts, categories and parameter and YajraDatatables.
My controller action is:
public function anyData()
{
$spareparts_param_list = Sparepart::all();
$list='';
foreach ($spareparts_param_list as $value) {
foreach ($value->category->parameter as $par_list) {
$list .= $par_list->Name.',';
}
}
$spareparts = Sparepart::
join('cars', 'spareparts.car_id', '=', 'cars.id')
->select(['spareparts.id', 'cars.Brend', 'spareparts.Model', $list]);
$datatables = app('datatables')->of($spareparts);
return $datatables->make();
}
My array list $list print parameters such as color,type,tires,.
How to pass $list array in select query?
You can use whereIn:
public function anyData()
{
$spareparts_param_list = Sparepart::all();
$list = [];
foreach ($spareparts_param_list as $value) {
foreach ($value->category->parameter as $par_list) {
$list[] = $par_list->Name;
}
}
$spareparts = Sparepart::
join('cars', 'spareparts.car_id', '=', 'cars.id')
->whereIn('Name', $list)
->select(['spareparts.id', 'cars.Brend', 'spareparts.Model']);
$datatables = app('datatables')->of($spareparts);
return $datatables->make();
}
You can run this raw Mysql command:
select sp.id as sp_id, sp.model as sp_model, c.brend as car_brend, json_arrayagg(p.name) as p_name
from spareparts as sp
join cars as c on sp.car_id=c.id
join categories as cat on sp.category_id=cat.id
join parameters as p on cat.id=p.category_id
group by sp.model;
Above command will give you a result like the following:
1 | Audi A6 door | Audi A6 | ["color", "window"]
The last column will be a json column of all parameters related to each category.
You can use whereIn(), something like this:
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
Docs - https://laravel.com/docs/5.7/queries#where-clauses

Getting null array PHP

i have a database named "products" which has a column "categories". This table contain four category of products namely electronic,Decoration,clothes and vehicle. My target to show these category with their count ie:if there are four products belongs to category electronic, then output should be like this :electronic=4 and so on
My code
public function category()
{
$arrayCategorys = ['electronic','Decoration','clothes','vehicle'];
$data = [];
foreach($arrayCategorys as $arrayCategory)
{
$sql = "SELECT count(id) FROM products WHERE categories='$arrayCategory'";
$records = \DB::select($sql);
$data = array_merge_recursive($data, [
"{$arrayCategory}" =>isset($records[0]->count),
]);
$data=array_filter($data);
dd($data);
}
}
I want show output like this
'electronic'=>'4',
'Decoration'=>'2',
'clothes'=>'2',
'vehicle'=>'1' according to data in database
but iam getting nothing ,[]
You can GROUP BY your categories like this way when you COUNT
SELECT categories,COUNT(*)
FROM products
GROUP BY categories;
For Idea: http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-count-with-group-by.php
EDIT: Though i am not familiar with laravel5 syntax but this may work for you
$result = DB::table('products')
->select('products.categories',
DB::raw('count(products.id) as category_count')
)
->orderBy('products.id', 'asc')
->groupBy('products.categories')
->get();
You used isset($records[0]->count) but the column name for the count will be count(id). Name the count as count like this "SELECT count(id) AS count FROM products WHERE categories='$arrayCategory'". And you wont be able to get the count just by checking if it is set. Remove the isset and just use $records[0]->count. The code should look like:
public function category()
{
$arrayCategorys = ['electronic','Decoration','clothes','vehicle'];
$data = [];
foreach($arrayCategorys as $arrayCategory)
{
$sql = "SELECT count(id) AS count FROM products WHERE categories='$arrayCategory'";
$records = \DB::select($sql);
$data = array_merge_recursive($data, [
"{$arrayCategory}" =>$records[0]->count,
]);
$data=array_filter($data);
dd($data);
}
}

Second counting query for pagination

I have a couple of pretty complex queries, and for each of them I have to write a second query counting results. So for example, in the model:
$dql = "SELECT u FROM AcmeBundle:Users u LEFT JOIN AcmeBundle:Products p WITH u.id = p.id";
I would have to create a duplicate query like this:
$countingQuery = "SELECT COUNT(u.id) FROM AcmeBundle:Users u LEFT JOIN AcmeBundle:Products p WITH u.id = p.id";
The main problem with that is that with every change in the first query, I would have to change the second either.
So I came up with another idea:
$countingSelect = "SELECT COUNT(u.id)";
$noncountingSelect = "SELECT u";
$dql = " FROM AcmeBundle:Users u LEFT JOIN AcmeBundle:Products p WITH u.id = p.id";
return $this->getEntityManager()->createQuery($noncountingSelect . $dql)
->setHint('knp_paginator.count', $this->getEntityManager()->createQuery($countingSelect . $dql)->getSingleScalarResult());
It works of course, but the solution seems quite ugly with larger selects.
How can I solve this problem?
I believe the Doctrine\ORM\Tools\Pagination\Paginator will do what you're looking for, without the additional complexity.
$paginator = new Paginator($dql);
$paginator
->getQuery()
->setFirstResult($pageSize * ($currentPage - 1)) // set the offset
->setMaxResults($pageSize); // set the limit
$totalItems = count($paginator);
$pagesCount = ceil($totalItems / $paginator->getMaxResults());
Code yanked from: http://showmethecode.es/php/doctrine2/doctrine2-paginator/
You can create a customer repository as explained in the docs and add your query to that with a minor edit like..
use Doctrine\ORM\EntityRepository;
class ProductRepository extends EntityRepository
{
public function findProducts()
{
return $this->findProductsOrCountProducts();
}
public function findCountProducts()
{
return $this->findProductsOrCountProducts(true);
}
private function findProductsOrCountProducts($count = false)
{
$queryBuilder = $this->createQueryBuilder('u');
if ($count) {
$queryBuilder->select('COUNT(u.id)');
}
$query = $queryBuilder
->leftJoin('AcmeBundle:Products', 'p', 'WITH', 'u.id = p.id')
->getQuery()
;
if ($count) {
return $query->getSingleScalarResult();
} else {
return $query->getResult();
}
}
}
Then you can call your method using...
$repository = $this->getDoctrine()
->getRepository('AcmeBundle:Users');
// for products
$products = $repository->findProducts();
// for count
$countProducts = $repository->findCountProducts();
Note:
I know it's not best practice to just say look at the docs for the customer repository bit s here' the YAML mapping...
# src/Acme/StoreBundle/Resources/config/doctrine/Product.orm.yml
Acme\StoreBundle\Entity\Product:
type: entity
repositoryClass: Acme\StoreBundle\Entity\ProductRepository
# ...

Fuelphp get post comments on same page logic

First I'm really new to fuelphp, you can down vote the question if needed.
My problem is that i made a facebook similar wall, and i don't really understand the comments logic.
So i tried to join my tables this way
static function get_stream()
{
$query = DB::select()->from('stream_post');
$query->join('users_metadata');
$query->on('stream_post.user_id', '=', 'users_metadata.user_id');
$query->join('stream_comment');
$query->on('stream_post.stream_id', '=', 'stream_comment.stream_id');
$query->order_by('stream_post.stream_id', 'DESC');
$result = $query->execute();
if(count($result) > 0) {
foreach($result as $row)
{
$data[] = $row;
}
return $data;
}
}
the problem with this is that, this only shows the stream posts what have comments, and doesn't show the others.
So can please someone give me a logic how to join the tables to show those post to what doesn't have a comment?
Try that:
static function get_stream()
{
$query = DB::select()->from('stream_post');
$query->join('users_metadata');
$query->on('stream_post.user_id', '=', 'users_metadata.user_id');
$query->join('stream_comment', 'RIGHT'); // The RIGHT JOIN keyword returns all rows from the right table, even if there are no matches in the left table.
$query->on('stream_post.user_id', '=', 'stream_comment.user_id');
$query->order_by('stream_post.stream_id', 'DESC');
$result = $query->execute();
if(count($result) > 0) {
foreach($result as $row)
{
$data[] = $row;
}
return $data;
}
}
Edit:
That query should work (when every user_id from stream_post has the same user_id in users_metadata. Just transfer this query to fuelphp (I didn't use it before).
SELECT *
FROM stream_post
RIGHT JOIN stream_comment
ON stream_post.stream_id = stream_comment.stream_id
JOIN users_metadata
ON stream_post.user_id = users_metadata.user_id
ORDER BY stream_post.stream_id DESC

Categories