Getting issue while fetching data using Relationship in laravel7 - laravel

I have three table country, state, customer_contacts. I want display all customer details into list view structure. But in my customer table only id save of country and state respectively. I want to fetch Country nad state Name from other two table .
Table structure as follows :
1. Country
id name
1 India
2 Canada
2. State
id name country_id
1 Mumbai 1
2 Delhi 1
3 abc 2
4 xyz 2
3. Customer_contact
id c_name country_id state_id
1 abcdee 1 2
2 xyzerr 1 1
3 extraa 2 3
4 newsss 2 4
i want to fetch customer name with country name and state name.
I am using below query to fetch data but getting only customer_contact data how to fetch name using any query or relationship.
$data = CustomerContact::with('Country', 'State')->get();
I am using relationship as follow:
1) Country Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
public function state()
{
return $this->hasMany(State::class);
}
public function customercontact()
{
return $this->hasMany(CustomerContact::class);
}
}
2) State Model
class State extends Model
{
public function customercontact()
{
return $this->hasMany(CustomerContact::class);
}
public function country()
{
return $this->belongsTo(Country::class);
}
}
3) CustomerContact
use Illuminate\Database\Eloquent\Model;
class CustomerContact extends Model
{
protected $guarded = [];
public function country()
{
return $this->belongsTo(Country::class);
}
public function state()
{
return $this->belongsTo(State::class);
}
}
I want show data in list view like CustomerName, CountryName,StateName.
When i do
dd($data)
getting data with country and state id but relationship getting null value.
Help me in this.

On CustomerSupport you defined two function country() and state(), so your code will be :
$data = CustomerContact::with('country','state')->get();
Or,
$data = CustomerContact::with('country')->with('state')->get();

Related

How do I retrieve all polymorphic relations for one model?

I have setup a User model to be able to be linked with a polymorphic many to many to 3 models, let's call them A, B and C.
I have setup the models as follows:
// User.php
public function a(): MorphToMany
{
return $this->morphedByMany(A::class, 'shareable')->withPivot('view_mode');
}
public function b(): MorphToMany
{
return $this->morphedByMany(B::class, 'shareable')->withPivot('view_mode');
}
public function c(): MorphToMany
{
return $this->morphedByMany(C::class, 'shareable')->withPivot('view_mode');
}
// A, B and C.php
public function users(): MorphToMany
{
return $this->morphToMany(User::class, 'shareable');
}
Of course I created a pivot table to store the relations.
I am now trying to find a way to find ALL relations linked to a given user based on the pivot table. Let's say this is currently my data:
user_id
view_mode
shareable_type
shareable_id
1
RW
App\Models\A
1
1
R
App\Models\A
2
1
W
App\Models\B
5
2
R
App\Models\A
2
1
W
App\Models\C
8
How can I retrieve all the related content for User with id 1?
My goal would be for it to look like this:
relations: [
a: [
- A entity with id 1
- A entity with id 2
],
b: [
- B entity with id 5
],
c: [
- C entity with id 8
]
]
I tried by created a model named Shareable, and adding the following:
protected $table = 'shareables';
public function shareable()
{
return $this->morphTo();
}
public function a()
{
return $this->morphedByMany(A::class, 'shareable');
}
I then added the following to the User model:
public function shared()
{
return $this->hasMany(Shareable::class)->with('a');
}
But when I call it by doing
$user->shared
I don't get my a instances, only an empty collection.
What am I doing wrong?

laravel 5.8 access other table's property in one to many relation

I have 3 tables
hotel
province
city
the hotel has a relation to the city and the city has a relation to the province.
city id will save in the city field in the hotel table.
how can I access city name and province name in the Hotel model?
in hotel model I wrote this :
public function city(){
return $this->belongsTo(City::class);
}
and in the city model I have this:
public function hotel(){
return $this->hasMany(Hotel::class);
}
public function province(){
return $this->belongsTo(Province::class);
}
and in province
public function cities(){
return $this->hasMany(City::class);
}
public function provinces()
{
return $this->hasManyThrough('App\Province', 'App\City');
}
In hotel model and you can access province name by using in the controller like
$hotel = Hotel::find(1);
dd($hotel->provinces);
and use that variable in the blade
Has Many Through relationship is what you are looking for.

Eloquent Relationship in the same model in Laravel

I am using User model to fetch user_type = 5. They are associated with user_type = 3 in vendors table. I want to make a relationship in User or Vendor model which could help me get users of type 5 along with the associated user of type 3.
To get data with a criteria you can use scopes, like this :
public function scopeMyType($query)
{
return $query->where('user_type ', 3);
}
To use it :
$users = User::myType()->get();
// this will return users with type = 3
You can make self relationship like this:
class User extends \Eloquent {
....
public function hasOne() {
$children = $this->hasMany('Vendor');
}
}

how to make join query between three tables in laravel

I want to build a query in laravel between three tables (regions, countries, packages) such that table are related together.
regions table contains (id, name, description)
countries table contains (id, region_id, name, description)
packages table contains (id, county_id, pkg_title, pkg_description, price)
I want to select all packages where region_id=1
how can I make query for this situation in laravel query builder. please help me about this question
Set your models as follows.
class Region extends Model
{
}
class Country extends Model
{
public function region()
{
return $this->belongsTo(Region::class);
}
}
class Package extends Model
{
public function country()
{
return $this->belongsTo(Country::class);
}
}
And state following query inside your method.
$region_id = 1;
$PackageWithRegions = Package::with([
'country' => function ($county) use ($region_id) {
return $county->with([
'region' => function ($region) use ($region_id) {
return $region->where('id', $region_id);
}
]);
}
])->get();
// $PackageWithRegions is a collection of packeges with regions where regiion_id = 1
More on eloquent relationships
You can use Eloquent model relationship to prform this, like below
In region controller file
$region = Region::where(['id'=>1])->with(['country'])->get(); ///fetch region with country calls relation declared in region model
In Region.php model
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Region extends Model {
protected $table = 'regions';
public function country(){
return $this->hasOne('App\Models\Country','region_id')->with(['packages']); ///this will fetch country with all pacckages on matching region_id, join of package with country is declared in country model.
}
}
In Country.php model
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Country extends Model {
protected $table = 'countries';
public function packages(){
return $this->hasMany('App\Models\Package','county_id'); //join of country with packages on county_id
}
}
In Package.php model
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Package extends Model {
protected $table = 'packages';
}
You can see in Laravel doc (https://laravel.com/docs/4.2/eloquent) in section Has Many Through that if you have a model like:
countries
id - integer
name - string
users
id - integer
country_id - integer
name - string
posts
id - integer
user_id - integer
title - string
Then you can access to last relation in this way:
class Country extends Eloquent {
public function posts()
{
return $this->hasManyThrough('Post', 'User');
}
}
Or using customs relations IDs:
class Country extends Eloquent {
public function posts()
{
return $this->hasManyThrough('Post', 'User', 'country_id', 'user_id');
}
}
So, you can access to the posts of a country:
$posts = Country::get(2)->posts;
// with where:
$posts = Country::where('name', '=', 'USA')->get()->posts;

Laravel hasMany relationship

I have three tables, stores, store_categories and product_categories. Structure of each table is below
stores
id name
1 Mystore
store_categories
id store_id product_category_id
1 1 1
2 1 2
product_categories
id name
1 Grocery
2 Vegetable
In my Store model, I write the relation
public function store_categories(){
return $this->hasMany('App\StoreCategory');
}
So to get all data of a store, I write i StoresController
$res = Store::with('store_categories')->get(); dump($res);
But the dump shows store_id and product_category_id in relations. How can I display their names( ie store name, product category name etc ) ?
You need to add Many to Many relationship as follows:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Store extends Model
{
/**
* The roles that belong to the user.
*/
public function categories()
{
return $this->belongsToMany('App\ProductCategory','store_categories','store_id','product_category_id');
}
}
Then you can perform the following:
$res = Store::with('categories')->get(); dump($res);

Resources