app/Http/Controllers/CampaignController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $rules = [
            'title' => 'required|min:8',
            'categories' => 'required|array',
            'short_description' => 'required',
            'body' => 'required|min:8',
            'publish_date' => 'required|date_format:Y-m-d H:i',
            'status' => 'required|in:publish,archived',
            'goal' => 'required|regex:/^[0-9.]+$/|min:7',
            'end_date' => 'required|date_format:Y-m-d H:i',
            'note' => 'nullable',
            'receiver' => 'required',
            'path_image' => 'required|mimes:png,jpg,jpeg|max:2048'
        ];
        if (auth()->user()->hasRole('donatur')) {
            $rules['status'] = 'nullable';
        }
        $validator = Validator::make($request->all(), $rules, [
            'goal.min' => 'Nominal minimal 100.000'
        ]);
        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 422);
        }
        $data = $request->except('path_image', 'categories');
        $data['slug'] = Str::slug($request->title);
        $data['goal'] = str_replace('.', '', $request->goal);
        $data['path_image'] = upload('campaign', $request->file('path_image'), 'campaign');
        $data['user_id'] = auth()->id();
        $campaign = Campaign::create($data);
        $campaign->category_campaign()->attach($request->categories);
        return response()->json(['data' => $campaign, 'message' => 'Projek berhasil ditambahkan']);
    }
public function store ( Request $ request ) { $ rules = [ 'title' => 'required|min:8' , 'categories' => 'required|array' , 'short_description' => 'required' , 'body' => 'required|min:8' , 'publish_date' => 'required|date_format:Y-m-d H:i' , 'status' => 'required|in:publish,archived' , 'goal' => 'required|regex:/^[0-9.]+$/|min:7' , 'end_date' => 'required|date_format:Y-m-d H:i' , 'note' => 'nullable' , 'receiver' => 'required' , 'path_image' => 'required|mimes:png,jpg,jpeg|max:2048' ] ; if ( auth ( ) -> user ( ) -> hasRole ( 'donatur' ) ) { $ rules [ 'status' ] = 'nullable' ; } $ validator = Validator :: make ( $ request -> all ( ) , $ rules , [ 'goal.min' => 'Nominal minimal 100.000' ] ) ; if ( $ validator -> fails ( ) ) { return response ( ) -> json ( [ 'errors' => $ validator -> errors ( ) ] , 422 ) ; } $ data = $ request -> except ( 'path_image' , 'categories' ) ; $ data [ 'slug' ] = Str :: slug ( $ request -> title ) ; $ data [ 'goal' ] = str_replace ( '.' , '' , $ request -> goal ) ; $ data [ 'path_image' ] = upload ( 'campaign' , $ request -> file ( 'path_image' ) , 'campaign' ) ; $ data [ 'user_id' ] = auth ( ) -> id ( ) ; $ campaign = Campaign :: create ( $ data ) ; $ campaign -> category_campaign ( ) -> attach ( $ request -> categories ) ; return response ( ) -> json ( [ 'data' => $ campaign , 'message' => 'Projek berhasil ditambahkan' ] ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .