relational database in eloquent laravel - laravel

hay i have table schema like this
table transaction_fueler
id | code | user_id |user_name| ref_code | area_name
table user_community
user_id | community_id
table user_community is pivot table from table users and community
in table transaction_fueler i want to get community_id from table user_community, depends on user_id* in **transaction_fueler how to get community_id depends on user_id in table transaction_fueler ??
i stuck this.. thanks

i recommend joining the two tables and select the columns you want:
$values = DB::table('transaction_fueler')
->join('user_community', 'user_community.user_id', '=', 'transaction_fueler.user_id')
->select('transaction_fueler.*', 'user_community.community_id')->get();

Related

Laravel 5.2 - Can You Create A Relationship Between A Pivot Table And A Table It's Not Pivoting For?

I have a pivot table called user_store. It's used to establish a many to many relationship between the users table and the stores table and looks something like this:
user_id - int
store_id - int
user_age_range - string
I have another table called user_memberships. It's columns look like:
user_id - int
store_id - int
membership_cost - decimal
membership_expiration - date
I need to establish a relationship between the user_memberships table and the user_store pivot table using the user_id and store_id columns present in both tables.
How would I do this when the user_store table does not have its own model (since it's a pivot table)?

Lavavel Eloquent: Relationships with 5 table

I have an app consisting of these tables
users
id
name
user_location
id
user_id
cities_id
cities
id
state_id
country_id
name
states
id
country_id
name
countries
id
name
Then how to get relationship this five table?
you have to create a many-to-many relationship between tables and use pivot table to show the relationship between table rows using foreignId

laravel join 3 table belongtomany

i have 3 tables and 1 tables pivot with 3 foreign keys , i want to show all artists name with theirs job for one media but i dont have any idea how about joinning those 3 tables
Media table: media_id , media_title
artist table: artist_id, artist_name
job table: job_id, job_name
pivot table: media_id, artist_id, job_id
I have tried to do with belongsToMany between them but no result, only show the artists name but can not get theirs job name for each
Thank you to much!
Can you give the relation between these tables?
I didn't really know what you want, but try this.
\DB::table('media_artist_job')
->join('media','media.media_id','=','media_artist_job.media_id')
->join('job','job.job_id','=','media_artist_job.job_id')
->join('artist','artist.artist_id','=','media_artist_job.artist_id')
->select([
'media.medial_title',
'job.job_name',
'artist.artist_name'
])->where('media.media_id',/*The media id you want to retrieve*/)->get();
Hope it help!

Laravel 5.3 query to get results from 4 tables which has connections via Foreign Key

I'm using Laravel 5.3. I've 4 tables.
Default Users table. Departments, Position, Employees tables.
Users table has ID | Email | Password
Departments table has ID | Department | User_Id - Here User_Id is foreign key comes from Users table's ID
Positions table has ID | Position | Department_Id - Here Department_Id is foreign key comes from Departments table's ID
Employees table has ID | Employee | Position_Id - Here Position_Id is foreign key comes from Positions table's ID
User can have multiple Departments. Departments can have multiple Positions, Positions can have multiple Employees. So, if user is different, how can i retrieve all data from all 4 tables which that user had created?
You can use nested eager loading:
$departments = Department::where('user_id', $id)
->with('positions', 'positions.employees')
->get();
Another way is to build simple queries:
$departments = Department::where('user_id', $id)->get();
$positions = Position::whereIn('department_id', $departments->pluck('id'));
$employees = Employee::whereIn('position_id', $positions->pluck('id'));

Save on different tables using Eloquent Laravel Relationships

Hi I have 3 tables "temp_quotes", "quotes" and "quote_items"
Every quote generated goes to the "temp_quotes" table at first.
At the end of my multi-step form I need the following:
1- Receive all the quotes from the "temp_quotes" table, send it to the "quote_items" table and delete it from the "temp_quotes" table
2- Generate a new entry in the "quotes" table referencing the "users" table and
the "quote_items" table
Here is all the data and how needs to be save/removed on the tables:
temp_quotes Table
ID
BRAND
MODEL
YEAR
quotes Table:
ID
USER_ID
QUOTE_ID
quote_items Table:
ID
QUOTE_ID
BRAND
MODEL
YEAR
Any help will be appreciated.

Resources