S
S
serik2020-01-19 11:14:20
Laravel
serik, 2020-01-19 11:14:20

Why is data not being entered into the database using the REST API?

Good day, who can tell me what I'm doing wrong, what is my mistake?
When I try to authorize, everything works correctly, the code 200 and JSON are returned
and a record is created in the database ....
5e240f6322085989218440.png
But when I start using the function to add a post, I specify the authorization token, I specify all the required fields and the code 200 is returned but without JSON and there is no record in the table .
Tell me how to fix it.
Or maybe I'm not working correctly with the Postman
routes/api.php program

Route::group(['middleware' => 'auth:api'], function () {
   Route::post('create-post', '[email protected]');
});

app\Http\Controllers\PostController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\AuthenticationException;

class PostController extends Controller
{
    //------------- [ Create new Post ] ----------------

    public function createPost(Request $request) {

        $img_name = "";

        // validate input
        $validator  =   Validator::make($request->all(),
            [
                'title'              =>      'required',
            ]
        );

        // if validation fails
        if($validator->fails()) {
            return response()->json(["validation errors" => $validator->errors()]);
        }

        // Retrieve User with acccess token
        $user           =       Auth::user();

        // Upload Featured Image
        $validator      =   Validator::make($request->all(),
           ['featured_img'      =>   'required|mimes:jpeg,png,jpg,bmp|max:2048']);

        // if validation fails
        if($validator->fails()) {
            return back()->withErrors($validator->errors());
        }

        if($file   =   $request->file('featured_img')) {

            $img_name      =   time().time().'.'.$file->getClientOriginalExtension();

            $target_path    =   public_path('/uploads/');

                $file->move($target_path, $img_name);

                // Creating slug

                $slug           =       str_replace(" ", "-", strtolower($request->title));

                $slug           =       preg_replace('/[^A-Za-z0-9\-]/', '', $slug);

                $slug           =       preg_replace('/-+/', '-', $slug);

                // creating array of inputs
                $input              =       array(
                    'title'          =>          $request->title,
                    'slug'           =>          $slug,
                    'featured_img'   =>          $img_name,
                    'user_id'        =>          $user->id
                );

                // save into database
                $post                   =       Post::create($input);
        }

        return response()->json(["success" => true, "data" => $post]);
    }


    // --------- [ Post Listing By User Token ] -------------
    public function postListing() {

        // Get user of access token
        $user           =       Auth::user();

        // Listing post through user id
        $posts          =       Post::where("user_id", $user->id)->get();

        return response()->json(["success" => true, "data" => $posts]);
    }
}

Did everything according to the instructions

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question