Laravel 5.3 Eloquent Relationship 1-1 Query - eloquent

Hi guys how could I query a data from my database with one-to-one relationship on eloquent model?
I want to query the menus with specific category id.
For example I only want to query the meals with "Breakfast Category".
Menu Model:
public function menucat()
{
return $this->hasOne('App\MenuCat', 'menu_cat_id', 'id');
}
Menu_Cat Model
public function menus()
{
return $this->belongsTo('App\Menu', 'menu_cat_id', 'menu_cat_id');
}
Database Table:
menus Table
id | menu_cat_id | menuName
menu_cat Table
menu_cat_id | menuCatName
I find it easy using the Query builder but I would like to use the eloquent to query out the data I need.
Thanks in Advance!

in the documentation you have that
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
you need to inverse menu_cat_id and id like that
return $this->hasOne('App\MenuCat', 'id', 'menu_cat_id');

Related

how to select particular column from relationship collection table in laravel mongodb jessenger

I have 3 columns in my database. 2 columns is connected with one column by using a hybrid relationship.
here is my query.
$data=Client::with('product','department')->select(['product.product_name','product.product_description']);
how to select row from another table?
in your product or department relation method do the selection with all the forging keys if you have any other relation for product for later use is you want them like
public function product()
{
return $this->hasMany(department::class)->select(['id', 'another_relation_to_product_id', 'product_name', 'product_description']);
}
You can do it this way
$data = Client::with(['product:id,product_name,product_description','department'])->get();
see docs https://laravel.com/docs/5.7/eloquent-relationships#constraining-eager-loads in Eager Loading Specific Columns section. Or you can do it
App\User::with([
'product' => function ($query) {
$query->select('id', 'product_name', 'product_description');
},
'department'
])->get();

eloquent refer to a column of a related a model

I have three tables:
categories
id, title
products
id, name
categories_products
id, category_id, product_id
I have also setup the according models and relationships (both have belongsToMany of the other)
Now I want to get all products belonging to a category
Category::where('title','Electronics')->first()->products()->limit(10)->get(['products.name']);
which works fine, but I also want to include the category title for each product as well:
Category::where('title','Electronics')->first()->products()->limit(10)->get(['products.name','category.title']);
However it returns: Column not found category.title
I thought that the relation would take care of it.
EDIT: Models -->
Category:
class Category extends Model
{
protected $fillable = array('title');
public function products()
{
return $this->belongsToMany('Product', 'categories_products', 'category_id', 'product_id');
}
}
class Product extends Model
{
protected $fillable = array('name');
public function categories()
{
return $this->belongsToMany('Category', 'categories_products', 'product_id', 'category_id');
}
}
The reason you're getting the error is because get() works just like select() and because you're running the category query and then running the product query after there is no categories table to reference for the select.
Look into Eager Loading. It will help with a lot of these kinds of issues. Your query can be written as:
Product::select('id', 'name')
->with(['categories' => function($query) {
return $query->select('id', 'title');
}])
->whereHas('categories', function($query) {
return $query->where('title', 'Electronics');
})
->limit(10)
->get();
Because we are lazy loading you NEED the id column on each model so Laravel knows where to attach the relationships after the queries are run.
The with() method above will eager load the categories relationship and the whereHas() method puts a relationship constraint on the current query.
UPDATE
Similar query from Category model:
$category = Category::where('title','Electronics')
->with(['products' => function($query) {
return $query->select('id', 'name')->limit(10);
}])
->first(['id', 'title']);
Then access the products with:
$category->products

Eloquent ORM One to many query is not working

I am working with laravel 5.4. I have created countries and states table.
countries table looks like below
And my states table is :
Here, I have written join query as shown in below. It works perfect.
$state = DB::table($this->tbl_states)
->join($this->tbl_countries, $this->tbl_countries.'.id', '=', $this->tbl_states.'.country_id')
->select($this->tbl_states.'.*', $this->tbl_countries.'.name as country_name')
->whereNull($this->tbl_states.'.deleted_at')
->paginate(10)
But, Instead of writing this query I want to use Eloquent ORM so what query should I have to write?
In Country model I have create function that looks like below :
public function states()
{
return $this->hasMany('state');
}
And in State model I have write function that looks like below :
public function country()
{
return $this->hasOne('country');
}
In Country model try this:
public function states()
{
return $this->hasMany(App\State::class);
}
And in State model:
public function country()
{
return $this->belongsTo(App\Country::class);
}
And then $country->states will give you all states of this country. As well as $state->country will return state's country.
Official Docs: Eloquent: Relationships

Laravel Eloquent Relationship 3 tables

I am a newbie to Laravel 5.2 and am working on a legacy system and am a bit confused regarding eloquent and would appreciate someone giving me the code.
There are 3 tables:
cards
categories
cards2cat
cards can be part many categories which are kept in cards2cat table.
The cards2cat table has the following structure
id (primary key)
image (card)
category
What I want to do is to have a method in the Cards model called something like getCardsWithCategores which returned the cards info plus the names of the categories from the category table.
The categories table has a key of id and a field category.
Thanks!
Go to your Card2Cats model and add this:
public function categories()
{
return $this->hasOne('App\Categories','id','category');
}
public function cards()
{
return $this->hasOne('App\Cards','id','image');
}
For the query you do this:
$cards = Card2Cat::with('categories','cards')->get();
foreach ($cards as $key => $value) {
echo $value->id.', Card:'.$value->cards->name.', Category:'.$value->categories->category.'<br>';
//$value->cards gives you all column of cards and you can do
//$value->cards->colName
// same goes for $value->categories
}
Make sure the spelling of your classes and table column names are correct before running the code :D

join and where clause on child in Laravel 5 Eloquent

I am developing an application with Laravel 5, I have 2 tables product and Category, the producttable has a column category_id which refers to id of catgeory table
I also have 2 models Category and Product
Question:
I just need to add a simple function to my Category model which return the categories joined by products where the product.sx_code = 0 with eloquent
You can just add a normal relationship method and append a where condition:
public function specialProducts(){
return $this->hasMany('App\Product')->where('sx_code', 0);
}