无法在laravel中的数据库中存储布尔值

时间:2017-01-13 04:41:05

标签: php jquery ajax laravel

我试图在laravel中制作喜欢和不喜欢的系统,我这样做是通过当有人点击时返回1和0当有人不喜欢但数据库没有显示任何条目时。 这是我web.php中的代码

<?php


Route::get('/', function () {
return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index');
//comments
Route::resource('comments','CommentsController');
//like
Route::post('/like', [
'uses' => 'LikeController@postLikePost',
'as' => 'like'
 ]);

这是我的控制器代码: -

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class LikeControler extends Controller
{
//


 public function postLikePost(Request $request)
{

    $is_like = $request['isLike'] === 'true';
    $update = false;

    $user = Auth::user();
    $like = $user->likes();
    if ($like) {
        $already_like = $like->like;
        $update = true;
        if ($already_like == $is_like) {
            $like->delete();
            return null;
        }
    } else {
        $like = new Like();
    }
    $like->like = $is_like;
    $like->user_id = $user->id;

    if ($update) {
        $like->update();
    } else {
        $like->save();
    }
    return null;
}

}

这是我的布局代码,名为create.blade.php

<html>
<head>
<h1>DONE</h1>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    
 <script>
$('.like').on('click', function(event) {
event.preventDefault();

var isLike = event.target.previousElementSibling == null;
$.ajax({
    method: 'POST',
    url: urlLike,
    data: {isLike: isLike,_token: token}
 })
 </script>
 <script> var urlLike ='{{ route ('like')}}';   </script>
 </head>
 <body>
<div class="row new-post">
    <div class="col-md-6 col-md-offset-3">

        <header><h3>Comments</h3></header>
        <form action="/comments" method="post">
        {{csrf_field()}}
            <div class="form-group">
                <textarea class="form-control" name="body" id="new-post"  rows="5" placeholder="Your review on above game"></textarea>
            </div>
            <button type="submit" class="btn btn-primary">Post  Comment</button>

        </form>
    </div>
</div>

 <div class="interaction">
                    <a href="#" class="like">Like</a> 
                    <a href="#" class="like">Unlike</a>

</div>

@foreach($comments as $comment) 
<h1>{{$comment->body }}</h1>
@endforeach

</body>

</html>

这是我的迁移代码: -

<?php

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

class CreateLikesTable extends Migration
{
 /**
  * Run the migrations.
  *
   * @return void
  */
    public function up()
    {
      Schema::create('likes', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->integer('user_id');

        $table->boolean('like');
    });
 }

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

这是我的user.php代码

<?php

namespace App;

 use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];


 public function likes()
{
    return $this->hasMany('App\Like');
}

}

这里是like.php的代码

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Like extends Model
{
//

public function user()
{
    return $this->belongsTo('App\User');
}

}

我甚至在github https://github.com/Pawan98/lara上传了我的代码。提前提交: - )

1 个答案:

答案 0 :(得分:1)

检查关联列的列数据类型。要将0或1存储到列中,则列为:

IntStringBooleanEnum('0', '1')TinyInt

然后将其更改为其中一个并重试。

如果它是布尔值而不是将其包装成单引号