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

laravel Elquent test #311

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
7 changes: 5 additions & 2 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public function mass_update(Request $request)
// where name = $request->old_name

// Insert Eloquent statement below

$projects = Project::where('name',$request->old_name)
->update([
'name' => $request->new_name
]);
return redirect('/')->with('success', 'Projects updated');
}

Expand All @@ -35,7 +38,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()->all();

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

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

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

$users = User::all(); // replace this with Eloquent statement
$users = User::whereNotNull('email_verified_at')->orderByDesc('created_at')->take(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); // TASK: find user by $userId or show "404 not found" page

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' => Hash::make(rand()),
]);
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::updateOrCreate([
'email' => $email,
], [
'name' => $name,
'password' => bcrypt(Str::random(8)),
]);


return view('users.show', compact('user'));
}
Expand All @@ -53,6 +66,7 @@ public function destroy(Request $request)

// Insert Eloquent statement here

User::destroy($request->users);
return redirect('/')->with('success', 'Users deleted');
}

Expand Down
1 change: 1 addition & 0 deletions app/Models/Morningnews.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ class Morningnews extends Model
{
use HasFactory;

protected $table = 'morning_news';
protected $fillable = ['title', 'news_text'];
}
3 changes: 3 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
class Project extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = [
'name',
];
}
4 changes: 4 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 Down Expand Up @@ -41,4 +42,7 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];
public function scopeActive(Builder $query){
$query->whereNotNull('email_verified_at');
}
}
52 changes: 52 additions & 0 deletions app/Observers/projectObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Observers;

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

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

Stat::query()->update(['projects_count'=> + 1]);

}

/**
* 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
{
//
}
}
11 changes: 10 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Providers;

use App\Models\Project;
use App\Observers\projectObserver;
use Illuminate\Support\Facades\Event;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

class EventServiceProvider extends ServiceProvider
{
Expand All @@ -20,6 +22,13 @@ class EventServiceProvider extends ServiceProvider
],
];

protected $observers = [
Project::class => [
projectObserver::class,
],
];


/**
* Register any events for your application.
*
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"require": {
"php": "^8.1",
"doctrine/dbal": "^3.6",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.0",
"laravel/sanctum": "^3.2",
Expand Down
Loading
Loading