好像有人也发现了这个问题,提了 issue Eloquent - nested relations lost when calling refresh on model,Dries Vints(Laravel 项目维护者)回复是 “这似乎是一个问题而不是 bug,建议到社区讨论”。我想了下,觉得他是对的,因为 with 上面附带的关联时作用于那次查询的,跟查询出来的模型没有关系。不看查询只看...
As you have already learned, working with many-to-many relations requires the presence of an intermediate table. Eloquent provides some very helpful ways of interacting with this table. For example, let's assume our User object has many Role objects that it is related to. After accessing this...
You may also insert related models when working with many-to-many relations. Let's continue using our User and Role models as examples. We can easily attach new roles to a user using the attach method:Attaching Many To Many Models1$user = User::find(1); 2 3$user->roles()->attach...
For example, you might add a featuredPosts method to a User model which constrains the broader posts relationship with an additional where constraint:<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; class User extends Model { ...
Added ability to define "with" relations as a nested array (#42690) Added ability to set backoff in broadcast events (#42737) Added host(), httpHost(), schemeAndHttpHost() to Request (#42797) Allow invokable rules to push messages to nested (or other) attributes (#42801) Adds compi...
//设置这种用Book::with('author.contacts')这种嵌套预加载的约束条件 $results = $this->addNestedWiths($name, $results); $results[$name] = $constraints; } return $results; } public function get($columns = ['*']) { $builder = $this->applyScopes(); ...
namespaceIlluminate\Database\Eloquent;...classBuilderimplementsBuilderContract{...publicfunctionwhere($column,$operator=null,$value=null,$boolean='and'){if($columninstanceofClosure&&is_null($operator)){$column($query=$this->model->newQueryWithoutRelationships());$this->query->addNestedWhereQuery(...
Fix blade tag issue with nested calls (#45764) Fix infinite loop in blade compiler (#45780) Fix ValidationValidator not to accept terminating newline (#45790) Fix stubs publish command generating incorrect controller stubs (#45812) fix: normalize route pipeline exception (#45817) Fix Illuminat...
public static function with($relations) public static function destroy($ids) public static function query() public static function resolveConnection($connection = null) public static function getConnectionResolver() public static function setConnectionResolver(Resolver $resolver) ...
returnarray_key_exists($key,$this->relations); } 可以看到,关联的加载带有缓存,laravel首先会验证当前关联关系是否已经被加载,如果加载过,那么直接返回缓存结果。 protectedfunctiongetRelationshipFromMethod($method) { $relation=$this->$method(); if(!$relationinstanceofRelation){ ...