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
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
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.