You are telling it to look for job_offer_uuid on the users table and the users table doesn't have that column.
How to access to user data through intermediate table (Relationships)
For a job posting application, I have three tables, which shortly are defined as:
-
users: Just laravel normal users table id as primary key
-
job_offers with field uuid as primary key with field user_id as external key
-
applications id as primary key job_offer_uuid as external key
Because I need to notify job_offer owner any time that application is registered, I'm trying to create a hasOneThrough relationship from applications to users, but without success for the moment.
Based on my understanding eloquent documentation (https://laravel.com/docs/8.x/eloquent-relationships#has-one-through), my actual code in Application model is:
public function publisher()
{
return $this->hasOneThrough(JobOffer::class, User::class, 'job_offer_uuid', 'user_id'');
}
But it fires an SQL error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.job_offer_uuid' in 'field list' (SQL: select `job_offers`.*, `users`.`job_offer_uuid` as `laravel_through_key` from `job_offers` inner join `users` on `users`.`id` = `job_offers`.`user_id` where `users`.`job_offer_uuid` in (1))
I can get results using pure SQL with a sentence like this (not optimized yet):
select * from `applications` inner join job_offers on `applications`.`job_offer_uuid` = `job_offers`.`uuid` join users on job_offers.user_id = users.id where `applications`.`job_offer_uuid` = '8bd1327a-8079-41e6-9fbe-63da9dd74c1f'
Any clarification should be truly appreciate. Regards!
@faridsilva Imagine trying to write this kind of queries with the Query builder or eloquent
SELECT
(SELECT GROUP_CONCAT(a.id ORDER BY a.id SEPARATOR ',')
FROM authors a, author_books ab
WHERE a.id = ab.author_id
AND ab.book_id = b.id) author_id,
(SELECT GROUP_CONCAT(CONCAT(a.last_name, ', ', a.first_name)
ORDER BY a.last_name, a.first_name SEPARATOR ' & ')
FROM authors a, author_books ab
WHERE ab.author_id = a.id
AND ab.book_id = b.id) author_name,
(SELECT GROUP_CONCAT(LOWER(CONCAT(a.last_name, '-', a.first_name))
ORDER BY a.last_name, a.first_name SEPARATOR ' & ')
FROM authors a, author_books ab
WHERE ab.author_id = a.id
AND ab.book_id = b.id) author_slug,
b.id,
b.title,
b.title_slug,
s.serie,
s.start_year,
s.serie_slug,
b.part,
b.pages,
b.published,
e.edition,
p.publisher,
f.format,
g.genre,
c.condition,
b.blurb
FROM books b,
series s,
editions e,
publishers p,
genres g,
formats f,
conditions c
WHERE b.serie_id = s.id
AND b.edition_id = e.id
AND b.publisher_id = p.id
AND b.format_id = f.id
AND b.genre_id = g.id
AND b.condition_id = c.id
I wouldn't even know where to begin.
Please or to participate in this conversation.