I have 3 tables school ,schooldetails & admission, and i want to get all data in index page how can i do that , i am getting data of school and admission table due to relationship but not of school details kindly check it and help
school
id
name
name
mobile
city
schooldetails
id
school_id(foreign key of school id)
contact_person
admission
id
school_id(foreign key of school_deatils id)
admission_classes
start_date
end_date
My function
public function schoolslist($class='', $city='')
{
$schools = Admission::where('admission_classes', 'like', "%{$class}%")->where('status', '1')->orderBy('id','desc')->whereHas('school', function($query) use($city) {
$query->where('city', 'like', $city);
})->paginate(10);
return view('frontend.index',compact('schools'));
}
my Admission model
public function School()
{
return $this->belongsTo('App\School');
}
public function SchoolDetails()
{
return $this->belongsTo('App\SchoolDetails');
}
My view
#foreach($schools as $i => $school)
{{ $school->School->name}} from user table Ravi
{{ $school->SchoolDetails->contact_person}} //from school_Details table No result
{{ $school->start_date }} //from admission table date 11/22/2018
#enforeach
SchoolDetails relation should be with school not addmission so remove SchoolDetails() from Admission Model and add it to School Model
School Model:
public function Admission()
{
return $this->hasMany('App\Admission');
}
public function SchoolDetails()
{
return $this->hasOne('App\SchoolDetails'); \\Edited
}
Admission model:
public function School()
{
return $this->belongsTo('App\School');
}
SchoolDetails model:
public function School()
{
return $this->belongsTo('App\School'); \\Edited
}
Then you can change your View accordingly:
#foreach($schools as $i => $school)
{{ $school->School->name}} from user table Ravi
{{ $school->School->SchoolDetails->contact_person}} // This Line is changed
{{ $school->start_date }}
#enforeach
Related
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');
}
I have 1 relation
Order(id, user_id, total_price)
OrderDetail(order_id, product_id, num_of_product)
Product(id, title, num_of_existed, price)
Ask: How to get order across template:
order:[
id: 1,
user_id:1,
total_price:1.1,
order_detail:[
id:1,
order_id:1,
book_id:1,
num_of_product:10
product:[
id:1,
title: 'abc',
num_of_existed: 100,
price: 0.1
]
]
]
Create relationship in your Order model as ,
public function order_details()
{
return $this->hasOne('App\OrderDetail','order_id','id');
}
In your Orderdetail model,
public function product_details()
{
return $this->belongsTo('App\Product','id','product_id'); // first foreign key then local key
}
In your controller
$orders = Order::all();
return view('view_name',compact('orders'));
In the view
#foreach ($orders as $order)
#if ($order->order_details)
{{$order->order_details->num_of_product}}
#if($order->order_details->product_details)
{{$order->order_details->product_details->title}}
{{$order->order_details->product_details->num_of_existed}}
{{$order->order_details->product_details->price}}
#endif
#endif
#endforeach
you can do it via laravel eloquent relationship, to define relationship you will have 3 models like Order, OrderDetail,Product related to corresponding tables.
On your order model:
public function OrderDetail()
{
return $this->hasOne('App\OrderDetail','order_id');
}
On your OrderDetail model:
public function Order()
{
return $this->belongsTo('App\Order','order_id');
}
public function Product()
{
return $this->belongsTo('App\Product','product_id');
}
Then on your controller
public function foo(){
$orders=Order::all(); //remember to use 'App\Order'
return view('filename',compact('orders'));
}
Now on your blade, you will find all related table data just by chaining orders
#foreach ($orders as $order)
#if ($order->OrderDetail)
#if($order->OrderDetail->Product)
{{$order->OrderDetail->Product->title}}
#endif
#endif
#endforeach
for more see documentation
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();
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
}
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')