WeDesIn of Digihood
9/25/2019 - 9:34 AM

Laravel metoda aktualizace příspěvku

Method for updating existing products

<?php

//update given product
public function update( Request $request, $id  ) {
    
    $validator = Validator::make($request->all(), [
        'title' => 'required|max:255',
        'content'=> 'max:999',
        'ean' => 'required|max:13',
        'price' => 'required|numeric',
        'stock' => 'numeric'
    ]);

    //validator fails
    if ( $validator->fails() ) {

        Session::flash('flash_warning', __('general.edit_error'));

        return redirect()->back()->withInput()->withErrors($validator->errors());
    } 

    //save the date to database
    $product = Post::find( $id );
    $product->title = $request->title;
    $product->body = $request->content;
    $product->updated_at = Carbon::now();
    $product->save();

    //save metadata
    $product->saveMeta( 'ean', $request->ean );
    $product->saveMeta( 'price', $request->price );
            
    //add flash success
    Session::flash('flash_success', __('general.edit_success'));

    //return to edit page
    return redirect()->route( 'admin.products.edit', [ 'request' => $product ] );
}
    
?>