Avoid duplicate record in same table
Avoid duplicate record in multiple table while leftjoin
Avoid duplicate record in same table
DB::raw('MAX(social_url.id) as max_id'),
>select(
'social_url.user_id',
DB::raw('MAX(social_url.id) as max_id'),
'social_url.*',
'addcarts.influencer_admin_id',
'addcarts.cart_socials',
'addcarts.admin_id',
'countries.country_name',
'states.state_name',
'cities.city_name'
)
$data = DB::connection('payments')->table("social_url")
->leftJoin('countries', 'social_url.country_id', '=', 'countries.country_id')
->leftJoin('states', 'social_url.state_id', '=', 'states.state_id')
->leftJoin('addcarts', function($join) use ($login_email) {
$join->on('social_url.user_id', '=', 'addcarts.influencer_admin_id')
->where('addcarts.admin_email', '=', $login_email);
})
->leftJoin('cities', 'social_url.city_id', '=', 'cities.city_id')
->where('social_url.user_email', '<>', $login_email)
->select(
'social_url.user_id',
DB::raw('MAX(social_url.id) as max_id'),
'social_url.*',
'addcarts.influencer_admin_id',
'addcarts.cart_socials',
'addcarts.admin_id',
'countries.country_name',
'states.state_name',
'cities.city_name'
)
->groupBy('social_url.user_id')
->orderBy('max_id', 'desc')
->get();
Avoid duplicate record in multiple table while leftjoin
apply multiple condition
leftJoin('addcarts', function($join) use ($login_email) {
$join->on('social_url.user_id', '=', 'addcarts.influencer_admin_id')
->where('addcarts.admin_email', '=', $login_email);
})
$data =DB::connection('payments')->table("social_url")
->leftJoin('countries', 'social_url.country_id', '=', 'countries.country_id')
->leftJoin('states', 'social_url.state_id', '=', 'states.state_id')
->leftJoin(DB::raw('(SELECT MAX(id) AS max_id, influencer_admin_id, cart_socials, admin_id FROM addcarts GROUP BY influencer_admin_id) addcarts'), function($join)
{
$join->on('social_url.user_id', '=', 'addcarts.influencer_admin_id');
})
->leftJoin('cities', 'social_url.city_id', '=', 'cities.city_id')
->select(
'social_url.user_id',
DB::raw('MAX(social_url.id) as max_id'),
'social_url.*',
'addcarts.influencer_admin_id',
'addcarts.cart_socials',
'addcarts.admin_id',
'countries.country_name',
'states.state_name',
'cities.city_name'
)
->groupBy('social_url.user_id')
->orderBy('id', 'desc')
->get();
other way
$login_email = Auth::check() ? Auth::user()->email : null;
->where('social_url.user_email', '<>', $login_email)
->whereNotNull('social_url.social_price')
$filteredData = DB::connection('payments')->table("social_url")
->leftJoin('countries', 'social_url.country_id', '=', 'countries.country_id')
->leftJoin('states', 'social_url.state_id', '=', 'states.state_id')
->leftJoin(DB::raw('(SELECT MAX(id) AS max_id, influencer_admin_id, cart_socials, admin_id FROM addcarts GROUP BY influencer_admin_id) addcarts'), function($join)
{
$join->on('social_url.user_id', '=', 'addcarts.influencer_admin_id');
})
->where('social_url.user_email', '<>', $login_email)
->leftJoin('cities', 'social_url.city_id', '=', 'cities.city_id')
->whereNotNull('social_url.social_price')
->where('social_url.city_id', $city_id)
->leftJoin('users', 'social_url.user_id', '=', 'users.id');
$filteredData->where(function ($query) use ($convertedArray) {
foreach ($convertedArray as $socialSite) {
$query->orWhereNotNull("social_site->$socialSite");
}
});
Top comments (0)