0

when i use raw option to post a data into the database at that time it will work ,it's not showing any error to me at that time when i tried to post a data like a form-data at that time i am getting Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null error , How to solve this error [How i am passing headers]1 && [How i pass form-data]2 migration table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->length(50)->unique();
            $table->binary('image');
            $table->integer('price')->unsigned();
            $table->text('title');
            $table->integer('quantity')->length(2)->unsigned();
            $table->integer('ratings')->length(2)->unsigned();
            $table->string('author');
            $table->longText('description');
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('books');
    }
}

BooksController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Books;
use App\Models\User;
use App\Http\Requests;
use Symfony\Component\HttpFoundation\Response;
use App\Http\Resources\Books as BooksResource;
use App\Http\Middleware\Authenticate;

class BooksController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function DisplayBooks()
    {
        $books=Books::all();
        return User::find($books->user_id=auth()->id())->books; 
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function AddBooks(Request $request)
    {
        $book=new Books();
        $book->name=$request->input('name');
        $book->image=$request->input('image'); 
        $book->price=$request->input('price');
        $book->title=$request->input('title');
        $book->quantity=$request->input('quantity');
        $book->ratings=$request->input('ratings');
        $book->author=$request->input('author');
        $book->description=$request->input('description');
        $book->user_id = auth()->id();          
        $book->save();
        return new BooksResource($book);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function ShowBook($id)
    {
        $book=Books::findOrFail($id);
        if($book->user_id==auth()->id())
            return new BooksResource($book);
        else{
            return response()->json([
                'error' => 'UnAuthorized/invalid id'], 401);
            }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function UpdateBook(Request $request, $id)
    {
        $book=Books::findOrFail($id);
        if($book->user_id==auth()->id()){
            $book->name=$request->input('name');
            $book->image=$request->input('image'); 
            $book->price=$request->input('price');
            $book->title=$request->input('title');
            $book->quantity=$request->input('quantity');
            $book->ratings=$request->input('ratings');
            $book->author=$request->input('author');
            $book->description=$request->input('description');
            $book->save();
            return new BooksResource($book);
        }
        else
        {
            return response()->json([
                'error' => ' Book is not available ith id'], 404);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function DeleteBook($id)
    {
        $book=Books::findOrFail($id);
        if($book->user_id==auth()->id()){
            if($book->delete()){
                return response()->json(['message'=>'Deleted'],201);
            }
        }
        else{
            return response()->json([
                'error' => ' Method Not Allowed/invalid Book id'], 405);
        }
    }
}

Sravani
  • 367
  • 1
  • 3
  • 17
  • 1
    @TimLewis, i added BooksController for that migration table – Sravani Jun 30 '21 at 13:45
  • I am not sure, but I guess these links should help you out to a certain extent if not entirely. https://stackoverflow.com/a/26730839/3514160 and https://stackoverflow.com/q/9870523/3514160 – Saiyan Prince Jun 30 '21 at 14:00

0 Answers0