app/Http/Controllers/Api/PostController.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)
    {
        $request->validate([
            'title'=> 'required|min:3',
            'body'=> 'required|min:6',
            'image' => 'string|min:6',
            'user_id'=> 'integer',
        ]);
        $image = $request->image;   your base64 encoded
        $image = str_replace('data:image/png;base64,', '', $image);
        $image = str_replace(' ', '+', $image);
        $imageName = Str::random(6).'.'.'png';
        \File::put(storage_path(). '/app/public/images/posts/' . $imageName, base64_decode($image));
        $blog = Post::create([
            'title' => $request['title'],
            'body' => $request['body'],
            'image' => $imageName, $request['image'] ,
            'user_id' => $request['user_id'],
        ]);
        if(!$blog){
            dd($blog);
            return response ([
                'message' => 'Blog not saved',
            ], 500);
        }
        return response([
            'message' => 'Post created',
        ], 201);
    }
public function store ( Request $ request ) { $ request -> validate ( [ 'title' => 'required|min:3' , 'body' => 'required|min:6' , 'image' => 'string|min:6' , 'user_id' => 'integer' , ] ) ; $ image = $ request -> image ; $ image = str_replace ( 'data:image/png;base64,' , '' , $ image ) ; $ image = str_replace ( ' ' , '+' , $ image ) ; $ imageName = Str :: random ( 6 ) . '.' . 'png' ; \ File :: put ( storage_path ( ) . '/app/public/images/posts/' . $ imageName , base64_decode ( $ image ) ) ; $ blog = Post :: create ( [ 'title' => $ request [ 'title' ] , 'body' => $ request [ 'body' ] , 'image' => $ imageName , 'user_id' => $ request [ 'user_id' ] , ] ) ; if ( ! $ blog ) { dd ( $ blog ) ; return response ( [ 'message' => 'Blog not saved' , ] , 500 ) ; } return response ( [ 'message' => 'Post created' , ] , 201 ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// // your base64 encoded //$request['image'] ,