I'm trying to see a webpage of a random post that contains two buttoms of Like and Dislike on Laravel..
I inserted some random numbers into the database table likes manually according to user.id and post.id and i turned like=1 just to test my view workin or not but Nothing seems to change, they both are set null no matter hat i change in the database
The error i was getting was this:
(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).
But then someone told me to change my likes function in my Post model to this:
Before :
public function likes() {
return $this->hasMany(User::class);
}
After:
public function likes() {
return $this->hasMany(Like::class, 'id', 'post_id');
}
As I said the error is gone, but it still doesn't work, the view doesn't respond to my random records on database.
Ps. my likes table has both user_id and post_id as arguments
----> 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>
Any help would be appreciated, thank you!
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2ISTUNm
via IFTTT
Aucun commentaire:
Enregistrer un commentaire