I make task manager there is a form where to assign task I want to do that if user select multiple option then it is seperated by comma
This is my form
<form method="post" action="{{route('assignments.store')}}">
#csrf
<table>
<tr>
<td>Task Title : </td>
<td>
<select name="task_id" id="task_id">
#foreach ($tasks as $task)
<option value="{{ $task->id }}">{{ $task->title }}</option>
#endforeach
</select>
</td>
</tr>
<tr>
<td>Staff Name : </td>
<td>
<select name="staff_id" multiple>
<option value=null>Select One</option>
#foreach ($staffs as $staff)
<option value="{{ $staff->id }}">{{ $staff->name }}</option>
#endforeach
</select>
</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">Submit</button></td>
</table>
</form>
And this is my store function from where I storing the data recieved from form
$request->validate([
'staff_id' => 'required',
'task_id' => 'required',
'done_at' => 'sometimes',
]);
$assignment = new Assignment();
$assignment->staff_id = $request->staff_id;
$assignment->task_id = $request->task_id;
$assignment->done_at = $request->done_at;
$assignment->save();
return redirect()->route('assignments.index', compact('assignment'))->withSuccess('Done');
try this,
change the name=staff_id[]
<select id="staff" name="staff_id[]" multiple="multiple">
---
</select>
you can check the by
dd(implode(',', $request->staff_id));
$assignment = new Assignment();
$assignment->staff_id = implode(',', $request->staff_id);
And ya one more thing I forgot to tell you in your database I think staff_id is f.k so it will allow one id at the item to store so what you have to do CHANGE staff_id with type varchar then it will allow to store value with comma separate.
And below is the image form implode we are converting an array to string
Please set select name = staff_id[] then only you will get multiple data
Related
I make task management system and i don't know well how to assign task to multiple users at same time?
This is my AssignmentController Store function.
public function store(Request $request)
{
$request->validate(array(
'assignment' => 'array|required',
'assignment.*.assigned_to' => 'required',
'task_title' => 'required',
'task_description' => 'required',
'done_at' => 'sometimes',
));
foreach($request->get('assignment') as $assignment)
$assignment = Assignment::create(array(
'task_title' => $request->input('task_title'),
'task_description' => $request->input('task_description'),
'assigned_to' => $assignment['assigned_to'],
'done_at' => $request->input('done_at'),
));
}
This is my assignment.create page where is 3 field assigned_to, task_title and task_description done_at is optional
<form method="post" action="{{route('assignments.store')}}">
#csrf
<table>
<tr>
<td>Staff Name : </td>
<td>
<select name="assignment[{{$key}}][assigned_to]" id="assigned_to" multiple>
<option value="">Select One</option>
#foreach ($staffs as $staff)
<option value="{{ $staff->id }}">{{ $staff->name }}</option>
#endforeach
</select>
</td>
</tr>
<tr>
<td>Task Title : </td>
<td><input type="text" name="task_title" class="form-control"></td>
</tr>
<tr>
<td>Task Description : </td>
<td><input type="text" name="task_description" 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>
You have users table
id
name
and assignments table
id
title
description
And you must have user_assignment table
user_id
assignment_id
This table define many to many relationship between users and assignments
Which means that each user has many tasks and each task belongs to many users
read the documentation :
https://laravel.com/docs/7.x/eloquent-relationships#many-to-many
This is my create page. Here there is form in foreach loop having on submit button. (This is my create page. Here there is form in foreach loop having on submit button.)
<form method="post" action="{{route('types.store')}}">
#csrf
<table>
#foreach ($students as $student)
<tr>
<td>Name : </td>
<td><input type="text" class="form-control" name="name" value="{{ $student->name }}" readonly></td>
</tr>
<tr>
<td>Type : </td>
<td>
<select name="type" id="type">
<option value="present">present</option>
<option value="absent">absent</option>
<option value="half_day">half day</option>
</select>
</td>
</tr>
<tr>
<td>Date : </td>
<td><input type="date" class="form-control" name="date"></td>
</tr>
#endforeach
<button class="btn btn-primary" name="submit" id="submit">Submit</button>
</table>
</form>
This is my TypeController. This is my TypeController. I want to store data in foreach loop. (This is my TypeController. This is my TypeController. I want to store data in foreach loop)
public function create()
{
$students = Student::all();
return view('types.create', compact('students'));
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'type' => 'required',
'date' => 'required',
]);
$type = Type::create([
'name' => $request->input('name'),
'type' => $request->input('type'),
'date' => $request->input('date'),
]);
return redirect()->route('types.index')->withSuccess('Done');
}
This is my Type model which has relation with Student table (This is my Type model which has relation with Student table)
use HasFactory;
protected $table = 'types';
protected $fillable = [
'name',
'date',
];
public function student()
{
return $this->belongsTo(Student::class);
}
Make array for each input like given below:
<form method="post" action="{{route('types.store')}}">
#csrf
<table>
#foreach ($students as $student)
<tr>
<td>Name : </td>
<td><input type="text" class="form-control" name="name[]" value="{{ $student->name }}" readonly></td>
</tr>
<tr>
<td>Type : </td>
<td>
<select name="type[]" id="type">
<option value="present">present</option>
<option value="absent">absent</option>
<option value="half_day">half day</option>
</select>
</td>
</tr>
<tr>
<td>Date : </td>
<td><input type="date" class="form-control" name="date[]"></td>
</tr>
#endforeach
<button class="btn btn-primary" name="submit" id="submit">Submit</button>
</table>
</form>
In my Laravel-5.8 project I am working on a leave application that will iterate with employee_type. The fields in the view blade are array elements. The code is as shown below:
#foreach ($employeetypes as $key=> $employeetype)
<tr>
<td width="5%">
{{$key+1}}
</td>
<td width="30%">
<span>{{$employeetype->employee_type_name}}</span>
</td>
<td width="20%"><input type="number" name="no_of_days[]" placeholder="Enter leave days here" class="form-control no_of_days" max="120">
</td>
<td width="15%">
<select class="form-control select2bs4" data-placeholder="Select Applicable Gender" tabindex="1" name="leave_applicable_gender[]">
<option value="0" selected="true" disabled="true">Select Applicable Gender</option>
#foreach(AppHelper::LEAVE_APPLICABLE_GENDER as $key1 => $value)
<option value="{{ $key1 }}" {{ (($applicableGender == $key1) || (is_null($applicableGender) && $key1 == 1)) ? 'selected' : '' }}>
{{ $value }}
</option>
#endforeach
</select>
</td>
<td width="10%"><input type="checkbox" name="weekend_inclusive{{$key}}" class="form-control" unchecked data-bootstrap-switch data-off-color="danger" data-on-color="success" data-off-text="NO" data-on-text="YES">
</td>
#endforeach
Everything submits to the database successfully.
Since all are array fields, how do I add old() helper to each of the input fields (number, dropdown and checkbox)?
Thank you
I am trying to insert the checkbox data in database in which data are coming dynamically from database,
Here is the form :
<form action="{{ $action }}" method="post" enctype="multipart/form-data">
#csrf()
#if(!empty($method)) #method($method) #endif
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
#foreach($users as $k=>$u)
<tr>
<td><input type="checkbox" name="user_id[]" value="{{ $u->id }}"></td>
<td>{{ $u->name }}</td>
<td>{{ trim($evtitle) }}</td>
<td>{{ trim($evteacher) }}</td>
</tr>
#endforeach
</table>
<input type="text" name="question_id[]" value="{{ trim($evid) }}">
<input type="submit" class="btn btn-lg btn-success" value="Share" name="btn_btn_add_product">
</form>
Here is the controller :
$rs = ShareQuestion::create([
'user_id' => $request->input('user_id'),
'question_id' => $request->input('question_id')
]);
if($rs)
{
$message = array('flag'=>'alert-success', 'message'=>'Successfully');
return redirect()->route('auth.question.index')->with(['message'=>$message]);
}
$message = array('flag'=>'alert-danger', 'message'=>'Please try again');
return redirect()->route('auth.question.share')->with(['message'=>$message]);
Please help me out,
I think here is the issue as your checkbox is not checked so data is not passed to the controller function.
so change from(if you want to send all user_id)
<td><input type="checkbox" name="user_id[]" value="{{ $u->id }}"></td>
to
<td><input type="checkbox" name="user_id[]" checked value="{{ $u->id }}"></td>
or you can pass the condition for a checked attribute by
{{ Form::checkbox('user_id[]',$u->id,true, array('class'=>'some_class')) }}
$request->user_id and $request->question_id, array of values you have to use json_encode() method
$rs = ShareQuestion::create([
'user_id' => json_encode($request->user_id),
'question_id' => json_encode($request->question_id)
]);
I have a page for purchase order in that page i show data from table obat and i want to get selected checkbox using that data into table purchase order
This is my table obat
<table class="table table-bordered" style="margin-top:20px">
<tbody>
<tr class="text-center">
<th>No</th>
<td>Kode Obat</td>
<td>Nama Obat</td>
<td>Harga</td>
</tr>
#foreach ($obat as $key =>$o)
<tr>
<th class="text-center">
#foreach ($po as $po)
<input type="checkbox" name="select" id="select">
#endforeach
</th>
<td>
<input type="text" class="form-control" value="{{ $o->kode_obat }}">
</td>
<td>
<input type="text" class="form-control" value="{{ $o->nama_obat }}">
</td>
<td>
<input type="text" class="form-control" value="{{ $o->harga_obat }}">
</td>
</tr>
#endforeach
</tbody>
</table>
The checkbox(select) is from table purchase order but it can't show. if i did't use foreach its show
you can pass array also to check if it is in the array or not then you can this like below.
in your controller
public function edit($id)
{
$odat=Odat::all();
foreach($odat as $od)
{
$listod[]=$odat->select;
}
return view('yourbladefile',compact('roles','listod'));
}
then in your view
<input type="checkbox" name="select" #if (in_array(yourpurchaseordernamefromdb, $listod))
{{'checked'}} #endif>Selected</td>
personally i managed liked this. hope it helps