草庐IT

eloquent

全部标签

php - 使用 artisan 迁移时流明错误

我是Lumen的新手,当我尝试使用时:phpartisanmigrate我收到以下错误。[PDOException]SQLSTATE[HY000][2002]Nosuchfileordirectory如果我将“localhost”更改为“127.0.0.1”,我会收到此错误:[PDOException]SQLSTATE[HY000][2002]Connectionrefused如果我使用:phpartisanmigrate--database=Lumen我得到:[InvalidArgumentException]Database[Lumen]notconfigured.这是我的.env

php - Laravel 5.1 从原始数据库(或只是一个数组)创建集合或关联数组

给定以下表结构,我想返回一个Eloquent集合或至少将原始数据库结果转换为关联数组以便于迭代。建筑物+----+---------------+|id|building_name|+----+---------------+|1|Building1||2|Building2|+----+---------------+房间+----+-----------+-------------+|id|room_name|building_id|+----+-----------+-------------+|1|Room1|1||2|Room2|1||3|Room3|2|+----+----

php - Laravel Eloquent 多列 IN 子句

我最近开始使用Laravel进行开发,我的第一个项目是一个车辆预订应用程序。我似乎做得很好,但是我无法尝试使用Eloquent显示当前预订。系统的工作原理是将车辆添加到vehicles表,将员工添加到employees表。使用employee_vehicle创建与数据透视表的多对多关系。员工+------------+-------------+------+-----+---------+----------------+|Field|Type|Null|Key|Default|Extra|+------------+-------------+------+-----+------

php - Laravel 5 调用未定义的方法 Illuminate\Database\Eloquent\Collection::attach()

我正在使用具有多对多关系的laravel5。下面是我的代码。文章.phpnamespaceApp;useIlluminate\Database\Eloquent\Model;useCarbon\Carbon;classArticleextendsModel{protected$fillable=['title','body','published_at','user_id'];protected$dates=['published_at'];publicfunctionsetPublishedArAttribute($date){Carbon::createFromFormat('Y-

mysql - Eloquent - 按 "from date"到 "to date"分组,间隔一天

我有一个Reservation模型,它有date_from和date_to字段。如何按一天的时间间隔对预订进行分组,以便了解在特定日期有多少预订是活跃的?例子Reservation::groupBy('...')->get()输出:reservations==================qty|date------------------3|2000-01-015|2000-01-022|2000-01-03... 最佳答案 首先将“date_from”和“date_to”添加到模型上的$dates数组protected$dat

mysql - Laravel 多对多关系OrderBy

我在多对多关系中有2个模型。假设用户和角色。我想根据角色中某个字段的ASC/DESC对我的用户进行排序。我的用户和角色类:classUserextendsModel{publicfunctionroles(){return$this->belongsToMany('App\Role','role_user');}classRoleextendsModel{publicfunctionusers(){return$this->belongsToMany('App\User','role_user');}我可以对每个用户中的角色进行排序,但我无法对用户进行排序$query=User::wi

php - 奇怪的 Laravel 错误(从范围加入)错误的 ID 列

这是错误输出SQLSTATE[23000]:Integrityconstraintviolation:1052Column'id'inwhereclauseisambiguous(SQL:update`table1`innerjoin`table2`on`table1`.`type_id`=`table2`.`id`set`type_id`=2,`updated_at`=2015-01-0919:36:16where`table1`.`site_id`=1and`id`=1)在本文中,我只是为了提出这个问题而编辑了表名。所讨论的查询是设置表中项目的“类型”,table1具有id,它与t

php - Laravel 5 数据透视表问题

我不确定如何解释我的问题,但我会试一试。我正在使用Laravel5,我有3个表:页面语言language_page这是我的模型的样子:页面模型:publicfunctionlanguages(){return$this->belongsToMany('App\Language','language_page')->withPivot('title','content','slug','meta_title','meta_description')->orderBy('main','desc');}语言模型:publicfunctionpages(){return$this->belon

php - 使用连接检索数据透视表和相关数据的 Eloquent 查询

在我的Controller中,我有一个这样的查询:$userinfos=User::with('skills');$userinfos->with('positions');$userinfos=$userinfos->get();这很好用,在我看来,我得到的职位名称是这样的:@forelse($userinfos[0]->positionsas$key=>$position){{$position->name}}@emptyNopositionlisted@endforelse我有另一个像这样检索相关公司数据:$positions=\DB::table('positions')->s

mysql - 在mysql中实现多对多关系

我有两个表1.矩形(rectId,xPos,yPos,height,width)2.规模(scaleId,someothercols...)现在矩形中的每一列都可以关联“0到1”的比例。即xPos可以有一个比例尺,yPos可以有一个比例尺等等。所以一般来说,一个矩形可以有多个尺度。此外,上述所有列也可以没有比例引用,在这种情况下,它们将具有静态值,即xPos=50、yPos=60等等。一个比例尺也可以被多个矩形引用。在sql中实现这个的最好方法是什么我想到的方法是有一个连接表Rectangle_scale和rect_column属性exg:矩形比例尺|rectScaleId|rectI