Laravel 5.5支点加入让枢值与主要的MySQL结果
我希望有人能帮助Laravel 5.5支点加入让枢值与主要的MySQL结果
我想在过去的3天加入透视表上mysqlquery
基本上我选择用户,其中一个用户有多个子类别。
所以在我的“ - 行业标准的关系本质上说,一个用户拥有多个子类
但因为我使用RAW选择,我不能选择/使用的关系。相反,我必须使用联接。
这是我 - 行业标准表
Column Type Comment id int(10) unsigned Auto Increment main_category_id int(10) unsigned [0] category_name varchar(100) created_at timestamp NULL updated_at timestamp NULL
,这是我的数据透视表
Column Type Comment user_id int(10) unsigned sub_category_id int(10) unsigned
这是我的SQL查询
$users= DB::table('users') ->select('users.*', 'user_options.*', DB::raw(' branches.*, professional_profiles.tags, ' . $lat . ' AS latpoint, ' . $lng . ' AS longpoint, ' . $radius . ' AS radius, ' . $measurement_number . ' AS distance_unit, ( ' . $measurement_number . ' * DEGREES( ACOS( COS(RADIANS(' . $lat . ')) * COS(RADIANS(branches.lat)) * COS(RADIANS(' . $lng . ' - branches.lng)) + SIN(RADIANS(' . $lat . ')) * SIN(RADIANS(branches.lat)) ) ) ) AS distance '), 'users.id AS id') ->leftJoin('branches', 'users.branch_id', '=', 'branches.id') ->leftJoin('user_options', 'user_options.user_id', '=', 'users.id') ->leftJoin('professional_profiles', 'professional_profiles.user_id', '=', 'users.id') ->where('user_options.professional', '>', 0) ->where('users.branch_id', '>', 0) ->where(function ($x) use ($term) { $x->where('branches.branch_name', 'like', '%' . $term . '%') ->orWhere('branches.branch_city', 'like', '%' . $term . '%') ->orWhere('users.firstname', 'like', '%' . $term . '%') ->orWhere('users.lastname', 'like', '%' . $term . '%') ->orWhere('professional_profiles.tags', 'like', '%' . $term . '%'); }) ->having('distance', 'orderBy('distance', 'asc') ->limit(50) ->get();
,这是我的结果
[ { id: 4, profile_id: 2, branch_id: 3, prefix: "dr", firstname: "SWK1", lastname: "Doe", email: "[email protected]", mobile_no: "811692244", password: "$2y$10$LzkPwc2TZu/.UzB.0mYJ", avatar: "123.jpg", remember_token: "wF33ShLirtvS3mIYJpmg5skVVoohGJCS7v", created_at: "2017-10-12 09:32:05", updated_at: "2017-10-12 09:32:05", provider: null, provider_id: null, user_id: 4, profile_administrator: 0, branch_administrator: 0, professional: 1, branch_name: "Swakopmund 1", branch_address_1: "14 Backer St", branch_address_2: null, branch_city: "Swakopmund", branch_state: null, branch_zip: "9000", branch_country: "NA", branch_phone: "77777", main_image: null, lat: -22.67, lng: 14.53, description: "Swakopmund 1", tags: "Doctors,Dietician,General Practitioner", latpoint: "-22.5608807", longpoint: "17.0657549", radius: 500, distance_unit: "111.045", distance: 260.210154298872 } ]
所以基本上的问题是要参加在用户表 - 行业标准表,通过利用由设定的值数据透视表,而不依赖于雄辩关系表,而是仅使用sql查询。
由于一个用户有许多 - 行业标准,这将是巨大的回报 - 行业标准作为数组值主SQL查询
请加入,如果有人能在正确的方向引导我
Thanx
我有类似的情况,我Query Scope连同我的数据透视表一对多关系。在我的情况下,用户有多个组,我需要将这些数据与用户对象一起提取,而无需额外查询或没有JOIN。 查看Query scope
和一对多和许多对许多在Laravel Doc枢轴。
如果你想获取使用pivote表中的数据,这里是例子
用户模型:
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'username', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function groups()
{
return $this->belongsToMany('App\Group', 'user_groups',
'user_id', 'group_id');
}
public function scopeDetail($query)
{
return $query->with('groups');
}
}
组型号:
class Group extends Model
{
protected $fillable = [
'dn', 'cn', 'description',
];
}
在上面的用户模型,请参阅return $this->belongsToMany('App\Group','user_groups', 'user_id', 'group_id');
,其中user_groups是我的数据透视表,它定义了用户和组之间的关系。 group_id
和user_id
是pivotote表中的字段。使用上述architechture
立即获取数据(在控制器):
User::where(.....)->detail()->first();
其中detail()
在用户模型定义为scopeDetail
我的范围。注意:必须附上scope
前缀。这将为用户提供用户所属的数组中的所有组,因此无论何时以JSON查看数据,都可以以正确的方式查看结构。
使用上述方法,我的用户对象具有用户所属的所有组。
额外
如果您的用户模型(用户)与其他型号太多,那么你可以包括所有通过模型类定义范围为
............
//..............
public function profile()
{
return $this->belongsToMany('App\Profile', 'user_id');
}
public function data1()
{
return $this->belongsToMany('App\Data1', 'user_id');
}
public function groups()
{
return $this->belongsToMany('App\Group', 'user_groups',
'user_id', 'group_id');
}
//Defining query scope................
public function scopeDetail($query)
{
return $query->with('groups','profile','data1');
//to fetch user with this scope use User::where(.....)->detail()->get(); notice there is not scope prefix while using the scope
}
........
........
嘿,它的这种你的详细回复。我会在今天晚上尝试一下这个方法,然后回来。非常感谢。 –
走向疯狂>>我注意到,我忽略了一些可以节省很多时间的东西。哎呀!我会回发;) –
这是我如何获取属于使用查询范围的用户的所有组。始终尝试利用Laravel Native功能。让我知道我是否可以为您的问题发布代码,但是如果您自己解决问题,那么它会更好。 – webDev
我有完全一样的情况的,我已经使用范围和()方法来获取与用户关联的子类别数组。我会发现我的代码和发布,同时你可以看看https://*.com/questions/26178315/laravel-use-scope-in-models-with-relation – webDev