I have two models which are connected each other.
class Company extends Model {
public function addresses() {
return $this->belongsToMany('\App\Address', 'address_mapping', 'uid_company', 'uid_address');
}
}
class Address extends Model {
}
In my JOIN table I have a column named active. How can I fetch all active addresses from the company? Or how can I implement a where-clause in the JOIN table?
Thank you!
The table that you call a "JOIN table", usually called a pivot table.
You can fetch all active records by using wherePivot method:
$company = Company::first();
$activeAdresses = $company->addresses()->wherePivot('active', 1);
Or you can directly define the relationship in your model:
class Company extends Model {
public function activeAddresses() {
return $this->belongsToMany('\App\Address', 'address_mapping', 'uid_company', 'uid_address')
->wherePivot('active', 1);
}
}
See section Filtering Relationships Via Intermediate Table Columns in Eloquent documentation
Related
I have 3 models: Movie, Celebrity, Role. Each movie has many celebrities and each celebrity has many roles in many movies (for example the movie "Once Upon a Time" has celebrity "Quentin Tarantino" as roles ["Director","Writer"], and obviously "Quentin Tarantino" can have other movies performing different roles.
What is the best way to implement a many-to-many relationship between these 3 models, where we can easily access each movie's staff directly (like $movie->$roles->director)
Should we define a pivot table with movie_id, celebrity_id, role_id and a primary key of (movie_id, celebrity_id, role_id)?
class Movie extends Model{
public function movieCelebrityRole()
{
return $this->hasMany(MovieCelebrityRole::class);
}
}
class Celebrity extends Model{
public function movieCelebrityRole()
{
return $this->hasMany(MovieCelebrityRole::class);
}
}
class Role extends Model{
public function userRoleCompany()
{
return $this->hasMany(MovieCelebrityRole::class);
}
}
class MovieCelebrityRole extends Model{
public function movie()
{
return $this->belongsTo(Movie::class);
}
public function celebrity()
{
return $this->belongsTo(Celebrity::class);
}
public function role()
{
return $this->belongsTo(Role::class);
}
}
If you get a pivot table with more than two Foreign Keys, or have multiple many-to-many relationships, try to treat it as full model.
This also helps you to define more simple relationships.
Instead of Many-to-Many relationship. You can divide it into multiple of One-to-Many relationships.
E.g: In your case, you're going to have 4 models : Movie, Celebrity, Role, Program.
Movie hasMany Program.
Celebrity hasMany Program.
Role hasMany Program.
Program belongsTo Movie.
Program belongsTo Celebrity.
Program belongsTo Role.
For more details, check the Laravel documentation about the One-to-Many relationships :
https://laravel.com/docs/8.x/eloquent-relationships#one-to-many
I've two tables jobs table (id,title,description)->Jobs model
and candinates table (id,applied_job_id(FK jobs),user_id)->Candidate model
I want to get all the rows of jobs with the total number of candidate applied for each job
Jobs model
class Jobs extends Model
{
public function appliedCount()
{
return $this->hasMany('App\Models\Candidate','applied_job_id');
}
}
Candidate Model
class Candidate extends Model
{
protected $table = 'candidate';
}
Here is my query
$query = Jobs::limit($this->limit)->offset($offset)->get();
Currently it returns all the rows of jobs table
Note:
I will fetch the result as JSON
You're defining the relationship with a non existent model Candidate, where as the model is named JobActivity which uses the table candidate, when the table is actually named candidates. Try this.
class Jobs extends Model
{
public function candidate()
{
return $this->hasMany(Candidate::class,'applied_job_id');
}
}
class Candidate extends Model
{
}
$jobs = Jobs::withCount('candidate')->limit($this->limit)->offset($offset)->get();
I want to use where clause on the another relationship not my current selecting model as below table
table Customer
-------id-----customer_name
Table ModelA
table Customer
-------id-----fk_custome_id
table ModelB
-------id-----fk_ModelA_id
Function in Controller
$data['data'] = customer::with(['modelA','modelA.modelB'])
->where('fk_customer_id', 2)->get();
Customer Model
final function ModalA (){
return $this->hasMany('App\Models\ModelA', 'fk_customer_id', 'id');
}
ModelA Model
final function Modelb (){
return $this->hasMany('App\Models\ModelB', 'fk_modelA_id', 'id');
}
Error:
I will got error as below because select sql don't find the column name fk_customer_id in table customer, So how can I user fk_customer_id (in table ModelA) for where.
You can useYou can use whereHas like:
$data['data'] = customer::with(['modelA','modelA.modelB'])
->whereHas('modelA', function ($query) {
$query->where('fk_customer_id', 2);
})->get();
It's because when you're using with you just eager load constrints but you won't attach it to the main query (of the customer model). So there are two way: one modern with using whereHas or using join queries.
I'm building a PM system, and I have a problem.
This is my PM table:
id, user_id, to, content
Now, in my inbox page I'm fetching all the users that sent me a message.
$pms = DB::table('pm')->select('user_id')->distinct()->where('to', Auth::id())->get();
The problem is if I add more columns to the select method, it won`t be distinct anymore..
You can easily do that using Eloquent and its whereHas() method.
First, define models and the relation in your model:
class Message extends Model {
protected $table = 'pm';
}
class User extends Model {
public function sent_messages() {
return $this->hasMany(Message::class);
}
}
Now, fetch all users that have a related Message models where to column matches your ID:
$usersThatSentMeMessages = User::whereHas('sent_messages', function($query) {
$query->where('to', Auth::id());
});
How to join the three table in Laravel Eloquent model. My table structure is like. It is easy with raw(mysql with left join) query to get the desired result, but it is possible to get via Eloquent.
Customer table
id
title
name
Order table(has customer id)
id
customer_id
Hour table (has order id)
id
order_id
From Hour model I want to get the Customer Details and Order Details. Now I am able to get the Order details like below.
public function order()
{
return $this->belongsTo('App\Models\Order', 'order_id', 'id');
}
How to get the Customer Details?
I tried like below, but not get success
1.return $this->hasManyThrough('App\Models\Customer','App\Models\Order','customer_id','id');
2. return $this->hasManyThrough('App\Models\Customer','App\Models\Order','customer_id','order_id');
Edit 1:
class HourLogging extends Model
{
public function order()
{
return $this->belongsTo('App\Models\Order', 'order_id', 'id');
}
$timeoverview = HourLogging::select('hour_logging.*')->whereRaw("hour_logging.date BETWEEN '".$start_date."' AND '".$end_date."'")->orderBy('date','asc');
$timeoverview->with('order.customer');
return $timeoverview->get();
}
class Order extends Model {
public function customer() {
return $this->belongsTo('App\Models\Customer');
}
}
hasManyThrough works the opposite way to what you're trying to achieve. It would let you get Hour models from your Customer model easily.
In order to get what you need you need to define the relations that go the other way - from Hour to Order to Customer. Add the following relations to your models:
class Hour extends Model {
public function order() {
return $this->belongsTo('App\Models\Order');
}
}
class Order extends Model {
public function customer() {
return $this->belongsTo('App\Models\Customer');
}
}
With those relations in place you should be able to access Order data with $hour->order and Customer data with $hour->order->customer.
One thing to remember: Eloquent offers possibility to eagerly load related data to avoid additional queries. It makes sense to do it when you fetch multiple Hour records at once. You an load all Hour records with related Order and Customer data with the following:
$hours = Hour::with('order.customer')->get();