jeudi 12 janvier 2017

laravel 5.1. why my update for pivot tabel dose not effected on table?

I have a user model :

namespace App\Models\UsersModels;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Model implements AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable,
        CanResetPassword,
        Authorizable,
        EntrustUserTrait // add this trait to your user model
    {
        EntrustUserTrait ::can insteadof Authorizable; //add insteadof avoid php trait conflict resolution
    }

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];



    /**
     * user belongs to many discount.
     */
    public function discounts()
    {
        return $this->belongsToMany('App\Models\Discounts\Discount', 'users_discounts')
            ->withPivot('id','earn_date_time','used_date_time','used_for_id','used_for_type');
    }

}

in this model I have a relation many to many with discounts. and other side this relation defined.

know I want update my users_discounts table .

when I use this for update:

 $a =  DB::table('users_discounts')
        ->where('discount_id', '=', 2)
        ->update(['used_for_id' => 2]);

       $d = DB::table('users_discounts')
            ->where('discount_id', '=', 2)
           ->get();

                dd($d);

and select after that, every thing is ok and return updated filed. but when I check my table any thing not updated. why ? I need define fillable for pivot table? or do more than update ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire