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

Test-Eloquent-Relationship #286

Open
wants to merge 2 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
9 changes: 7 additions & 2 deletions app/Http/Controllers/CountryController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Log;

use App\Models\Country;

Expand All @@ -9,8 +10,12 @@ class CountryController extends Controller
public function index()
{
// TASK: load the relationship average of team size
$countries = Country::all();

//$countries = Country::all();
$countries = Country::with(['teams' => function ($query) {
$query->selectRaw('country_id, AVG(size) as teams_avg_size')
->groupBy('country_id');
}])->get();
Log::info($countries);
return view('countries.index', compact('countries'));
}
}
2 changes: 2 additions & 0 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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
$user = auth()->user();
$user->projects()->attach($request->project_id,['start_date' => $request->start_date]);

return 'Success';
}
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Task;
use Illuminate\Http\Request;


class TaskController extends Controller
{
public function index()
Expand All @@ -16,6 +17,7 @@ public function index()

public function store(Request $request)
{

// TASK: find out why this sentence fails, and fix it in Eloquent Model
auth()->user()->tasks()->create([
'name' => $request->name
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ class UserController extends Controller
{
public function index()
{
$users = User::all();
//$users = User::all();

$users = User::has('projects')->get();

return view('users.index', compact('users'));
}

public function show(User $user)
{
$user = User::with('comments')->findOrFail($user->id);
return view('users.show', compact('user'));
}
}
1 change: 1 addition & 0 deletions app/Models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ class Attachment extends Model
public function attachable()
{
// TASK: fill in the code to make it work
return $this->morphTo();
}
}
2 changes: 1 addition & 1 deletion app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class Role extends Model
public function users()
{
// TASK: fix this by adding a parameter
return $this->belongsToMany(User::class);
return $this->belongsToMany(User::class,'users_roles');
}
}
7 changes: 6 additions & 1 deletion app/Models/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class Task extends Model

public function user()
{
return $this->belongsTo(User::class, 'users_id');
return $this->belongsTo(User::class, 'users_id')->withDefault();
}

public function comments(){

return $this->hasMany(Comment::class);
}
}
6 changes: 5 additions & 1 deletion app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ 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)->withPivot('position', 'created_at', 'updated_at');
}
public function country()
{
return $this->belongsTo(Country::class);
}

}
3 changes: 2 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ class User extends Authenticatable
public function tasks()
{
// TASK: fix this by adding a parameter
return $this->hasMany(Task::class);
return $this->hasMany(Task::class,'users_id');
}

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

public function projects()
Expand Down
2 changes: 1 addition & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// \App\Models\User::factory(10)->create();
\App\Models\User::factory(10)->create();
}
}
2 changes: 1 addition & 1 deletion resources/views/countries/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<ul>
@foreach ($countries as $country)
<li>{{ $country->name }} (avg team size {{ $country->teams_avg_size }})</li>
<li>{{ $country->name }} (avg team size {{ $country->teams->first()->teams_avg_size ?? 0 }})</li>
@endforeach
</ul>
Loading