Skip to content

Commit

Permalink
11 tasks are solved. Thanks.
Browse files Browse the repository at this point in the history
  • Loading branch information
nasirhumbatli committed Oct 15, 2023
1 parent 1eb80e6 commit 7e8511f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
4 changes: 3 additions & 1 deletion app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function mass_update(Request $request)
// where name = $request->old_name

// Insert Eloquent statement below
Project::whereName($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::softDeleted();

return view('projects.index', compact('projects'));
}
Expand All @@ -47,6 +48,7 @@ public function store_with_stats(Request $request)
$project = new Project();
$project->name = $request->name;
$project->save();
Stat::whereId(1)->increment('projects_count', 1);

return redirect('/')->with('success', 'Project created');
}
Expand Down
28 changes: 23 additions & 5 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,14 +16,18 @@ 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')
->orderByDesc('created_at')
->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 = NULL; // TASK: find user by $userId or show "404 not found" page
$user = User::findOrFail($userId);

return view('users.show', compact('user'));
}
Expand All @@ -31,7 +36,12 @@ 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 = NULL;
$user = User::firstOrCreate([
'name' => $name,
'email' => $email,
'password' => Str::random(8),
]);

return view('users.show', compact('user'));
}
Expand All @@ -42,6 +52,13 @@ public function check_update($name, $email)
// if not found, create a user with $name, $email and random password
$user = NULL; // updated or created user

$user = User::updateOrCreate([
'name' => $name
],[
'email' => $email,
'password' => Str::random(8)
]);

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

Expand All @@ -52,6 +69,7 @@ public function destroy(Request $request)
// $request->users is an array of IDs, ex. [1, 2, 3]

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

return redirect('/')->with('success', 'Users deleted');
}
Expand All @@ -60,7 +78,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::where('email_verified_at', '!=', 'NULL')->get();

return view('users.index', compact('users'));
}
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'];
}
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'];
}

0 comments on commit 7e8511f

Please sign in to comment.