简体   繁体   中英

Laravel method update with the logged user

I have method store:

public function store(CreateProductRequest $request){
    $product = new Product($request->all());
    Auth::user()->products()->save($product);
    Session::flash('status3', 'Produkt został dodany poprawnie');
    return redirect('warehouse');
}

In this method I used Auth::user to add the name of the user who added a new record to the database. I want to do the same in the method update:

public function update($id, CreateProductRequest $request){
    $product = Product::find($id);
    $products = new Product($request->all());
    Auth::user()->products()->update($products);
    Session::flash('status4', 'Produkt został zaktualizowany poprawnie');
    return redirect('warehouse');
}

and method products in model User:

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

Laravel returned:

Type error: Argument 1 passed to Illuminate\\Database\\Eloquent\\Relations\\HasOneOrMany::update() must be of the type array, object given, called in ...

change

Auth::user()->products()->update($products);

to this

Auth::user()->products()->update([
    'article_id' => $request->get('article_id'),
    'category_id' => $request->get('category_id'),
    'quantity' => $request->get('quantity'),
    'warranty' => $request->get('warranty')
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM