diff --git a/app/Http/Controllers/ItemController.php b/app/Http/Controllers/ItemController.php index 8459127..a05ff0d 100644 --- a/app/Http/Controllers/ItemController.php +++ b/app/Http/Controllers/ItemController.php @@ -6,7 +6,6 @@ use App\Models\Item; use App\Models\Question; use Illuminate\Http\Request; -use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Storage; use Inertia\Inertia; use Vinkla\Hashids\Facades\Hashids; diff --git a/app/Http/Controllers/QuestionController.php b/app/Http/Controllers/QuestionController.php index 03a9f4f..5971a76 100644 --- a/app/Http/Controllers/QuestionController.php +++ b/app/Http/Controllers/QuestionController.php @@ -2,7 +2,6 @@ namespace App\Http\Controllers; -use App\Models\Answer; use App\Models\AttributeType; use App\Models\Question; use Illuminate\Http\Request; @@ -14,7 +13,7 @@ class QuestionController extends Controller public function index() { return Inertia::render('Questions/Index', [ - 'questions' => Question::all() + 'questions' => Question::all(), ]); } @@ -22,7 +21,7 @@ public function create() { return Inertia::render('Questions/Edit', [ 'question' => null, - 'attributeTypes' => AttributeType::with('attributes:id,title,attribute_type_id', 'attributes.attributeType:id,color')->get() + 'attributeTypes' => AttributeType::with('attributes:id,title,attribute_type_id', 'attributes.attributeType:id,color')->get(), ]); } @@ -34,7 +33,7 @@ public function edit($id) { return Inertia::render('Questions/Edit', [ 'question' => Question::with('answers', 'answers.attributes:id,title,attribute_type_id', 'answers.attributes.attributeType:id,color')->findOrFail($id), - 'attributeTypes' => AttributeType::with('attributes:id,title,attribute_type_id', 'attributes.attributeType:id,color')->get() + 'attributeTypes' => AttributeType::with('attributes:id,title,attribute_type_id', 'attributes.attributeType:id,color')->get(), ]); } @@ -43,19 +42,21 @@ public function update(Request $request, $id) $question = Question::findOrFail($id); $question->update($request->only('text', 'description')); foreach ($request->input('answers') as $newAnswer) { - $answer = $question->answers()->updateOrCreate(['id' => $newAnswer['id']??null], ['text' => $newAnswer['text']]); + $answer = $question->answers()->updateOrCreate(['id' => $newAnswer['id'] ?? null], ['text' => $newAnswer['text']]); $answer->attributes()->sync(collect($newAnswer['attributes'])->pluck('id')); } + return to_route('questions.edit', $question->id)->with('success', 'Question saved!'); } public function destroy($id) { - DB::transaction(function () use ($id) { + DB::transaction(static function () use ($id): void { $question = Question::findOrFail($id); $question->answers()->delete(); $question->delete(); }); + return redirect()->route('questions.index')->with('success', 'Question deleted!'); } } diff --git a/app/Models/Answer.php b/app/Models/Answer.php index f9b6575..3c2106c 100644 --- a/app/Models/Answer.php +++ b/app/Models/Answer.php @@ -10,7 +10,9 @@ class Answer extends Model { use HasFactory; + protected $hidden = ['created_at', 'updated_at']; + protected $fillable = [ 'text', ]; diff --git a/app/Models/Question.php b/app/Models/Question.php index 955a19f..243161a 100644 --- a/app/Models/Question.php +++ b/app/Models/Question.php @@ -20,5 +20,4 @@ public function answers(): HasMany { return $this->hasMany(Answer::class); } - } diff --git a/database/migrations/2024_06_17_124214_create_questions_table.php b/database/migrations/2024_06_17_124214_create_questions_table.php index 812ad73..9c667da 100644 --- a/database/migrations/2024_06_17_124214_create_questions_table.php +++ b/database/migrations/2024_06_17_124214_create_questions_table.php @@ -4,7 +4,8 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration { +return new class extends Migration +{ public function up(): void { Schema::create('questions', function (Blueprint $table) { diff --git a/database/migrations/2024_06_17_124332_create_answers_table.php b/database/migrations/2024_06_17_124332_create_answers_table.php index 23ab2c0..fefb33c 100644 --- a/database/migrations/2024_06_17_124332_create_answers_table.php +++ b/database/migrations/2024_06_17_124332_create_answers_table.php @@ -4,7 +4,8 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration { +return new class extends Migration +{ public function up(): void { Schema::create('answers', function (Blueprint $table) {