Skip to content

Commit

Permalink
#1 - Structure de notre blog
Browse files Browse the repository at this point in the history
  • Loading branch information
alexis-riot committed Feb 18, 2020
1 parent 9b7e620 commit 5b63e20
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
17 changes: 17 additions & 0 deletions project/app/Comment.php
@@ -0,0 +1,17 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id', 'post_id', 'message',
];
}
22 changes: 22 additions & 0 deletions project/app/Post.php
@@ -0,0 +1,22 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id', 'title', 'content',
];

public function comments()
{
return $this->hasMany('App\Comment', 'post_id', 'id');
}
}
10 changes: 10 additions & 0 deletions project/app/User.php
Expand Up @@ -36,4 +36,14 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

public function posts()
{
return $this->hasMany('App\Post', 'user_id', 'id');
}

public function comments()
{
return $this->hasMany('App\Comment', 'user_id', 'id');
}
}
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');;
$table->string('title', 150);
$table->text('content');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');;
$table->unsignedBigInteger('post_id');
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');;
$table->text('message');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}

0 comments on commit 5b63e20

Please sign in to comment.