Bi Híp
Bi Híp

Reputation: 103

Call to undefined method Laravel 5.2

i have problems

BadMethodCallException in Builder.php line 2258: Call to undefined method Illuminate\Database\Query\Builder::add()

Controller

public function Cart(Request $request, $id){

            $products_buy = Products::find($id);

            Carts::add(array('id'=>$id,'name'=>$products_buy->name_product,
                             'qty'=>1,'price'=>$products_buy->price,
                             'options'=>array('img'=>$products_buy->picture)));

            $content= Carts::content();
            return View('pay.cart')->with(
                "cart",$content
            );      
        }

Upvotes: 1

Views: 1337

Answers (1)

Brett
Brett

Reputation: 2010

In laravel 5.2 to correctly create an entry use

Model::create 

not

Model::add

Please note this adds it to the database straight away.

If you don't want to add straight away use

$flight = new Flight;
$flight->fill($valuesArray);
$flight->save();

only call save when ready to commit to DB

If you get a Mass Assignment error. You need to ensure all the values in the array you pass to create or fill are set in the model in the $fillables variable. If it is not in the fillable it can't be assigned in the fill or create method and you have to manually do it. $model->value_not_in_fillable = 1;

class Flight extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name'];
}

Upvotes: 2

Related Questions