Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

330
Visualizações
Laravel: Not getting required data

I am a Laravel newbie so I don't understand why this query is giving me only one user result instead of all entries containing posts.title or posts.description. This should be a search expression (%for%) and use the specific category (get).

DB::table('posts')
->join('users', 'users.id', '=', 'posts.user_id')
->join('locations', 'users.id', '=', 'locations.id')
->join('main_categories', 'main_categories.id', '=', 'posts.main_category_id')
->select('posts.*', 'users.*', 'locations.address', 'main_categories.name as cat', DB::raw('ROUND((6371 * ACOS(COS(RADIANS(31.430443800000003)) * COS(RADIANS(locations.lattitude)) * COS(RADIANS(locations.longitude) - RADIANS(74.280726)) + SIN(RADIANS(31.430443800000003)) * SIN(RADIANS(locations.lattitude)))),2) AS DISTANCE'))
->where([['posts.title','like','%for%'],['main_categories.name', '=', 'get']])
->orwhere([['posts.description','like','%for%'],['main_categories.name', '=', 'get']])
->get();

I am trying to convert this MySQL query into Laravel format. This MySQL query gives me 3 rows, 2 of one user and 1 row of another user. But in Laravel I only get one user row.

SELECT
  posts.title,
  posts.description,
  posts.sub_category_id,
  posts.image,
  posts.created_at,
  posts.main_category_id,
  posts.user_id,
  locations.address,
  ROUND(
    (
      6371 * ACOS(
        COS(RADIANS(31.430443800000003)) * COS(RADIANS(locations.lattitude)) * COS(
          RADIANS(locations.longitude) - RADIANS(74.280726)
        ) + SIN(RADIANS(31.430443800000003)) * SIN(RADIANS(locations.lattitude))
      )
    ),
    2
  ) AS DISTANCE
FROM
  posts
INNER JOIN
  users ON posts.user_id = users.id
INNER JOIN
  locations ON users.location_id = locations.id
INNER JOIN
  main_categories ON main_categories.id = posts.main_category_id
WHERE
  (
    (
      posts.title LIKE '%for%' OR posts.description LIKE '%for%'
    ) AND(main_categories.name = 'Get')
  )
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You use invalid join in your Laravel query. Instead of

->join('locations', 'users.id', '=', 'locations.id')

it should be

 ->join('locations', 'users.location_id', '=', 'locations.id')

because in your raw query you use:

INNER JOIN
  locations ON users.location_id = locations.id
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda