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

slove eloquent #408

Open
wants to merge 1 commit 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
4 changes: 3 additions & 1 deletion app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Project;
use App\Models\Stat;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

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

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

return redirect('/')->with('success', 'Projects updated');
}
Expand All @@ -35,7 +37,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
31 changes: 26 additions & 5 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

class UserController extends Controller
{
Expand All @@ -14,15 +16,16 @@ public function index()
// where email_verified_at is not null
// order by created_at desc
// limit 3
$users = DB::select('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::all(); // 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::findOrFail($userId); // TASK: find user by $userId or show "404 not found" page

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

if (!$user) {
$user = new User();
$user->name = $name;
$user->email = $email;
$user->password = bcrypt(Str::random(10)); // Generate a random 10-character password
$user->save();
}

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 = NULL; // updated or created user
$user = User::where('email', $email)->where('name', $name)->first();

if (!$user) {
$user = new User();
$user->name = $name;
$user->email = $email;
$user->password = bcrypt(Str::random(10)); // Generate a random 10-character password
$user->save();
}

return view('users.show', compact('user'));
}
Expand All @@ -52,6 +73,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 All @@ -64,5 +86,4 @@ public function only_active()

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

}
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'];
}
4 changes: 4 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
class Project extends Model
{
use HasFactory, SoftDeletes;

protected $fillable = [
"name"
];
}
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('email_verified_at', 'active');
}
}
53 changes: 53 additions & 0 deletions app/Observers/ProjectObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Observers;

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

class ProjectObserver
{
/**
* Handle the Project "created" event.
*/
public function creating(Project $project): void
{
DB::table('stats')->increment('projects_count');
}
public function created(Project $project): void
{
//
}

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