Mass Attribution in Laravel

Asked

Viewed 330 times

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)
   {

Works :

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

    $this->save();

Doesn’t 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);
}

}

  • Place your class

  • Funny because you have created a method that already exists? You are rewriting it including! Your missing class set up fillable ...

1 answer

1


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

In his class also lacked fillable configured by this configuration that the create use to save data to 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 do the following line of code:

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

At this link Mass Assignment explains that the fillable to use the method create.

The class already has the methods implemented, no need to put anything else by itself it generates list, records information, makes relations, etc., no need to do anything else besides that.

I was looking at your code, there’s a multiplication, so look at the best way to do it for example, 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 can already use all the resources and work with the information in the way you want, through its rules.

If you still want to create methods within the model (would not recommend), but, can changing the name of the methods, for example: 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());
    }
}

then I’d wear it like this:

Sale::insert($request);

or

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

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.