Select from 2 tables in Laravel 4 - php

I have a problem with my select, so I have 2 tables:
category
id name
product
id name category_id
Now I want to select products for one category:
public function getCategory($cat_id)
{
return View::make('store.category')
->with('products', Product::where('category_id', '=' , $cat_id))
}
This query is empty, exists another solution??Help me please.

Your function isn't built quite right. You aren't calling get() on the query, so its not actually running. And also, you aren't passing the products quite right. Try this
public function getCategory($cat_id)
{
$products = Product::where('category_id', '=' , $cat_id)->get();
return View::make('store.category')
->with(array('products' => $products));
}

Related

Not able to fetch data from primary table in laravel

I have 3 tables i.e category, sub category and products. I have passed category id to sub category and then sub category id to product so that i can get the whole data as per requirements. The problem in i an able to get the data from the secondary tables that has joins statement not from the primary table.Here is my code of query. Any help from you would be appreciated
public function showProduct()
{
$data = DB::table('category')
->join('sub_category','category.id',"=",'sub_category.category_id')
->join('product','sub_category.id', '=', 'product.sub_category_id')
->get();
return $this->success('date' ,$data);
}
use leftjoin
$data = DB::table('category')
->leftjoin('sub_category','category.id',"=",'sub_category.category_id')
->leftjoin('product','sub_category.id', '=', 'product.sub_category_id')
->get();

Fetch products by category id using pivot table

I have a function where I am passing the category id and based on that I want to fetch all the products .
Here is a structure of my db
Category db:
category_name
Product db:
product_name;
category_product:
category_id;
product_id;
Below are the relations between them
In Product :
public function categories()
{
return $this->belongsToMany(Category::class);
}
In Category:
public function products()
{
return $this->belongsToMany(Product::class);
}
I have tested multiple queries but nothing worked for my case .
You can do it in two different ways
Indirect way wich verifies that the category exists (2 queries)
$category = Category::with('products')->findOrFail($categoryId);
// you can also do without the with(); only one id, so no benefit for the eager load
// $category = Category::findOrFail($categoryId);
$products = $category->products;
Direct way, wich will return an empty collection if the category doesnt exist (but no message) (1 query)
$products = Product::whereHas('categories', function($qbCategory) use($categoryId) {
$qbCategory->where('id',$categoryId);
})->get();

How to get all the people in table 1 without getting the people who have me as parent_id in table 2?

I'm writing this post because I have a problem with my relationships on Laravel.
Here is the structure I currently have:
1st table:
- id
- name
- ...
2nd table :
- parent_id
- child_id
knowing that parent_id and child_id correspond to the same table. Here is the function that links them
public function relation()
{
return $this->belongsToMany('App\Person', 'person_relations', 'parent_id', 'child_id', 'id', 'id');
}
Currently I would like, for a search system, to get all the people in table 1 without getting the people who have me as parent_id in table 2.
I have added an inverse relation parents() to the People model instead of the relation() method in the question.
Person Model :
public function parents()
{
return $this->belongsToMany('App\Models\Person', 'person_relations', 'child_id', 'parent_id', 'id', 'id');
}
Controller :
public function test()
{
$myId = 1;
$persons = \App\Models\Person::whereDoesntHave('parents')
->orWhereHas('parents', function($qry) use($myId){
$qry->where('person_relations.parent_id', '!=', $myId);
})
->get();
}
This method will return all records which doesn't have a given $myId as their parent id.
Tested and working in Laravel 6.2 and 7.29

How to write query to get specific data from pivot table in laravel?

I'm still a beginner in Laravel. I'm trying to write a query to get categories which are associated with specific place. I have the following three tables
place place_categorye category
------ ---------------- -------------
id place_id id
name category_id name
each place has number of categories
what I want to do is when I choose place_id I get the categories associated with it in the pivot table.
I supposed that you have a Many To Many relation between places and categories then in your Place Model
public function categories(){
return $this->belongsToMany(Category::class, 'place_categorye', 'place_id', 'category_id');
}
And now you can access the categories of a certain Place like below:
$place->categories;
Without the relationship definitions, it is hard to give an answer on the ORM queries, but here is a raw query which will give you the expected result,
DB::select(DB::raw("
select category.id, category.name
from place
join place_categorye on place_categorye.place_id = place.id
join category on category.id = place_categorye.category_id
where place.id = 1");
if you want the results inclusive of all the null values, depending on the use case you can use a left join instead of join (join means innter join by default)
** to many relationship** for this
<?php
namespace App\PlaceCategory;
use Illuminate\Database\Eloquent\Relations\Pivot;
class PlaceCategory extends Pivot {
public function place()
{
return $this->belongsTo('App\Place');
}
public function category()
{
return $this->belongsTo('App\Category');
}
}
category Model
public function places()
{
return $this->hasMany('App\Place')
->using('App\PlaceCategory');
}
Place Model
public function categpries()
{
return $this->hasMany('App\Category')
->using('App\PlaceCategory');
}
Now you can access this easily & can query easily .
Like
$place=Place::with('categories')->first();
by using
$place->categories we get all the categories for this place

Codeigniter getting data by passing comma separated values

Here is my application details:
In mysql database, I have products table with id, description and price. And in basket table, I have products_id in the form 1,2,3 i.e. comma separated values.
Now, in controller, I got the product_id list from basket table by this function:
$data['baskets'] = $this->baskets_model->get_all();
$pr_id=$this->baskets_model->get_all();
foreach ($pr_id as $pr){
$get_pr_info=$this->products_model->get_all($pr->product_id);
}
I have this function in products_model model:
public function get_all($ids=NULL) {
if($ids !== NULL) {
echo $ids;
}
else {
$query = $this->db->get('products');
return $query->result_array();
}
}
The value $ids echoes the products_id as 1,2,3. Now, how to get the details of each product by passing this products_id from product table and show in basket? Please drop comment if my question is not clear.
Hope this may help you
public function get_all($ids=NULL)
{
$this->db->from('products');
if($ids !== NULL)
{
$this->db->where("id in ($ids)");//use this
//$this->db->where_in('id',$ids);//This may work but I think it will not give your right answer
//if your id like 1,2,3. this will not work i think but if you send only one id it will work.
}
$query = $this->db->get();
return $query->result_array();
}
Add where condition in to the database query i.e.
$this->db->where_in('your_column_name',$ids);

Categories