Skip to content

Commit

Permalink
complete eloquent basics tesk
Browse files Browse the repository at this point in the history
  • Loading branch information
rafihossain committed Dec 6, 2024
1 parent 1eb80e6 commit ae6b20e
Show file tree
Hide file tree
Showing 10 changed files with 1,452 additions and 974 deletions.
6 changes: 5 additions & 1 deletion app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ 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 +39,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
35 changes: 31 additions & 4 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ 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')
->take(3)
->get(); // replace this with Eloquent statement

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::find($userId); // TASK: find user by $userId

if(empty($user)){
abort(404); // show 404 page if user not found
}

return view('users.show', compact('user'));
}
Expand All @@ -31,7 +38,15 @@ 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::findByNameAndEmail($name, $email)->first();

if(empty($user)){
$user = User::create([
'name' => $name,
'email' => $email,
'password' => bcrypt(\Illuminate\Support\Str::random(8)), // generate random password
]);
}

return view('users.show', compact('user'));
}
Expand All @@ -40,7 +55,18 @@ 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->email = $email;
$user->save(); // update user
}else{
$user = User::create([
'name' => $name,
'email' => $email,
'password' => bcrypt(\Illuminate\Support\Str::random(8)), // generate random password
]); // created 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::whereIn('id',$request->users)->delete();

return redirect('/')->with('success', 'Users deleted');
}
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Morningnews.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ class Morningnews extends Model
{
use HasFactory;

protected $table = 'morning_news';

protected $fillable = ['title', 'news_text'];
}
5 changes: 5 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

namespace App\Models;

use App\Observers\ProjectObserver;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

// #[ObservedBy([ProjectObserver::class])]
class Project extends Model
{
use HasFactory, SoftDeletes;

protected $fillable = ['name', 'created_at', 'updated_at', 'deleted_at'];
}
11 changes: 11 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,15 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

public function scopeFindByNameAndEmail($query, $name, $email)
{
return $query->where('name', $name)->where('email', $email);
}

public function scopeActive($query)
{
return $query->whereNotNull('email_verified_at');
}

}
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\Project;
use Illuminate\Support\Facades\DB;

class ProjectObserver
{
/**
* Handle the Project "created" event.
*/
public function created(Project $project): void
{
DB::statement('UPDATE stats SET projects_count = projects_count + 1');
}

}
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);
}
}
Loading

0 comments on commit ae6b20e

Please sign in to comment.