Laravel: fetch pivot relationship through another relationship - php

Using Laravel, I'd like to fetch an order status (order_status table) from an order (orders table) based on the
locale relationship stored in the orders table using a pivot table (order_status_locale table).
The pivot table contains the "localised" order status from the order.
Of course, I could fetch the localised order status (order_status_locale) directly from the database, but I'd like to
fetch the localised order status through a Laravel relationship.
Something like this:
$order = Order::find(123);
$order->localisedOrderStatus->label;
This is an example of the database structure:
orders:
id: int
locale_id: int
...other columns
locales:
id: int
...other columns
order_statuses:
id: int
...other columns
order_status_locale:
order_status_id: int
locale_id: int
label: varchar

its sometimes better to make model of your pivot table. like OrderStatusLocale.php
then you can make the relation through it.
lastly if you want to achieve
$order->localisedOrderStatus->label;
you can use laravel has one through. or i think its better to go through the relation like $order->order_status->locale->label

Related

Laravel, Eloquent: Fetch all entries from many-to-many relation

I have two tables linked by a pivot table (with a custom migration):
Interest table:
ID | label
Person table:
ID | label
PersonHasInterest table (custom migration):
InterestID | PersonID | notes
How can I get all records from the pivot table (with persons and interests joined in)? I don't want to get all interests of a person or all persons that have an interest but all entries (with joins) of the pivot table.
Even though Pivot extends Model it is not possible to call the standard model functions on a Pivot-object. Issue on Github.
What I came up is using the DB-Facade to execute a select statement, like so:
DB::table('person_has_interest')
->join('interest', 'person_has_interest.interest_id', '=', 'interest.id')
->join('person', 'person_has_interest.person_id', '=', 'person.id')
->get(); // further manipulation like select possible
Try to define a pivot model like this:
<?php
...
use Illuminate\Database\Eloquent\Relations\Pivot;
...
class PersonHasInterest extends Pivot
{
protected $table = '...'; // table name
}
Then use it: PersonHasInterest::all();

How to use whereIn query for hasMany associated model using laravel 5.4 Eloquent

I have one table employee_profile, I am using this table for searching employees, it's columns are id, user_id, experience, salary.
I have two more tables
employee_keyskills with columns user_id and keyskills_id
employee_preferred_cities with columns user_id and city_id
Now I have to find employees with experience, salary, keyskills[], salaries[]
Just I need help in finding results from hasMany associated tables, means employees should be filtered only with specified keyskills[] and cities[] from employee_keyskills and employee_preferred_citites tables.
you should use whereHas which allow you to add customized constraints to a relationship constraint.
check this
https://laravel.com/docs/5.4/eloquent-relationships#querying-relationship-existence

Laravel - Inserting/Updating Pivot Table

I have a pivot table employee_project. In it, I have id, employee_id and project_id.
Each project has an Account Manager and a Project Manager. So in my form I have name (for the project) which should add the id to project_id and I also have am_id and pm_id, which refer to employee_id. How do I get these IDs into the pivot table since they are named differently than employee_id?
This does not work:
$p = Project::create($request->all());
$p->employees()->sync(['am_id', 'pm_id']);
What should I do differently?
Table Structures
--projects
id
name
stage
status
timestamps
--employees
id
name
department
--employee_project
id
employee_id
project_id
Naming doesnot matter.The values inside array are employee_id anyway. $p->employees()->sync([1,2]); should work.
You are passing am_id and pm_id as string?
Try with:
$p->employees()->sync([$request->am_id, $request->pm_id]);

Joining table created_at and updated_at values not being set

I have two models, Product and Attribute. There is a 'product_attributes' joining table between 'products' and 'attributes'. The relationship is defined in both models using belongsToMany(), and the key of the table is a compound one of product_id and attribute_id.
I can successfully retrieve and store records in the joining table - so, as such everything is functioning as expected, with the exception that the created_at and updated_at in product_attributes is not being set.
Is the above by design, and is there anything I can do to rectify this?
You should use the belongsToMany function with the timestamps declaration like this:
$this->belongsToMany(<entity>)->withTimestamps();

Eloquent - Many to many where not found in the pivot table

My DB is set up like...
Accounts --< Accounts_Games >-- Games
Accounts
---------
id INT
Accounts_Games
---------
id INT
account_id INT
game_id INT
Games
---------
id INT
I have managed to get a list of Games by a single Account quite easily using belongsToMany. Now I need to get a list of Games that an Account is NOT joined to.
How can I achieve this in Eloquent?
Thanks
I am didn't know your tables fields in your each tables.
but you can do with this way : add 1 method like this in GamesEloquent file :
public function account_not_join()
{
return $this->belongsToMany('Account', 'accounts_games', 'game_id', 'account_id')->where('is_join', 0);
}

Categories