数据透视表中的laravel/雄辩的mutators/accessors [英] laravel/eloquent mutators/accessors in a pivot table

查看:90
本文介绍了数据透视表中的laravel/雄辩的mutators/accessors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我认为标题可以解释其中的大部分内容.让我们开始吧!

Well, I think the title explains most of it. Lets get right into it!

空白模型:

class Blank extends Eloquent 
{

    protected $table = 'blanks';

    protected $softDelete = true;

    protected $hidden = array();

    /**
     * Get associated jobs.
     *
     * @return mixed
     */
    public function jobs()
    {
        return $this->belongsToMany('Job')->withPivot('status', 'inventory', 'sizes', 'mill', 'po', 'location', 'ordered_at', 'expected_at', 'note')->withTimestamps();
    }

    /**
     * Blanks sizes accessor
     *
     * @return object
     */
    public function getSizesAttribute($value)
    {
        return json_decode($this->pivot->sizes);
    }

    /**
     * Blanks sizes mutator
     *
     * @return void
     */
    public function setSizesAttribute($value)
    {
        $this->pivot->attributes['sizes'] = json_encode($this->pivot->sizes);
    }

}

工作模式:

class Job extends Eloquent
{

    protected $table = 'jobs';

    protected $softDelete = true;

    protected $hidden = array();

    /**
     * Get associated blank.
     *
     * @return mixed
     */
    public function blanks()
    {
        return $this->belongsToMany('Blank')->withPivot('status', 'inventory', 'sizes', 'mill', 'po', 'location', 'ordered_at', 'expected_at', 'note')->withTimestamps();
    }

    /**
     * Blanks sizes accessor
     *
     * @return object
     */
    public function getSizesAttribute($value)
    {
        return json_decode($this->pivot->sizes);
    }

    /**
     * Blanks sizes mutator
     *
     * @return void
     */
    public function setSizesAttribute($value)
    {
        $this->pivot->attributes['sizes'] = json_encode($this->pivot->sizes);
    }
}

附加代码:

$job->blanks()->attach($blank->id,[
    'status'      => Input::get('status'),
    'inventory'   => Input::get('inventory'),
    //'sizes'       => $sizes,
    'mill'        => Input::get('mill'),
    'po'          => Input::get('po'),
    'location'    => Input::get('location'),
    'ordered_at'  => Carbon::parse(Input::get('ordered_at'))->format('Y-m-d H:i:s'),
    'expected_at' => Carbon::parse(Input::get('expected_at'))->format('Y-m-d H:i:s'),
    'note'        => Input::get('note'),
]);

根本没有调用增变器.有什么想法吗?

The mutator is not being called at all.. Any ideas?

推荐答案

似乎不太可能 通过 ::attach() 方法执行此操作.

Seems like that not possible to do this through ::attach() method.

但是,也许您想使用'定义自定义数据透视模型'

public function newPivot(Model $parent, array $attributes, $table, $exists)
{
    return new YourCustomPivot($parent, $attributes, $table, $exists);
}

因此,您可以使用增变子定义自己的数据透视类:

So, you can define your own pivot class with mutators:

class BlankJobPivot extends Eloquent
{
    // ...

    /**
     * Blanks sizes accessor
     *
     * @return object
     */
    public function getSizesAttribute($value)
    {
        return json_decode($value);
    }

    /**
     * Blanks sizes mutator
     *
     * @return void
     */
    public function setSizesAttribute($value)
    {
        $this->attributes['sizes'] = json_encode($value);
        return $value; // return for multiple assignment statement:  $arr = $pivot->sizes = array(12, 23, 34);
    }
}

然后您可以使用吸气剂:

And than you can use getters:

$blank->jobs[$i]->pivot->sizes; // - ::getSizesAttribute() will called ( I hope :) )

也许您会找到一种通过setSizesAttribute增变器进入save/attach的方法.

And maybe you will find a way to save / attach through the setSizesAttribute mutator.

祝你好运.

这篇关于数据透视表中的laravel/雄辩的mutators/accessors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆