app/Http/Controllers/PostController.php
/**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $post_model = new Post;
        $requestData = $request->all();
        $validationRules = $post_model->validation();
        if ($request->post_type == 1) {
            $validationRules = Arr::add($validationRules, "video", "required|unique:posts,video");
        } else if ($request->post_type == 2) {
            $validationRules = Arr::add($validationRules, "image", "required|unique:posts,image");
        } else {
            $validationRules = $validationRules;
        }
        $validate = Validator::make($requestData, $validationRules);
        if ($validate->fails()) {
            return back()->withErrors($validate)->withInput($requestData);
        } else {
            if ($request->hasFile('image')) {
                $image_type = $request->file('image')->getClientOriginalExtension();
                $path = "backend_assets/images/";
                $name = "Post_" . time() . "." . $image_type;
                $full_path = $path . $name;
                $request->file('image')->move($path, $name);
                $requestData = Arr::set($requestData, 'image', $full_path);
            }
            $post_model->fill($requestData)->save();
            Toastr::success('Post Save', 'Success', ["positionClass" => "toast-bottom-right"]);
            return back();
        }
    }
public function store ( Request $ request ) { $ post_model = new Post ; $ requestData = $ request -> all ( ) ; $ validationRules = $ post_model -> validation ( ) ; if ( $ request -> post_type == 1 ) { $ validationRules = Arr :: add ( $ validationRules , " video " , " required|unique:posts,video " ) ; } else if ( $ request -> post_type == 2 ) { $ validationRules = Arr :: add ( $ validationRules , " image " , " required|unique:posts,image " ) ; } else { $ validationRules = $ validationRules ; } $ validate = Validator :: make ( $ requestData , $ validationRules ) ; if ( $ validate -> fails ( ) ) { return back ( ) -> withErrors ( $ validate ) -> withInput ( $ requestData ) ; } else { if ( $ request -> hasFile ( 'image' ) ) { $ image_type = $ request -> file ( 'image' ) -> getClientOriginalExtension ( ) ; $ path = " backend_assets/images/ " ; $ name = " Post_ " . time ( ) . " . " . $ image_type ; $ full_path = $ path . $ name ; $ request -> file ( 'image' ) -> move ( $ path , $ name ) ; $ requestData = Arr :: set ( $ requestData , 'image' , $ full_path ) ; } $ post_model -> fill ( $ requestData ) -> save ( ) ; Toastr :: success ( 'Post Save' , 'Success' , [ " positionClass " => " toast-bottom-right " ] ) ; return back ( ) ; } }
Store a newly created resource in storage.
Store a newly created resource in storage .
// dd($request->all()); // type is video