php - can't access the values throw the eloquent relationship
I have a post model and like model and I make a relationship between them but still can't access the values of the like model through the post
that is the like model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Like extends Model
{
protected $guarded = [];
public function user()
{
return $this->hasOne(User::class);
}
public function post()
{
return $this->hasOne(Post::class);
}
}
and that is the post modal
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravelista\Comments\Commentable;
class Post extends Model
{
use Commentable;
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
public function likes()
{
return $this->hasMany(Like::class);
}
}
this is the like Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLikesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('likes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('like')->default(0);
$table->unsignedBigInteger('dislike')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('likes');
}
}
and that the output when I try to access the data I can access the relationship but can't access the rest of the data
$t->likes
=> Illuminate\Database\Eloquent\Collection {#4667
all: [
App\Like {#4654
id: 1,
user_id: 1,
post_id: 1,
like: 1,
dislike: 2,
created_at: "2021-05-21 16:49:39",
updated_at: "2021-05-21 16:49:39",
},
],
}
>>> $t->likes-like
PHP Warning: Use of undefined constant like - assumed 'like' (this will throw an Error in a future version of PHP) in E:/files/workshop/projects/iraqm-maineval()'d code on line 1
Answer
Solution:
- when ever you see yourself using hasOne relationship think of creating a field !
- if you have a like and a dislike and no relationship those are 3 possibilities of interaction of the user on the post so it would be efficient if you have exactly a combination of 3 possibilities of data in your db .
record exists => there an interaction : 0 for dislike and 1 for like .
record does not exist => there is no interaction .
in your code you used like and dislike fields which means you have 4 possibilit?�s which doesnt make any sense since we only need 2 ( and the does not exits ) . i would recommand to change your migration for less queries and tests , remove the like model and use model method (likes , liked ..)
public function up()
{
Schema::create('likes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('user_id');
$table->foreignId('post_id');
$table->unsignedBigInteger('like')->default(0);
$table->timestamps();
});
}
Post Model
public function user()
{
return $this->belongsTo(User::class);
}
public function likes()
{
return $this->belongsToMany(User::class, 'likes', 'post_id', 'user_id')->withPivot('like')
}
}
User Model
public function user()
{
return $this->belongsTo(User::class);
}
public function likes()
{
return $this->belongsToMany(Post::class, 'likes', 'user_id', 'post_id')->withPivot('like')
}
}
Like Controller
public like(Post $post) {
$like = Auth::user()->likes()->where('post_id' , $post->id)->first();
if($like) {
if($like->pivot == 1) {
$like->delete();
}else {
$like->like = 1;
$like->save();
}
}else {
Auth::user()->create(['post_id'=>$post->id , 'like'=>1])
}
}
// same for dislike ,, you can refactor in multiple model methods
Source