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-Laravel-Eloquent-Basics #347

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 3 additions & 2 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public function store(Request $request)
Project::create([
'name' => $request->name
]);

return redirect('/')->with('success', 'Project created');
}

Expand All @@ -27,6 +26,8 @@ public function mass_update(Request $request)

// Insert Eloquent statement below

Project::where('name', $request->old_name)->update(['name' => $request->new_name]);

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

Expand All @@ -35,7 +36,7 @@ public function destroy($projectId)
Project::destroy($projectId);

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

return view('projects.index', compact('projects'));
}
Expand Down
53 changes: 39 additions & 14 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,67 @@

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

class UserController extends Controller
{
public function index()
{
// TASK: turn this SQL query into Eloquent
// select * from users
// where email_verified_at is not null
// 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

$user = User::findorFail($userId);
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::where('name', $name)
->where('email', $email)
->first();

if (!$user) {
$password = Str::random(8);

$user = User::create([
'name' => $name,
'email' => $email,
'password' => bcrypt($password), // Hash the password using bcrypt
]);
}

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


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::where('name', $name)->first();

if ($user) {
$user->update([
'email' => $email,
]);
} else {
$password = Str::random(8);

$user = User::create([
'name' => $name,
'email' => $email,
'password' => Hash::make($password),
]);
}

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

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

return redirect('/')->with('success', 'Users deleted');
}
Expand All @@ -60,7 +85,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::active()->get();;

return view('users.index', compact('users'));
}
Expand Down
1 change: 1 addition & 0 deletions app/Models/Morningnews.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class Morningnews extends Model
{
protected $table = 'morning_news';
use HasFactory;

protected $fillable = ['title', 'news_text'];
Expand Down
1 change: 1 addition & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@

class Project extends Model
{
protected $fillable = ['name'];
use HasFactory, SoftDeletes;
}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

public function scopeActive($query)
{
return $query->where('active', true);
}
}
18 changes: 18 additions & 0 deletions app/Observers/ProjectObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Observers;

use App\Models\Stat;
use Illuminate\Support\Facades\DB;

class ProjectObserver
{
public function created($project)

{
$project->save();
// DB::update('update stats set projects_count = projects_count + 1');
Stat::increment('projects_count');

}
}
5 changes: 4 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,7 @@ public function register()
*/
public function boot()
{
//
Project::observe(ProjectObserver::class);

}
}
Loading