Mass assignment in Laravel

1
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Facades\DB;

class Sale extends Model

{
   protected $guarded = ['id'];

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

    public function listSales()
    {
       return $this->all();
    }

   public function create($request)
   {

It works:

    $this->id_vendor = $request['id_vendor'];
    $this->price = $request['price'];
    $this->comission = $request['price'] * 0.085;

    $this->save();

Does not work:

     // $teste = $this::create([
    //     'id_vendor' => $request['id_vendor'],
   //      'price'     => $request['price'],
  //       'comission' => '$request[\'price\']' * 0.085,
 //  ]);


    return $this->informationVendor($request['id_vendor']);
}

public function informationVendor($id)
{
    return DB::table('sales')
        ->join('vendors', 'vendors.id', '=', 'sales.id_vendor')
        ->where('sales.id_vendor', $id)
        ->select('vendors.name', 'vendors.email', 'sales.price', 'sales.comission')
        ->get()
        ->take(1);
}

}

    
asked by anonymous 22.07.2017 / 22:09

1 answer

1

Already indicating a problem, which is to rewrite a method that already exists to make a certain operation that is moreover primordial, the create method is intended to create new records and does not have to create again, use the which already exists and it itself does what you need which is to generate new records.

In your class, too, fillable was set up, which is why this setting create uses to write the data to the table:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Sale extends Model
{
    protected $guarded = ['id'];
    protected $fillable = ['id_vendor','price', 'comission'];

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

To use just make the following line of code:

Sale::create($request->all());

In this link Mass Assignment explains that fillable must be set to use the create .

The class already has the implemented methods, do not need to put anything else by itself it generates list, writes information, makes relationships, etc., does not have to do anything beyond that.

I was looking at your code, it has a multiplication, so analyze the best way to do this eg it can be done like this:

$arr = $request->only(['id_vendor','price']);
$arr['comission'] = $request->get('price') * 0.085;
Sale::create($arr);

Then, just pointing out you can use all the resources and work with the information in any way you want, by means of your rules.

If you still want to create methods within the model ( would not recommend ), > : insert

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Sale extends Model
{
    protected $guarded = ['id'];
    protected $fillable = ['id_vendor','price', 'comission'];

    public function vendor()
    {
        return $this->hasMany('App\Vendor');
    }
    public function insert($request)
    {
        return $this->create($request->all());
    }
}

I would use it like this:

Sale::insert($request);

or

$sale = new Sale();
$sale->insert($request);

22.07.2017 / 23:11