Skip to content

Commit

Permalink
Solve Eloquent basics test
Browse files Browse the repository at this point in the history
  • Loading branch information
ANAS29-CODER committed Jan 9, 2025
1 parent 1eb80e6 commit b67cca3
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 6 deletions.
6 changes: 5 additions & 1 deletion app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ public function store(Request $request)

public function mass_update(Request $request)
{


// TASK: Transform this SQL query into Eloquent
// update projects
// set name = $request->new_name
// 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');
}
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
24 changes: 20 additions & 4 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

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

$users = User::all(); // replace this with Eloquent statement
//$users = User::all(); // replace this with Eloquent statement

$users=User::where('email_verified_at','!=','null')->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

$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::firstOrCreate(
['name' => $name, 'email' => $email],
['password' => bcrypt(Str::password())]
);;

return view('users.show', compact('user'));
}
Expand All @@ -40,7 +50,12 @@ 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

// updated or created user
$user =User::updateOrCreate(
['name' => $name],
['email' => $email, 'password' => bcrypt(Str::password())]
);

return view('users.show', compact('user'));
}
Expand All @@ -51,6 +66,7 @@ public function destroy(Request $request)
// SQL: delete from users where id in ($request->users)
// $request->users is an array of IDs, ex. [1, 2, 3]

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

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 @@ -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'];
}
11 changes: 11 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
Expand All @@ -12,6 +13,10 @@ class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;





/**
* The attributes that are mass assignable.
*
Expand Down Expand Up @@ -41,4 +46,10 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];


public function scopeActive(Builder $query){

$query->where('email_verified_at','!=','null');
}
}
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\Project;
use App\Models\Stat;

class ProjectObserver
{
/**
* Handle the Project "created" event.
*/
public function created(Project $project): void
{

Stat::first()->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);
}
}

0 comments on commit b67cca3

Please sign in to comment.