Laravel: 未获取所需数据
Laravel: Not getting required data
我是 Laravel 新手,所以我不明白为什么这个查询只给我一个用户结果,而不是所有包含 posts.title
或 posts.description
的条目。这应该是一个搜索表达式 (%for%
) 并使用特定类别 (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();
我正在尝试将此 MySQL 查询转换为 Laravel 格式。这个 MySQL 查询给了我 3 行,一个用户的 2 行和另一个用户的 1 行。但是在 Laravel 我只得到一个用户行。
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')
)
您在 Laravel 查询中使用了无效的连接。而不是
->join('locations', 'users.id', '=', 'locations.id')
应该是
->join('locations', 'users.location_id', '=', 'locations.id')
因为在您的原始查询中您使用:
INNER JOIN
locations ON users.location_id = locations.id
我是 Laravel 新手,所以我不明白为什么这个查询只给我一个用户结果,而不是所有包含 posts.title
或 posts.description
的条目。这应该是一个搜索表达式 (%for%
) 并使用特定类别 (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();
我正在尝试将此 MySQL 查询转换为 Laravel 格式。这个 MySQL 查询给了我 3 行,一个用户的 2 行和另一个用户的 1 行。但是在 Laravel 我只得到一个用户行。
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')
)
您在 Laravel 查询中使用了无效的连接。而不是
->join('locations', 'users.id', '=', 'locations.id')
应该是
->join('locations', 'users.location_id', '=', 'locations.id')
因为在您的原始查询中您使用:
INNER JOIN
locations ON users.location_id = locations.id