Mena
Mena

Reputation: 2029

How to test laravel model relationship in postman

I am trying to test a relationship between Book and Rating models. User should be able to rate a book as long as they are signed in on the app. For some reason, my test on postman returns the error below

SQLSTATE[HY000]: General error: 1364 Field 'rating' doesn't have a default value (SQL: insert into ratings (user_id, book_id, updated_at, created_at) values (2, 6, 2018-09-19 10:03:11, 2018-09-19 10:03:11))

Here's my Rating Model

class Rating extends Model
{
protected $fillable = ['user_id', 'book_id', 'rating'];

/**
 * Rating/Book
 * A rating belongs to a book
 */
public function book()
{
    return $this->belongsTo(Book::class);
}
}

Book Model

class Book extends Model
{
protected $fillable = ['user_id', 'title', 'author'];

/**
 * Book/User relationship
 * A book belongs to only one user
 */
public function user()
{
    return $this->belongsTo(User::class);
}

/**
 * Book/Rating relationship
 * A book has many ratings
 */
public function ratings()
{
    return $this->hasMany(Rating::class);
}
}

RatingController

public function store(Request $request, Book $book)
{
    // Allow only logged in users to rate books
    if ($request->user()->id) {

        $rating = Rating::create([
            'user_id'   => $request->user()->id,
            'book_id'   => $book->id,
        ],
        [
            'rating'    => $request->rating
        ]);

        return new Rating($rating);
    } else {
        return response()->json(['error', 'Login to rate this book.'], 403);
    }
}

I obviously can't find where the problem lies or how to fix it and need some expert eyes and guide.

Upvotes: 0

Views: 954

Answers (3)

Daniel Protopopov
Daniel Protopopov

Reputation: 7236

public function store(Request $request, Book $book)
{
    // Allow only logged in users to rate books
    if ($request->user()->id) {

        try {
            $rating = new Rating([
                'user_id'   => $request->user()->id,
                'book_id'   => $book->id,
                'rating'    => $request->rating
            ]);

            $rating->save();
            return $rating;
        } catch(Exception $ex) {
            return response()->json(['error', 'Failed to create new user'], 403);
        }
    } else {
        return response()->json(['error', 'Login to rate this book.'], 403);
    }
}

Upvotes: 2

atf.sgf
atf.sgf

Reputation: 494

its a sql error and dont related to postman, in Rating model you have a column called rating and this column cant be null when you insert a record in database, you have two solution, you can set a value for rating column in code when value is empty (such as 0 or null or empty string and ..) other solution is, you set a default value for rating columnin data base such as 0 in migration:

 $table->integer('rating')->default(0)

OR say database that filed can be null

$table->integer('rating')->nullable()

Upvotes: 0

party-ring
party-ring

Reputation: 1871

Your value for 'rating' is not being submitted. Why is rating in a separate array? You are trying to create one object to submit to the database. I would try:

if ($request->user()->id) {

    $rating = Rating::create([
        'user_id'   => $request->user()->id,
        'book_id'   => $book->id,
        'rating'    => $request->rating
     ])

 ...

A good test would be to create a default value in the database for a rating (e.g. 0), that way you can test to see which parts of your query are successful.

Upvotes: 2

Related Questions