mercredi 23 mai 2018

SQLSTATE[42S22]: Column not found: 1054 Unknown column (Laravel)

The error i'm getting is: (SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause' (SQL: select * from users where users.post_id = 1 and users.post_id is not null).

I'm trying to consult a page of a post that contains two buttoms of Like and Dislike:

----> migration of the table likes:

public function up()
{
    Schema::create('likes', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id');
        $table->integer('post_id');
        $table->boolean('like');
        $table->timestamps();
    });
}

----> migration of the table users is:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();

    });
}

-------------------->My User model is :

class User extends Authenticatable
{
use Notifiable;

public function likes() {

return $this->hasMany(User::class);
}
/**
 * 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',
];
 }

--------------> My Post model is:

 class Post extends Model
 {
public $table="posts";


 public function comments() {

return $this->hasMany(Comment::class)->orderBy('created_at');
}

 public function likes() {

return $this->hasMany(User::class);
 }
 }

-----------> My Like model is :

 class Like extends Model
  {
 public function post() {

return $this->belongsTo(Post::class);
  }

 public function user() {

return $this->belongsTo(User::class);
 }

-------------and my Comment model is :

 class Comment extends Model
 {

 protected $fillable = [
'username', 'body', 'post_id'
 ];
public function post() {

return $this->belongsTo(Post::Class);
 }

  }

----> The part of code i'm calling out for variables in the blade Post.blade.php is :

  @php

        $like_count  = 0;
        $dislike_count = 0;

        @endphp

        @foreach ($post->likes as $like)
  @php

  if($like->like == 1)
 {
  $like_count++;

  }
  if($like->like == 0)
  {
   $dislike_count++;
 }

     @endphp
     @endforeach
        <button type="button" class="btn btn-success">Like <i class="fa fa- 
        thumbs-up"></i><b>  </b>
        </button>
      <button type="button" class="btn btn-danger">Dislike <i class="fa fa- 
       thumbs-down"></i> <b>  </b>
     </button>

I inserted some random numbers into the table likes manually according to user.id and post.id and i turned like=1



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire