Skip to content

Commit fa5f891

Browse files
Add Like and Share migrations and models
1 parent 311a3ac commit fa5f891

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

app/Models/Like.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Like extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $fillable = [
13+
'user_id', 'post_id',
14+
];
15+
16+
public function post()
17+
{
18+
return $this->belongsTo(Post::class);
19+
}
20+
21+
public function user()
22+
{
23+
return $this->belongsTo(User::class);
24+
}
25+
}

app/Models/Share.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Share extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $fillable = [
13+
'user_id', 'post_id',
14+
];
15+
16+
public function post()
17+
{
18+
return $this->belongsTo(Post::class);
19+
}
20+
21+
public function user()
22+
{
23+
return $this->belongsTo(User::class);
24+
}
25+
}

database/migrations/2023_08_31_215542_create_posts_table.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ public function up()
1616
$table->text('content')->nullable();
1717
$table->string('image_path')->nullable();
1818
$table->unsignedInteger('comments_count')->default(0);
19+
$table->unsignedInteger('likes_count')->default(0);
20+
$table->unsignedInteger('shares_count')->default(0);
1921
$table->unsignedBigInteger('user_id');
22+
$table->unsignedBigInteger('shareduser_id')->default(0);
2023
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
2124
$table->softDeletes();
2225
$table->timestamps();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('likes', function (Blueprint $table) {
17+
$table->id();
18+
$table->unsignedBigInteger('user_id');
19+
$table->unsignedBigInteger('post_id');
20+
$table->timestamps();
21+
22+
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
23+
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('likes');
35+
}
36+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('shares', function (Blueprint $table) {
17+
$table->id();
18+
$table->unsignedBigInteger('user_id');
19+
$table->unsignedBigInteger('post_id');
20+
$table->timestamps();
21+
22+
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
23+
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('shares');
35+
}
36+
};

0 commit comments

Comments
 (0)