Skip to content

Commit f11b668

Browse files
authored
Merge pull request #9 from JeremyParker/add-word-to-category
Add word to a category
2 parents 5c37c4f + eb583f0 commit f11b668

24 files changed

+205
-57
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@ php artisan cache:clear
5757
php artisan view:clear
5858
php artisan route:clear
5959
```
60+
61+
To check the phpinfo for debugging, go to http://localhost/phpinfo-secret.php
62+
TODO: this is a security issue.

app/Http/Controllers/CategoryController.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ public function createCategory(Request $request)
4242
return back()->withInput()->withErrors('Error. See logs');
4343
}
4444

45-
public function showCategories()
46-
{
47-
return view('categories');
48-
}
49-
5045
public function showEditCategory(Category $category)
5146
{
5247
return view('category', ['category' => $category]);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\{Word, Category, CategoryWord};
6+
use Illuminate\Http\Request;
7+
8+
class CategoryWordController extends Controller
9+
{
10+
protected $word;
11+
protected $category;
12+
13+
// Show the form for creating a new CategoryWord instance
14+
public function showCreate(Request $request)
15+
{
16+
$wordId = $request->query('word');
17+
$categoryId = $request->query('category');
18+
$returnTo = $request->query('returnTo');
19+
20+
$word = Word::findOrFail($wordId);
21+
$category = Category::findOrFail($categoryId);
22+
return view('categoryword.create', [
23+
'word' => $word,
24+
'category' => $category,
25+
'returnTo' => $returnTo,
26+
]);
27+
}
28+
29+
// Store the new CategoryWord instance
30+
public function store(Request $request)
31+
{
32+
$validated = $request->validate([
33+
'word_id' => 'required|exists:words,id',
34+
'category_id' => 'required|exists:categories,id',
35+
'difficulty_override' => 'nullable|numeric',
36+
'example_sentence' => 'nullable|string',
37+
]);
38+
39+
// Create a new CategoryWord instance
40+
CategoryWord::create($validated);
41+
42+
// return to either the edit word page, or edit category page depending on where we came from
43+
$returnTo = $request->input('return_to');
44+
if ($returnTo === 'word') {
45+
return redirect()->route('showEditWord', $validated['word_id'])->with('success', 'successfully added to that category!');
46+
} else {
47+
return redirect()->route('showEditCategory', $validated['category_id'])->with('success', 'word added successfully!');
48+
}
49+
}
50+
}

app/Http/Controllers/WordController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
use Illuminate\Http\Request;
88
use Illuminate\Validation\Rule;
99
use Illuminate\Support\Facades\DB;
10+
use Illuminate\Support\Facades\Log;
1011
use Illuminate\Database\QueryException;
1112

1213
class WordController extends Controller
1314
{
1415
public function showWords()
1516
{
16-
$words = Word::orderBy('updated_at', 'desc')->get();
17-
return view('words', ['words' => $words]);
17+
return view('words');
1818
}
1919

2020
public function showEditWord(Word $word)
@@ -43,9 +43,9 @@ public function createWord(Request $request)
4343
return back()->with('success', 'Word saved. Thanks!');
4444
}
4545
} catch (Exception $e) {
46-
\Log::error($e->getMessage());
46+
Log::error($e->getMessage());
4747
} catch (QueryException $qe) {
48-
\Log::error($qe->getMessage());
48+
Log::error($qe->getMessage());
4949
}
5050
return back()->withInput()->withErrors('Error: ' . $e->getMessage());
5151
}
@@ -71,9 +71,9 @@ public function updateWord(Word $word, Request $request)
7171
return redirect('/words')->with('success', 'Word saved. Thanks!');
7272
}
7373
} catch (Exception $e) {
74-
\Log::error($e->getMessage());
74+
Log::error($e->getMessage());
7575
} catch (QueryException $qe) {
76-
\Log::error($qe->getMessage());
76+
Log::error($qe->getMessage());
7777
}
7878
return back()->withInput()->withErrors('Error: ' . $e->getMessage());
7979
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Http\Livewire;
4+
5+
use App\Models\Word;
6+
use Livewire\Component;
7+
use Livewire\Attributes\On;
8+
9+
/**
10+
* Component that shows the list of categories so the user can choose which category to add the word to
11+
*/
12+
class AddWordToCategory extends Component
13+
{
14+
public $layout = 'components.layouts.app';
15+
public Word $word;
16+
17+
public function mount(Word $word)
18+
{
19+
$this->word = $word;
20+
}
21+
22+
public function render(Word $word)
23+
{
24+
return view('livewire.add-word-to-category', ['word' => $word]);
25+
}
26+
27+
#[On('categorySelected')]
28+
public function categorySelected($categoryId)
29+
{
30+
return redirect()->to(route('category_word.create', [
31+
'category' => $categoryId,
32+
'word' => $this->word->id,
33+
'returnTo' => 'word',
34+
]));
35+
}
36+
}

app/Http/Livewire/Categories.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Http\Livewire;
4+
5+
use Livewire\Component;
6+
use Livewire\Attributes\On;
7+
8+
class Categories extends Component
9+
{
10+
public $layout = 'components.layouts.app';
11+
12+
public function render()
13+
{
14+
return view('livewire.categories');
15+
}
16+
17+
#[On('categorySelected')]
18+
public function categorySelected($categoryId)
19+
{
20+
return redirect()->to(route('showEditCategory', ['category' => $categoryId]));
21+
}
22+
}

app/Http/Livewire/CategoryList.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
77

88
class CategoryList extends Component
99
{
10-
public $categories;
10+
public $layout = 'components.layouts.app';
11+
1112
public $search = '';
1213

1314
public function render()
1415
{
15-
$this->categories = Category::whereRaw('LOWER(name) LIKE ?', ['%' . strtolower($this->search) . '%'])
16+
$categories = Category::whereRaw('LOWER(name) LIKE ?', ['%' . strtolower($this->search) . '%'])
1617
->orderBy('updated_at', 'desc')
1718
->get();
18-
return view('livewire.category-list', ['categories' => $this->categories]);
19+
return view('livewire.category-list', ['categories' => $categories]);
20+
}
21+
22+
public function onSelected($categoryId)
23+
{
24+
$this->dispatch('categorySelected', categoryId: $categoryId);
1925
}
2026
}

app/Http/Livewire/WordForm.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88

99
class WordForm extends Component
1010
{
11+
public $layout = 'components.layouts.app';
1112
public Word $word;
12-
1313
public String $text;
14-
1514
public bool $isTopical;
1615

1716
public function mount(Word $word)

app/Http/Livewire/WordList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class WordList extends Component
99
{
10+
public $layout = 'components.layouts.app';
1011
public $words;
1112
public $search = '';
1213
public bool $isTopical;

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
"name": "laravel/laravel",
33
"type": "project",
44
"description": "The skeleton application for the Laravel framework.",
5-
"keywords": ["laravel", "framework"],
5+
"keywords": [
6+
"laravel",
7+
"framework"
8+
],
69
"license": "MIT",
710
"require": {
811
"php": "^8.1",

0 commit comments

Comments
 (0)