【问题标题】:Laravel 5 cannot update relationshipLaravel 5 无法更新关系
【发布时间】:2017-07-18 13:13:03
【问题描述】:

我已经尝试了无数个小时,但在更新模型关系时仍然遇到问题,我最接近的是“方法填充不存在”。错误。

上市模式:

 class Listing extends Model
    {       
        protected $fillable = [
            'uid', 'start_date',...........
        ];     

        public function locations()
        {
            return $this->hasMany('App\ListingLocation');
        }        
    }

位置(与列表的关系 - hasMany):

class ListingLocation extends Model
{

    protected $fillable = [
        'listing_id', 'location',
    ];

    public function listing()
    {
        return $this->belongsTo('App\Listing');
    }
}

这将返回我的模型和关系,我可以使用 dd($listing) 进行查看

$listing = Listing::with('locations')->findOrFail($id);

这将更新我的列表模型,我可以在再次调用 dd($listing) 后看到更改

$listing->fill($array);

但是,当我尝试按照以下方式填充关系时,我得到“方法填充不存在”。

$listing->locations->fill($array['locations']);

如何在调用 $listing->push(); 之前成功更新关系?

【问题讨论】:

  • LocationshasMany 关系,因此它将返回一个集合,而不是单个模型。您必须选择要更新的位置。
  • 我想你正在寻找同步方法:$listing->locations()->sync($array['locations']);
  • 因为这是一对多的关系,同步不能正常工作?调用同步时出现错误:调用未定义的方法 Illuminate\\Database\\Query\\Builder::sync()

标签: php laravel-5


【解决方案1】:

将您的位置更改为单个记录,而不是集合

例如:

$listings->locations->first()->fill($array['locations']);

使用 foreach 填充每条记录

@foreach($listings->locations as $location)
$location->fill(do_something);
@endforeach

【讨论】:

  • 这似乎也不起作用,因为它试图填充第一条记录中的位置数组。即使我将其更新为 .....->fill($array['locations'][0]);在进行转储和死亡时它仍然不会更新属性
  • 还注意到如果我使用更新而不是填充,这可行,但是我想覆盖该模型的所有位置,而不仅仅是第一个。同步似乎是我想要的,但不确定它是否适用于一对多关系
  • 我已经更新了我的答案,同步适合多对多...不是一对多
【解决方案2】:

我最终创建了一个新类来扩展 hasMany,它允许我按照 https://laracasts.com/discuss/channels/general-discussion/syncing-one-to-many-relationships 的 alexweissman 使用同步。

论坛摘录:

use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * @link https://github.com/laravel/framework/blob/5.4/src/Illuminate/Database/Eloquent/Relations/HasMany.php
 */
class HasManySyncable extends HasMany
{
    public function sync($data, $deleting = true)
    {
        $changes = [
            'created' => [], 'deleted' => [], 'updated' => [],
        ];

        $relatedKeyName = $this->related->getKeyName();

        // First we need to attach any of the associated models that are not currently
        // in the child entity table. We'll spin through the given IDs, checking to see
        // if they exist in the array of current ones, and if not we will insert.
        $current = $this->newQuery()->pluck(
            $relatedKeyName
        )->all();

        // Separate the submitted data into "update" and "new"
        $updateRows = [];
        $newRows = [];
        foreach ($data as $row) {
            // We determine "updateable" rows as those whose $relatedKeyName (usually 'id') is set, not empty, and
            // match a related row in the database.
            if (isset($row[$relatedKeyName]) && !empty($row[$relatedKeyName]) && in_array($row[$relatedKeyName], $current)) {
                $id = $row[$relatedKeyName];
                $updateRows[$id] = $row;
            } else {
                $newRows[] = $row;
            }
        }

        // Next, we'll determine the rows in the database that aren't in the "update" list.
        // These rows will be scheduled for deletion.  Again, we determine based on the relatedKeyName (typically 'id').
        $updateIds = array_keys($updateRows);
        $deleteIds = [];
        foreach ($current as $currentId) {
            if (!in_array($currentId, $updateIds)) {
                $deleteIds[] = $currentId;
            }
        }

        // Delete any non-matching rows
        if ($deleting && count($deleteIds) > 0) {
            $this->getRelated()->destroy($deleteIds);

            $changes['deleted'] = $this->castKeys($deleteIds);
        }

        // Update the updatable rows
        foreach ($updateRows as $id => $row) {
            $this->getRelated()->where($relatedKeyName, $id)
                 ->update($row);
        }

        $changes['updated'] = $this->castKeys($updateIds);

        // Insert the new rows
        $newIds = [];
        foreach ($newRows as $row) {
            $newModel = $this->create($row);
            $newIds[] = $newModel->$relatedKeyName;
        }

        $changes['created'][] = $this->castKeys($newIds);

        return $changes;
    }


    /**
     * Cast the given keys to integers if they are numeric and string otherwise.
     *
     * @param  array  $keys
     * @return array
     */
    protected function castKeys(array $keys)
    {
        return (array) array_map(function ($v) {
            return $this->castKey($v);
        }, $keys);
    }

    /**
     * Cast the given key to an integer if it is numeric.
     *
     * @param  mixed  $key
     * @return mixed
     */
    protected function castKey($key)
    {
        return is_numeric($key) ? (int) $key : (string) $key;
    }
}

然后你可以在你的模型类中重写 Eloquent 的 hasMany 方法:

 /**
     * Overrides the default Eloquent hasMany relationship to return a HasManySyncable.
     *
     * {@inheritDoc}
     */
    public function hasMany($related, $foreignKey = null, $localKey = null)
    {
        $instance = $this->newRelatedInstance($related);

        $foreignKey = $foreignKey ?: $this->getForeignKey();

        $localKey = $localKey ?: $this->getKeyName();

        return new HasManySyncable(
            $instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey
        );
    }

    /**
     * Get all of a user's phone numbers.
     */
    public function phones()
    {
        return $this->hasMany('App\Phone');
    }

现在,您在此模型上拥有的任何 hasMany 关系都可以使用同步方法:

$user->phones()->sync([
    [
    'id' => 21,
        'label' => "primary",
        'number' => "5555551212"
    ],
    [
    'id' => null,
        'label' => "mobile",
        'number' => "1112223333"
    ]
]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2015-08-06
    相关资源
    最近更新 更多