Skip to content
Snippets Groups Projects
Commit 91bf4eae authored by LIENHARDT QUENTIN's avatar LIENHARDT QUENTIN :snake:
Browse files

User name answer if logged in, else anonymous

parent f261ab2a
Branches
No related merge requests found
......@@ -26,6 +26,7 @@ class AnswerRequest extends FormRequest
return [
'content' => 'required',
'subject_id' => 'nullable|exists:subjects,id',
'user' => 'nullable',
];
}
}
......@@ -10,7 +10,7 @@ class Answer extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = ['content', 'subject_id'];
protected $fillable = ['content', 'subject_id', 'user'];
public function getSubjectClass()
{
......@@ -24,7 +24,13 @@ class Answer extends Model
public function getUserAttribute()
{
// TODO Return real user if logged in
return 'anonymous';
if ($this->attributes['user'])
{
return $this->attributes['user'];
}
else
{
return 'anonymous';
}
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddAuthorToAnswersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('answers', function (Blueprint $table)
{
$table->string('user')
->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('answers', function (Blueprint $table)
{
$table->dropColumn('user');
});
}
}
......@@ -22,6 +22,12 @@
@csrf
{{-- Contains the subject ID --}}
<input type="hidden" name="subject_id" value="{{ $subject->id }}">
{{-- Contains the author --}}
@if (Auth::user())
<input type="hidden" name="user" value="{{ Auth::user()->name }}">
@else
<input type="hidden" name="user" value="">
@endif
<textarea rows="5" name="content" class="w-96 border p-2" placeholder="Your answer"></textarea>
<br>
<button type="submit" class="p-1 bg-green-500 text-white">Answer !</button>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment