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

Completed all the Eloquent Relationship Tasks #307

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion app/Http/Controllers/CountryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CountryController extends Controller
public function index()
{
// TASK: load the relationship average of team size
$countries = Country::all();
$countries = Country::withAvg('teams', 'size')->get();

return view('countries.index', compact('countries'));
}
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use Auth;
use Illuminate\Http\Request;

class ProjectController extends Controller
Expand All @@ -11,6 +12,8 @@ public function store(Request $request)
// TASK: Add one sentence to save the project to the logged-in user
// by $request->project_id and with $request->start_date parameter

Auth::user()->projects()->attach($request->project_id, ['start_date' => $request->start_date]);

return 'Success';
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class UserController extends Controller
{
public function index()
{
$users = User::all();
$users = User::has('projects')->get();

return view('users.index', compact('users'));
}
Expand Down
5 changes: 3 additions & 2 deletions app/Models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Attachment extends Model
{
use HasFactory;

protected $fillable = ['filename', 'attachable_id', 'attachable_type'];

public function attachable()
public function attachable() : MorphTo
{
// TASK: fill in the code to make it work
return $this->morphTo();
}
}
5 changes: 3 additions & 2 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Role extends Model
{
use HasFactory;

protected $fillable = ['name'];

public function users()
public function users() : BelongsToMany
{
// TASK: fix this by adding a parameter
return $this->belongsToMany(User::class);
return $this->belongsToMany(User::class, 'users_roles');
}
}
2 changes: 1 addition & 1 deletion app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Team extends Model
public function users()
{
// TASK: fix this by adding some extra code
return $this->belongsToMany(User::class);
return $this->belongsToMany(User::class, 'created_at', 'position');
}

}
22 changes: 16 additions & 6 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace App\Models;

use DB;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
Expand Down Expand Up @@ -42,19 +46,25 @@ class User extends Authenticatable
'email_verified_at' => 'datetime',
];

public function tasks()
public function tasks() : HasMany
{
// TASK: fix this by adding a parameter
return $this->hasMany(Task::class);
return $this->hasMany(Task::class, 'users_id');
}

public function comments()
public function comments() : HasManyThrough
{
// TASK: add the code here for two-level relationship
return $this->hasManyThrough(
Comment::class,
Task::class,
'users_id'
);
}

public function projects()
public function projects() : BelongsToMany
{
return $this->belongsToMany(Project::class)->withPivot('start_date');
return $this->belongsToMany(Project::class)
->withPivot('start_date');
}

}
4 changes: 2 additions & 2 deletions resources/views/tasks/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<ul>
@foreach ($tasks as $task)
<li>{{ $task->name }} ({{ $task->user->name }})</li>
<li>{{ $task->name }} </li>
@endforeach
</ul>
</ul>
Loading