How get rows from closest table using one to one? - laravel

I have two tables:
Hotels | Countries
country id name
I need to get all notes from Hotels , and get Hotels.country will be name of country: Countries.name
There is function in Hotel model:
public function country()
{
return $this->hasOne('App\Country', 'country', 'id');
}
In controller I try to get all hotels with related countries:
$hotels = Hotel::all();
After I try to get country name for each row from $hotels:
foreach($hotels as $item){
echo $item->country["name"]; // It does not work
}

It should be
public function country()
{
return $this->belongsTo('App\Country', 'country', 'id');
}
After that,
#foreach ($hotels as $hotel) {{ $hotel->country->name }}
should work.

You will need to use eager loading.
$hotels = Hotel::with('country')->get();
Check out the documentation here.
In order to access the country name, it's a normal object:
$hotel->country->first()->name
If you want to have a direct function, in your Hotel Model
Hotel.php
public function countryName()
{
return $this->country->first()->name
}

Related

Laravel fetch intermediate table data with many to many relationship

I have 3 tables
user, specialities & results
results contain specialitie_id, user_id
I have create many-to-many relationship
Here's the relationship in user model
public function specialities()
{
return $this->belongsToMany(Speciality::class, 'results', 'user_id', 'specialitie_id')->withPivot('result', 'color');
}
I want to fetch data from the result table based on specialitie_id
If my url is
abc.com/result/5
I have attached table structure as well,
it should show me result related to specialitie_id 5
I tried this but it doesn't work
$result = User::with('specialities')->where('id', 5)->get();
Any help would be appreciated.
Your current query:
$result = User::with('specialities')->where('id', 5)->get();
Is saying, give me all results where the USER_ID = 5 WITH all the specialities related to it.
You are very close but did it backwards:
//Give me all results where the SPECIALITY_ID = 5
//WITH all the users related to it
$result = Speciality::with('users')->where('id', 5)->get();
Please note that the above is an array
Example of a Controller Function:
public function results($id){
$results = Speciality::with('users')->where('id', $id)->get();
return view('user_specialities', compact('results'));
}
Example view user_specialities:
#foreach($results as $result)
<p>Result: {{$result->result}}</p>
<p>Formulary: {{$result->formulary}}</p>
<p>Color: {{$result->color}}</p>
<p>User ID: {{$result->user->id}}</p>
<p>User Name: {{$result->user->name}}</p>
#endforeach
Speciality Model:
public function users()
{
return $this->belongsToMany(User::class, 'results', 'specialitie_id', 'user_id')->withPivot('result', 'color');
}
User Model:
public function specialities()
{
return $this->belongsToMany(Speciality::class, 'results', 'user_id', 'specialitie_id')->withPivot('result', 'color');
}

laravel multiple relation query

I have 3 model in my project and i want to get a query or collection result as i say:
JewelsItem model:
protected $table = 'jewel_items';
public function jewel(){
return $this->belongsTo('App\Models\Jewel');
}
public function sellInvoice(){
return $this->belongsTo(SellInvoice::class,'sell_invoice_id');
}
2.Jewel model:
public function jewelsItems(){
return $this->hasMany('App\Models\JewelsItem');
}
3.sellInvoice model:
protected $table = "sell_invoices";
public function jewelsItems(){
return $this->hasMany(JewelsItem::class,'buy_invoice_id');
}
Query: i want to get all jewelsitems that has no sellInvoice and has jewel name like 'something'.
note: jewel model has a name attribute .and i want to add name of jewel to first of all the collection's items of result.
i know how to get all jewel with name of 'something' :
Jewel::where('name','like','something'.'%')->get();
but i can't get all jewelItem related to it an add name of jewel to first of them.
To look for condition in another relationship you can use whereHas() and whereDoesntHave()
$name = 'something';
$items = JewelsItem::whereHas('jewel', function($query) use ($name) {
$query->where('name', 'LIKE', "%{$name}%");
})
->whereDoesntHave('sellInvoice')
->with('jewel')
->get();
It reads: getting jewel items where jewel has $name in the name field
and doesn't have sell invoice
then add related jewel to the jewel item.
When you want to retrieve the name of the jewel, you can access its property like so.
foreach($items as $item) {
echo $item->jewel->name;
}

Laravel: How to get data from 3 tables with relationship

I have 3 Tables:
Customers
id
name
Sales
customer_id
sale_date
Contacts
customer_id
contact_date
There aren't any update operations in the contacts table. Each process opens a new record in the contacts table. So, a user can have more than one records in the contacts table.
Here are my relations in models:
Customer
public function contacts()
{
return $this->hasMany(Contact::class);
}
public function sales()
{
return $this->hasMany(Sale::class);
}
Contact
public function customer()
{
return $this->belongsTo('App\Customer', 'customer_id');
}
Sale
public function customer()
{
return $this->belongsTo('App\Customer');
}
I would like to have the latest record of the contacts table and make it join with the other related tables.
Here is the query which I have tried:
$record = Contact::groupBy('customer_id')
->select(DB::raw('max(id)'));
$result = Customer::query();
$result->where('is_active', 'YES');
$result->with('sales');
$result->whereHas('contacts', function ($q) use($record){
return $q->whereIn('id', $record)->where('result', 'UNCALLED');
});
return $result->get();
In the blade file, I get some result in foreach loops. However, I am unable to get the related data from the sales and contacts table.
#foreach($result as $item)
#foreach($item->sales as $sale) // Has no output and gives error: Invalid argument supplied for foreach()
#foreach($item->contacts as $contact) // Has no output and gives error: Invalid argument supplied for foreach()
Can anyone help me how to display the sale and contact date? Or any idea for how to improve this code quality?
If you want the latest record of the contacts you can declare another relationship on the Customer model, e.g.:
public function latest_contact()
{
return $this->hasOne(Contact::class)->latest('contact_date');
}
BTW you can always declare one or more hasOne additional relationship if you have a hasMany in place the foreign key used is the same.
In this way you can retrieve latest_contact eager loaded with your Customer model:
$customer = Customer::with('latest_contact')->find($id);
Or use this relationship in your queries, something like that:
$customers = Customer::where('is_active', 'YES')
->with('sales')
->with('contacts')
->whereHas('last_contact', function ($q){
return $q->where('result', 'UNCALLED');
})->get();
Or that:
$customers = Customer::where('is_active', 'YES')
->with('sales')
->with('contacts')
->with('last_contact', function ($q){
return $q->where('result', 'UNCALLED');
})->get();
If you want you can declare last_contact with the additional where:
public function latest_contact()
{
return $this->hasOne(Contact::class)
->where('result', 'UNCALLED')
->latest('contact_date');
}
This way all other queries should be easier.
I hope this can help you.
I'm not sure, but can you try to do the following:
return Customer::where('is_active', 'YES')
->with([
'sale',
'contact' => function ($query) use($record) {
return $query->whereIn('id', $record)->where('result', 'UNCALLED');
}
])->get();

Laravel 5.5 Relationships - ErrorException Trying to get property of non-object

Can't display an info from DB to view, have an error
ErrorException
Trying to get property of non-object
Let me explain the logic:
User can create many companies
Companies can have many items
I don't have problems with inserting info to DB, everything is fine.
I can display every info from Users table and Company table in my Show View, but I can't display info from Company_Items tables in my view.
I guess something is wrong with my Relationships or keys, but what?
Trying to display Item name that belong to the specific company
{{$company->company_items->name}}
Company Table:
id(int)
company_name(string)
info(text)
user_id(int)
Company_items Table:
id(int)
company_id(int)
name(string)
Company Model:
class Company extends Model
{
protected $table = 'company';
//relation. MANY companies can be created by ONE user
public function user(){
return $this->belongsTo('App\User');
}
//relation. ONE company can have MANY items
public function item(){
return $this->hasMany('App\Item');
}
}
Item Model:
class Item extends Model
{
protected $table = 'company_items';
//relation. MANY items can be applied TO ONE company
public function company(){
return $this->belongsTo('App\Company');
}
}
Controller:
public function show($id)
{
$company = Company::find($id);
$user = User::find($id);
$company_items = Item::find($id);
return view('company_show')->with(['company' => $company,'company_items' => $company_items]);
}
View:
This is <b>{{$company->company_id}}</b> page info created by <b>{{$company->user->name}}</b><br>
Company category is <b>{{$company->company_category}}</b>.<br>
Item name is <b>{{$company->company_items->name}}</b>. // Can't display this expression
$company->company_items is a list of all the items of the company, you can't call a param on it. And you declared your relation with the name item and not company_items
Try to do :
$company->item[0]->name
or
#foreach($company->item as $item)
{{ $item->name }}
#endforeach
First error you made is:
$company_items = Item::find($id);
This will return the company Item that has the ID=$id which means it will return the Item that has the same ID as the company! I'm sure that's not what you want.
Instead you can do:
$company_items = $company->item;
which will return all the items of that company
and in your view you can access the items with a foreach loop:
#foreach($company_items as $item){
{{$item->name}}
}

How to fetch specific columns from multiple tables in Laravel 5?

I have 3 tables,
Country:
id,
name
Province:
id,
name,
country_id
City:
id,
name,
province_id
I have defined relationships in Model as follows,
Country Model:
public function Provinces()
{
return $this->hasMany('App\Province');
}
Province Model:
public function Country()
{
return $this->belongsTo('App\Country');
}
public function Cities()
{
return $this->hasMany('App\City');
}
City Model:
public function Province()
{
return $this->belongsTo('App\Province');
}
I am using the below query, but it overwrites all the data with Country name.
$city = DB::table('cities')
->join('provinces', 'cities.province_id', '=', 'provinces.id')
->join('countries', 'provinces.country_id', '=', 'countries.id')
->select('cities.name','provinces.name','countries.name')
->get();
I want to fetch a result of only City Name, Province Name, Country Name from these tables in laravel 5. Can you help me with that ?
Try just using as:
->select('cities.name as city_name', 'provinces.name as province_name', 'countries.name as country_name')
Would you try with this :
$city = DB::table('cities')
->join('provinces', 'cities.province_id', '=', 'provinces.id')
->join('countries', 'provinces.country_id', '=', 'countries.id')
->get(['cities.name', 'provinces.name', 'countries.name']);
I hope it helps you !
ok this is going to be a specific answer
learn about realtions in laravel and look at these
class example1 extends Model
{
public function examplefunction() {
return $this->hasMany('App\othermodel', 'id');
}
}
then use this query
$abc=example1::with('examplefunction')->get();
and there you go just take a look at a laravel relationship by using this u can get combined result
$products = User::select('id', 'email', 'first_name', 'last_name','country_code','mobile')->get()->toArray();
$gaurang = Ticket_Thread::select('body','title','resolv_note')

Resources