草庐IT

eloquent

全部标签

php - 创建一个包含关系的 Eloquent 对象

我对opps和laravel都很陌生因此,要将值插入到具有OneToOne关系的users和profiles表中,这是我的store()方法看起来像publicfunctionstore(Requests\StoreNewUser$request){//crateanobjctofusermodel$user=new\App\User;//nowrequestandassignvalidatedinputtoarrayofcolumnnamesinusertable$user->first_name=$request->input('first_name');$user->last_n

php - WhereHas Laravel 中的关系计数条件是什么

我很难理解WhereHas中的关系计数条件.文档页面没有关于它的讨论,但是APIpage谈论它。引用自API。Builder|BuilderwhereHas(string$relation,Closure$callback,string$operator='>=',int$count=1)Addarelationshipcountconditiontothequerywithwhereclauses.示例Resource模型与ResourceCategory有多对多关系publicfunctioncategories(){return$this->belongsToMany('Reso

php - 你能迭代一个 Eloquent 对象的属性吗?

如果我有例如:$project=newProject();//ProjectisaclassthatextendsEloquent$project->title;$project->something;是否可以迭代这些属性...像这样:foreach($projectas$key=>$value){echo$key;}我正在尝试这样做以实现编辑Eloquent模型的工作单元 最佳答案 您可以使用toArray()方法:foreach($project->toArray()as$key=>$value){echo$key;}http:

php - Laravel 在 Eloquent 修改器中保存多对多关系

我有2个具有多对多关系的模型。我希望能够使用一个id数组设置一个特定的属性,并在mutator中建立这样的关系:tags()->get(['tag_id']);foreach($tagsas$tag){$tag_ids[]=$tag->tag_id;}return$tag_ids;}publicfunctionsetTagsAttribute($tag_ids){foreach($tag_idsas$tag_id){$this->tags()->attach($tag_id);}}publicfunctiontags(){return$this->belongsToMany('Tag'

php - 消息为 '... must return a relationship instance.' 的 LogicException

尝试在整个互联网上搜索此错误,但一切都是徒劳,所以作为最后的手段,我在StackOverflow上创建了一个问题。我设置了两个简单的Eloquent模型:1。Teacher(扩展了Authenticable)——因为我正在为系统使用MultiAuth。2。GeneralNotice(扩展Eloquent/Model)app\Teacher.phppublicfunctiongeneralNotices(){$this->hasMany('App\Modules\GeneralNotice');}app\Modules\GeneralNotice.phppublicfunctiontea

php - 如何在 Laravel 中将一个实例与多个实例分离 | Eloquent 数据透视表?

数据透视表:“bonus_circle”能够拥有多个具有相同circle_id和bonus_id的项目。换句话说,同一个圈子可以有多个相同的奖金。使用$circle->bonuses()->detach($id)删除所有实例。我需要它只分离一个实例。有谁知道解决这个问题的方法吗? 最佳答案 我搜索了一个多星期才找到这个问题的答案。我不能用你的代码作为例子,因为那里还不够我继续下去,但我会用我的代码向你展示我在LaravelIRC聊天中从Kindari那里收到的答案(谢谢你)。我有用户、角色和帐户。一位用户可以在一个或多个帐户上拥有一

php - 阐明如何在 Laravel 的 Eloquent ORM 中设置一对多关系

好的,我正在研究Laravel4文档以在两个模型之间建立一对多关系。显然,一侧应该使用hasMany()。但是对于另一边,我应该使用hasOne还是belongsTo?有关系吗?有什么区别?为什么两者都存在?我原以为hasOne将用于一对一关系,而belongsTo将用于一对多的一侧。但是在文档中,为了在此处插入相关模型:http://laravel.com/docs/eloquent#inserting-related-models他们正在使用save(),它似乎只存在于hasOne和hasMany关系中,而不存在于belongsTo。看起来belongsTo使用associate(

php - Laravel save() 方法返回 true 但不更新记录

我正在使用Eloquent来更新我的表Opportunity,机会模型hasMany('App\Csf','opportunityID');}publicfunctionbenefits(){return$this->hasMany('App\Benefit','opportunityID');}publicfunctionrisks(){return$this->hasMany('App\Risk','opportunityID');}publicfunctionprojects(){return$this->belongsTo('App\Project','projectID');

php - 在 Laravel 5 中使用模型事件监听器

我想确保我在Laravel5中正确使用了模型事件监听器并且我没有搞砸任何事情(监听器与处理程序?)。我的解决方案工作正常,但我想知道我是否根据Laravel5的概念和约定开发。目标:保存模型时,始终将$issue->status_id设置为某个值。在app\Providers\EventServiceProvider.php中在app\Handlers\Events\SetIssueStatus.php中firstOrFail();}else{$issueStatus=IssueStatus::where(somethingAnother)->firstOrFail();}//issu

php - whereBetween Dates in laravel 4 Eloquent

我有这样的疑问SELECT*FROM`sp_price`WHERE(`from_date`between'2014-08-15'and'2014-09-18')||(`to_date`between'2014-08-15'and'2014-09-18')现在我如何在laravel4中转换这个查询。我使用Eloquent 最佳答案 DB::table(sp_price)->whereBetween('from_date',array('2014-08-15','2014-08-18'))->orWhereBetween('to_dat