Sunday, February 25, 2018

Laravel one to many relation delete request with guzzle - development

Laravel one to many relation delete request with guzzle

I have two table, Book and Author, use one to many relation, Author as parent and Author as child. I used Guzzle for client request to Book and Author. But the problems when delete book table request from client app, getting error like this

    GuzzleHttp  Exception  ServerException (500)
Server error: `DELETE http://localhost:8080/api/author/4` resulted in a `500 Internal Server Error` response: <!DOCTYPE html> <html> <head> <meta name="robots" content="noindex,nofollow" /> <style> (truncated...)

and sometimes its success but parent table (Author) removed too.
And when i make request with postman all is work fine.

Its my code, thanks before.

Book controller API

<

?php

namespace AppHttpControllers;

use AppBook;
use IlluminateHttpRequest;
use DB;

/**
* 
*/
class BookController extends Controller

{
    public function allBook()
    {
        return response()->json(Book::all());
    }

    public function Book($id)
    {
        return response()->json(Book::find($id));
    }

    public function createBook(Request $request)
    {
        $book = Book::all();
        return Book::create($request->all());
    }

    public function updateBook($id, Request $request)
    {
        $book = Book::find($id);
        $book->update($request->all());

        return response()->json($book, 200);
    }

    public function deleteBook($id)
    {
        Book::find($id)->delete();
        return response('Book deleted', 200);
    }
}

Book migration table

 <?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('genres');
            $table->text('synopsis');
            $table->integer('author_id')->unsigned();
            $table->foreign('author_id')->references('id')->on('authors')->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

    }
}

Book Model

  <?php

    namespace App;

    use IlluminateDatabaseEloquentModel;

    class Book extends Model
    {
        protected $fillable =[
            'title','genres','synopsis','author_id'];

        public function author()
        {
            return $this->belongsTo(Author::class);
        }
    }

and its Book controller for request from client

     public function deleteBook($id)
   {
    $client = New GuzzleHttpClient();
       $apiRequest = $client->request('DELETE','http://localhost:8080/api/book/'. $id);

       return redirect()->route('book.index')->with('response', 'Delete book successfully');

   }



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-one-to-many-relation-delete-request-with-guzzle/
via Lzo Media

No comments:

Post a Comment