Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Solve Exercises 1 and 2 #9

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions app/Console/Commands/MigrateAttachmentsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace App\Console\Commands;

use App\Models\Attachment;
use App\Models\Comment;
use App\Models\CommentAttachment;
use App\Models\Post;
use App\Models\PostAttachment;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;

class MigrateAttachmentsData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrate_attachments_data';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Migration of the data from the post_attachments and comment_attachments tables to attachments table';

/**
* The attachment data array.
*
* @var array
*/
protected $attachmentArray = [];

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*/
public function handle()
{
$this->line('Migrating...');
$this->newLine();

PostAttachment::all()->each(function ($postAttachment) {
$this->setAttachmentArray($postAttachment);
});

CommentAttachment::all()->each(function ($commentAttachment) {
$this->setAttachmentArray($commentAttachment);
});

$this->withProgressBar(collect($this->attachmentArray)->chunk(100)->toArray(), function ($attachmentArray) {
Attachment::insert($attachmentArray);
});

$this->newLine();
$this->newLine();
$this->info('Done!');
}

/**
* Set attachmentArray.
*
* @param Illuminate\Database\Eloquent\Model $model
*
* @return void
*/
protected function setAttachmentArray(Model $model)
{
$attachable_type = (get_class($model) == PostAttachment::class) ? Post::class : Comment::class;
$attachable_id = ($attachable_type == Post::class) ? $model->post_id : $model->comment_id;

$this->attachmentArray[] = [
'attachable_type' => $attachable_type,
'attachable_id' => $attachable_id,
'url' => $model->url,
'updated_at' => $model->updated_at,
'created_at' => $model->created_at,
];
}
}
4 changes: 2 additions & 2 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console;

use App\Console\Commands\MigrateAttachmentsData;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand All @@ -13,13 +14,12 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
//
MigrateAttachmentsData::class,
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
Expand Down
26 changes: 26 additions & 0 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Controllers;

use App\Models\User;

class HomeController extends Controller
{
/**
* Display a listing of users, posts, comments and attachments.
*
* @return \Illuminate\View\View
*/
public function index()
{
$users = User::with([
'posts' => function ($post) {
$post->withCount('attachments');
$post->withCount('comments');
$post->withCount('commentAttachments');
},
])->get();

return view('index', compact('users'));
}
}
29 changes: 29 additions & 0 deletions app/Models/Attachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Attachment extends Model
{
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'attachable_type',
'attachable_id',
'url',
];

/**
* Get the parent attachable model.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function attachable()
{
return $this->morphTo();
}
}
22 changes: 21 additions & 1 deletion app/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,36 @@ class Comment extends Model
*/
protected $fillable = [
'body',
'post_id'
'post_id',
];

/**
* A comment belongs to a post.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function post()
{
return $this->belongsTo(Post::class);
}

/**
* A comment may have many comment attachments.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function comment_attachments()
{
return $this->hasMany(CommentAttachment::class);
}

/**
* Get all of the comment's attachments..
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function attachments()
{
return $this->morphMany(Attachment::class, 'attachable');
}
}
37 changes: 36 additions & 1 deletion app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,56 @@ class Post extends Model
protected $fillable = [
'title',
'body',
'user_id'
'user_id',
];

/**
* A post belongs to a user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}

/**
* A post may have many comments.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function comments()
{
return $this->hasMany(Comment::class);
}

/**
* A post may have many post attachments.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function post_attachments()
{
return $this->hasMany(PostAttachment::class);
}

/**
* Get all of the post's attachments.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function attachments()
{
return $this->morphMany(Attachment::class, 'attachable');
}

/*
* Get all of comment attachments for the post.
*
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
public function commentAttachments()
{
return $this->hasManyThrough(Attachment::class, Comment::class, 'post_id', 'attachable_id')->where('attachable_type', Comment::class);
}
}
10 changes: 8 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
use HasApiTokens;
use HasFactory;
use Notifiable;

/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -42,6 +43,11 @@ class User extends Authenticatable
'email_verified_at' => 'datetime',
];

/**
* A user may have many posts.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function posts()
{
return $this->hasMany(Post::class);
Expand Down
33 changes: 33 additions & 0 deletions database/migrations/2022_02_02_154220_create_attachments_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

class CreateAttachmentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('attachments', function (Blueprint $table) {
$table->id();
$table->morphs('attachable');
$table->string('url');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('attachments');
}
}
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<!-- <server name="DB_CONNECTION" value="sqlite"/> -->
<!-- <server name="DB_DATABASE" value=":memory:"/> -->
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
<server name="MAIL_MAILER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
Expand Down
34 changes: 8 additions & 26 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,19 @@
<table>
<thead>
<tr>
<th>
Title
</th>
<th>
Attachments
</th>
<th>
Comments
</th>
<th> Comment Attachments

</th>
<th>Title</th>
<th>Attachments</th>
<th>Comments</th>
<th>Comment Attachments</th>
</tr>
</thead>
<tbody>
@foreach ($user->posts as $post)
<tr>
<td>
{{ $post->title }}
</td>
<td>
{{ $post->post_attachments()->count() }}
</td>
<td>
{{ $post->comments()->count() }}
</td>
<td>
{{ $post->comments->reduce(function ($carry, $comment) {
return $carry + $comment->comment_attachments()->count();
}) }}
</td>
<td>{{ $post->title }}</td>
<td>{{ $post->attachments_count }}</td>
<td>{{ $post->comments_count }}</td>
<td>{{ $post->comment_attachments_count }}</td>
</tr>
@endforeach
</tbody>
Expand Down
Loading