what datatype is this to save on mysql 1 - 4.9

2019-09-05 23:59发布

问题:

I want to save style ratings that ranges from 1 - 4.9 on my mysql database please what datatype should i use? and i keep getting this error when i save it as a decimal(11,0)

ErrorException in helpers.php line 747: Method App\Wasamar\Rating::__toString() must return a string value

MY CODE

$allInputs = Input::all();
    $catalogueStyleId = Input::get('catalogueStyleId');
    $userId = Crypt::decrypt( Input::get('user') );
    $rating = Input::get('rating');


    $countUserStyleRating = count( Rating::where('catalogue_style_id',$catalogueStyleId)->where('user_id','=', $userId)->first() );

    if ( $countUserStyleRating == 0 ) {
        # add new rating data

        $rating = new Rating;
        $rating->user_id = $userId;
        $rating->catalogue_style_id = $catalogueStyleId;
        $rating->catalogue_style_rating = $rating;
        $rating->save();

        echo "Your rating has been noted thank you";
    }

回答1:

The problem is that your decimal definition specifies no decimal points. DECIMAL(11,0) means you want to store 11 digits left of the decimal point and none after.

You need to define your decimal as DECIMAL(2,1) if you want to store 0.1 to 4.9. The 2 means you want to store two total digits, and the 1 means you want 1 of those digits right of the decimal point.

From the documentation:

The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.7 are as follows:

M is the maximum number of digits (the precision). It has a range of 1 to 65.

D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

MySQL Documentation