Errors in add multiple img by laravel 5.3

Posted 2 years ago by Mo7amad_7amdy

what i do in this Controller function:

public function newOffer(Request $request){

    $validate_marge['images'] = 'required';
    $validator = Validator::make($request->all(), $validate_marge);
    if ($validator->fails()){
        return back()->withInput()->withErrors($validator);
    }else{
        $post = new Post();
        $files = $request->file('images');
        foreach ($files as $file):
            $filename = str_random(6) . '_' . time(). $file->getClientOriginalName();
            $file->move(public_path('img/offers').'/', $filename);
            $post->images = $filename;
        endforeach;
    }
    $post->title = Input::get('title');
    $post->category_id = Input::get('category_id');
    $post->second_title = Input::get('second_title');
    $post->description = Input::get('description');
    $post->location = Input::get('location');
    $post->price = Input::get('price');
    $post->save();


    Session::flash('message', 'Congratulation, Post Created!');
    return back();
}

the errors is: in public_path img's added Okay, but in DB only one img add !

what i do in controller ?? thank you

Please sign in or create an account to participate in this conversation.