Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数据透视表中的laravel/雄辩的变种人/访问人_Php_Laravel_Eloquent_Pivot Table - Fatal编程技术网

Php 数据透视表中的laravel/雄辩的变种人/访问人

Php 数据透视表中的laravel/雄辩的变种人/访问人,php,laravel,eloquent,pivot-table,Php,Laravel,Eloquent,Pivot Table,嗯,我想标题解释了其中的大部分。让我们开始吧 空白模型: class Blank extends Eloquent { protected $table = 'blanks'; protected $softDelete = true; protected $hidden = array(); /** * Get associated jobs. * * @return mixed */ public fu

嗯,我想标题解释了其中的大部分。让我们开始吧

空白模型:

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'),
]);

根本没有调用突变子。。有什么想法吗?

似乎不可能通过
实现这一点::attach()
方法。

但是也许您想使用“

因此,您可以使用mutators定义自己的pivot类:

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);
    }
}
此外,还可以使用getter:

$blank->jobs[$i]->pivot->sizes; // - ::getSizesAttribute() will called ( I hope :) )
也许你会通过
setSizeAttribute
mutator找到一种
保存
/
附加
的方法


祝你好运。

你希望被称为哪一个突变子。。。什么时候?我会假设作业模型上的变量“setSizeAttribute”会被调用,因为我正在设置“Size”。它当前在代码中被注释掉了,因此您可以看到它被调用的位置。这仅仅是在数组中,只有在您直接从模型实例调用该值时,才会调用mutator。如果您想在枢轴上使用变异体,则需要为枢轴显式地创建一个新模型。
$blank->jobs[$i]->pivot->sizes; // - ::getSizesAttribute() will called ( I hope :) )