我有Groups,Users,GroupPositions,table.
关系:
用户所属的ToMany Groups
Users areToMany GroupPositions
Group hasMany GroupPositions
//users
public function groupPositions() {
return $this->belongsToMany(
'App\Models\GroupPositionView',
'vw_group_members',
'userId',
'groupPositionId');
}
public function groups() {
return $this->belongsToMany(
'App\Models\GroupView',
'vw_group_members',
'userId',
'groupId');
}
Run Code Online (Sandbox Code Playgroud)
我需要选择一个组,其中包含成员及其在该特定组中的相应位置.成员可以是具有不同职位的不同组的一部分.他可以成为一个团队的领导者,也可以是不同团队的经理.
目前,我有这个查询:
$group = GroupView::where('tag', $tag)
->with(['user.groupPositions'])
->get();
Run Code Online (Sandbox Code Playgroud)
它给了我所有成员的所有成员的所有职位.我想将其过滤到特定的组.
我也有这个:
$groupId = 1;
$group = GroupView::with(['users.groupPositions' => function($query) use($groupId) {
$query->whereHas('group', function($query) use($groupId) {
$query->where('groupId', $groupId);
});
}])
->where('groupId', $groupId)
->firstOrFail();
Run Code Online (Sandbox Code Playgroud)
这个工作.但是,问题是我需要通过标签获取组.如果我将所有groupId更改为tag,则它不再起作用.可能是因为groupPositions和我的vw_group_members没有标记列.
所以我的问题是,是否可以通过标签过滤我的查询?
我有一个带有复合主键的表.我想创建一个迁移,删除该主键,并引入一个新的列'id',它是顺序的,将成为新的主键.
Schema::table('cart_line', function (Blueprint $table) {
$table->dropPrimary('cart_line_pkey');
$table->increments('id');
$table->primary('id');
});
Run Code Online (Sandbox Code Playgroud)
此代码抛出以下错误:
SQLSTATE[42P16]: Invalid table definition: 7 ERROR: multiple primary keys for table "cart_line" are not allowed (SQL: alter table "cart_line" add primary key ("id"))
Run Code Online (Sandbox Code Playgroud)
如果我直接执行以下SQL,则删除主键并执行上面的代码而不会出现错误:
ALTER TABLE cart_line DROP CONSTRAINT cart_line_pkey;
Run Code Online (Sandbox Code Playgroud)
这就是我尝试的原因
DB::statement('ALTER TABLE cart_line DROP CONSTRAINT cart_line_pkey;');
Schema::table('cart_line', function (Blueprint $table) {
$table->increments('id');
$table->primary('id');
});
Run Code Online (Sandbox Code Playgroud)
但是抛出了同样的错误.旧主键不会被删除,但在尝试创建新主键之前没有错误.
我正在使用laravel 5.5和postgres 9.6
我有一张products桌子和一张orders桌子。它们之间有很多对很多的关系,而product_order表是中介。现在,我有两个产品ID,并且想要选择包含两个产品ID的订单(如果存在)。如何用Laravel雄辩地做到这一点?
class Product extends Model
{
// ...
public function orders()
{
return $this->belongsToMany('App\Order', 'product_order');
}
}
class Order extends Model
{
// ...
public function products()
{
return $this->belongsToMany('App\Products', 'product_order');
}
}
Run Code Online (Sandbox Code Playgroud) 我知道以前曾经问过这个问题,但根据我的情况,我找不到一个有效的答案.
目前我有两个型号.App\JobStatus App\Customer
在模型App\JobStatus我有这个:
public function customer()
{
return $this->belongsTo('App\Customer');
}
Run Code Online (Sandbox Code Playgroud)
在模型App\Customer我有这个:
public function jobs()
{
return $this->hasMany('App\JobStatus', 'customer_id');
}
Run Code Online (Sandbox Code Playgroud)
'customer_id'是外键.然后我尝试从我的控制器中的Jobstatus访问客户,如下所示:
$testMePlease = JobStatus::first()->where('qb', '=', 1);
$testMePlease->customer;
Run Code Online (Sandbox Code Playgroud)
我曾尝试过这个.把它放在foreach循环中.我也试过$ testMePlease-> customer-> customer_name.Customer_name是表中的一列,我得到了同样的错误:"Undefined property:Illuminate\Database\Eloquent\Builder :: $ customer"
我有什么想法我做错了吗?
假设我有三个与外键链接的数据库,即玩家,信用和照片。
player
id | name | address
credit
id | player_id | credit_status
photo
id | player_id
Run Code Online (Sandbox Code Playgroud)
假设我想获得所有具有credit_status $ status的玩家,我会这样做:
$status = 'bar';
Player::with('photo','credit')->whereHas('credit', function ($q) use ($status) {
$q->where('credit_status', $status)->with('credit_status');
})->paginate(15);
Run Code Online (Sandbox Code Playgroud)
这将列出所有具有credit_status $ credit的玩家,但仍然列出该玩家的所有积分,而不管其状态如何。
输出类似于:
{
id: 1
name: Andrew
address: home
photo: {
id: 2
photo: image1
}
credit: {
[
{
id: 6
credit_status: foo,
id: 2
credit_status: bar
}
]
}
},
{
id: 2
name: Mark
address: home
photo: {
id: 5
photo: image4
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试获取与数据库中的应用程序相关的信息.
我正在返回一个带有如此口才的查询的视图:
$active_applications = Application::with('applicant', 'puppy')->where('kennel_id', '=', $user_kennel->id)->get();
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序模型中定义了一些关系,如下所示:
public function puppy(){
return $this->belongsTo('App\Puppy');
}
public function applicant(){
return $this->belongsTo('App\User');
}
Run Code Online (Sandbox Code Playgroud)
当视图加载时,我能够获得与'puppy'相关的信息.它正确检索.然而,申请人保持无效.
我在我的应用程序表中有一个名为"user_id"的列,我希望它在该列中使用value来搜索users表'id',并检索用户的信息.但是它保持为空.以下是有问题的变量的dd():
我错过了一些明显的东西吗 为什么要找回一个而不是另一个?
我有三个表:流,document_types和文档.Flows有许多属于文档类型的文档.
假设我需要选择所有具有属于特定文档类型列表的文档的流,例如,文档类型id在1,2,3和4中.换句话说,我只想选择包含文档的流以上所有文档类型ID.我应该如何使用逻辑/查询?
我的第一次尝试是,where in但它不能确保文档具有完全所有文档类型,它至少查询一个:
select * from flows where id in (
select flow_id from documents where document_type_id in (1, 2, 3, 4)
);
Run Code Online (Sandbox Code Playgroud)
我必须用Laravel Eloquent编写我的查询,但在发现正确的逻辑之后这将是微不足道的.
我在我的网站上为管理员构建了一个cms界面.除其他外,管理员可以使用表单添加\编辑用户信息.当我发送编辑表单时,我不断收到此错误:Column not found: 1054 Unknown column 'updated_at' in 'field list'这表明数据库更新正在尝试保存所有请求索引(包含其他表中列的值),而不仅仅是我正在尝试更新的索引.
我已设法将问题跟踪到一行$user_role->save();.上面的行做他们想做的事情(找到thr correcct user_role并改变它的值).
这是我的代码
模型
static public function update_user($request, $id){
$image_name = '';
if( !empty($request['profile_image']) && $request->hasFile('profile_image') && $request->file('profile_image')->isValid() ){
$file = $request->file('profile_image');
$image_name = date('Y.m.d.H.i.s') . '-' . $file->getClientOriginalName();
$request->file('profile_image')->move( public_path() . '/images/profile-images/' , $image_name);
$img = Image::make( public_path() . '/images/profile-images/' . $image_name );
$img->resize(370, null, function ($constraint) {
$constraint->aspectRatio();
});
$img->save();
}
$user = self::find($id);
$user->name = $request['name'];
$user->email = $request['email'];
$user->phone = $request['phone']; …Run Code Online (Sandbox Code Playgroud)如何在laravel雄辩的对象中选择字段
User::select('username', 'firstname', 'lastname')->with(['periods' => function($){
$->select('jobtitle')->orderBy('start_date', 'desc')->limit(1);
}])->paginate(50);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我可能得到句点为空
我有以下查询
$objects = Object::where('user_id', auth()->user()->id)
->where('object_name','LIKE','%'.$request->search."%")
->get();
Run Code Online (Sandbox Code Playgroud)
这将返回包含关键字的当前用户的所有对象.这仅搜索object_name列中的关键字.
因为我想内同时进行搜索object_name和object_description,我增加了一个orWhere条款如下:
$objects = Object::where('user_id', auth()->user()->id)
->where('object_name','LIKE','%'.$request->search."%")
->orWhere('object_description','LIKE','%'.$request->search."%")
->get();
Run Code Online (Sandbox Code Playgroud)
现在,这会返回一个包含关键字的所有对象object_name或object_description,但它搜索整个用户群内.
我想要的是更改此查询,以便我可以在object_name或中搜索关键字object_description,但搜索范围必须限制为当前登录用户.
eloquent ×10
laravel ×10
php ×4
laravel-5 ×3
mysql ×2
relationship ×2
database ×1
postgresql ×1
sql ×1
subquery ×1