This is my TaskController where done_at is optional and their is relation with 1 model named Staff.
public function create()
{
$staffs = Staff::all();
return view('tasks.create', compact('staffs'));
}
public function store(Request $request)
{
$request->validate([
'staff_id' => 'required',
'task' => 'required',
'done_at' => 'sometimes',
]);
$task = Task::create([
'staff_id' => $request->input('staff_id'),
'task' => $request->input('task'),
'done_at' => $request->input('done_at'),
]);
return redirect()->route('tasks.index')->withSuccess('Done');
}
This is my Task model where their is relationship with staff model.
use HasFactory;
protected $table = 'tasks';
protected $fillable = [
'staff_id',
'task',
];
public function staff(){
return $this->belongsTo(Staff::class);
}
This is my Staff model with has relationship with task model.
use HasFactory;
protected $table = 'staffs';
protected $fillable = [
'name',
'gender',
'date_of_birth',
];
public function task() {
return $this->hasMany(Task::class);
}
This is my tasks.create page where there is form which is not submitting the done_at field is optional.
<form method="post" action="">
@csrf
<table>
<tr>
<td>Staff Name : </td>
<td>
<select name="staff_id" id="staff_id">
<option value="">Select One</option>
@foreach ($staffs as $staff)
<option value=""></option>
@endforeach
</select>
</td>
</tr>
<tr>
<td>Task : </td>
<td><input type="text" name="task" class="form-control"></td>
</tr>
<tr>
<td>Done At :</td>
<td><input type="time" name="done_at" class="form-control"></td>
</tr>
<td><button class="btn btn-primary" name="submit" type="submit" value="submit" id="submit">Submit</button></td>
</table>
</form>
This is my tasks table and this is structure of Task.
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->bigInteger('staff_id')->nullable();
$table->string('task')->nullable();
$table->dateTime('done_at')->nullable();
$table->timestamps();
});
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire