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

complete eloquent tasks #433

Open
wants to merge 3 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ Test method `test_create_model_incorrect_table()`.
In `app/Http/Controllers/UserController.php` file method `index()`, write Eloquent query to get 3 newest users with verified emails, ordered from newest to oldest. Transform this SQL query into Eloquent:

```
select * from users where email_verified_at is not null order by created_at desc limit 3
select *
from users
where email_verified_at is not null
order by created_at desc limit 3
```

Test method `test_get_filtered_list()`.
Expand Down Expand Up @@ -74,7 +77,9 @@ Test method `test_create_project()`.
In `app/Http/Controllers/ProjectController.php` file method `mass_update()`, write the update SQL query as Eloquent statement.

```
update projects set name = $request->new_name where name = $request->old_name
update projects
set name = $request->new_name
where name = $request->old_name
```

Test method `test_mass_update_projects()`.
Expand Down
7 changes: 4 additions & 3 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Stat;
use Illuminate\Http\Request;


class ProjectController extends Controller
{
public function store(Request $request)
Expand All @@ -26,16 +27,16 @@ public function mass_update(Request $request)
// where name = $request->old_name

// Insert Eloquent statement below
Project::where('name', $request->old_name)
->update(['name' => $request->new_name]);

return redirect('/')->with('success', 'Projects updated');
}

public function destroy($projectId)
{
Project::destroy($projectId);

// TASK: change this Eloquent statement to include the soft-deletes records
$projects = Project::all();
$projects = Project::find($projectId)->delete();

return view('projects.index', compact('projects'));
}
Expand Down
41 changes: 34 additions & 7 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Support\Str;
use Illuminate\Http\Request;

class UserController extends Controller
Expand All @@ -15,23 +16,41 @@ public function index()
// order by created_at desc
// limit 3

$users = User::all(); // replace this with Eloquent statement
$users = User::whereNotNull('email_verified_at')
->orderBy('created_at', 'desc')
->limit(3)
->get();

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

public function show($userId)
{
$user = NULL; // TASK: find user by $userId or show "404 not found" page
// TASK: find user by $userId or show "404 not found" page
// Find the user by $userId
$user = User::find($userId);

return view('users.show', compact('user'));
}
// If user is not found, abort with 404
if (!$user) {
abort(404, 'User not found');
}

return view('users.show', compact('user'));
}

public function check_create($name, $email)
{
// TASK: find a user by $name and $email
// if not found, create a user with $name, $email and random password
$user = NULL;
$user = User::firstOrCreate(
[
'name' => $name,
'email' => $email,
],
[
'password' => bcrypt(Str::random(10)),
]
);

return view('users.show', compact('user'));
}
Expand All @@ -40,7 +59,14 @@ public function check_update($name, $email)
{
// TASK: find a user by $name and update it with $email
// if not found, create a user with $name, $email and random password
$user = NULL; // updated or created user

$user = User::updateOrCreate(
['name' => $name], // Search criteria
[
'email' => $email,
'password' => bcrypt(Str::random(10)) // Set password if creating new user
]
);

return view('users.show', compact('user'));
}
Expand All @@ -52,6 +78,7 @@ public function destroy(Request $request)
// $request->users is an array of IDs, ex. [1, 2, 3]

// Insert Eloquent statement here
User::destroy($request->users);

return redirect('/')->with('success', 'Users deleted');
}
Expand All @@ -60,7 +87,7 @@ public function only_active()
{
// TASK: That "active()" doesn't exist at the moment.
// Create this scope to filter "where email_verified_at is not null"
$users = User::active()->get();
$users = User::whereNotNull('email_verified_at')->get();

return view('users.index', compact('users'));
}
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Morningnews.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ class Morningnews extends Model
use HasFactory;

protected $fillable = ['title', 'news_text'];
protected $table = 'morning_news';

}
2 changes: 2 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
class Project extends Model
{
use HasFactory, SoftDeletes;

protected $fillable = ['name'];
}
49 changes: 49 additions & 0 deletions app/Observers/ProjectObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Observers;

use App\Models\Stat;
use App\Models\Project;

class ProjectObserver
{
/**
* Handle the Project "created" event.
*/
public function created(Project $project): void
{
Stat::increment('projects_count');
}

/**
* Handle the Project "updated" event.
*/
public function updated(Project $project): void
{
//
}

/**
* Handle the Project "deleted" event.
*/
public function deleted(Project $project): void
{
//
}

/**
* Handle the Project "restored" event.
*/
public function restored(Project $project): void
{
//
}

/**
* Handle the Project "force deleted" event.
*/
public function forceDeleted(Project $project): void
{
//
}
}
4 changes: 3 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Models\Project;
use App\Observers\ProjectObserver;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -23,6 +25,6 @@ public function register()
*/
public function boot()
{
//
Project::observe(ProjectObserver::class);
}
}