How to assign task to multiple users at same time? - laravel

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

Related

Displaying Validation Error Messages Beside The Input Box in Laravel

<form action="/regvalid", method="POST">
{{ csrf_field()}}
<h2>Personal Information</h2>
<hr>
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="fname" value="{{ old('fname') }}"></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lname"></td>
</tr>
<tr>
<td>Gender:</td>
<td>
<div class="radio">
<label><input type="radio" value="male" name="gender">Male</label>
<label><input type="radio" value="female" name="gender">Female<label>
</div>
</td>
</tr>
<tr>
<td>Mobile Phone:</td>
<td><input type="text" name="mp"></td>
</tr>
<tr>
<td>Tel No.:</td>
<td><input type="text" name="telno"></td>
</tr>
<tr>
<td>Birth Date:</td>
<td><input type="date" name="bd" ></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="add"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="email" name="email"></td>
</tr>
<tr>
<td>Website:</td>
<td><input type="text" name="website"></td>
</tr>
I have already their error rules in the controller;
$rules = [
'fname' => 'required|max:50',
'lname' => 'required|max:50',
'gender' => 'required',
'mp' => 'required|regex:/^([0-9\s-+()]*)$/|min:11',
'telno' => 'required|min:11|numeric',
'bd' => 'required|date',
'add' => 'required|max:100',
'email' => 'required|email',
'website' => 'required|url',
'username' => 'required|min:6|max:20',
'pass' => 'required|min:6|max:12',
'repass' => 'required|min:6|max:12',
];
and also the code that will show all;
#if(count($errors)>0)
#foreach ($errors->all() as $errors)
{{ $errors}}
#endforeach
#endif
I want to display the error messages on each one of the input boxes besides them. I don't know how.
For Displaying error message
Method 1:
<input type="text" name="firstname">
#if($errors->has('firstname'))
<div class="error">{{ $errors->first('firstname') }}</div>
#endif
Method 2:
#error('firstname')
<div class="error">{{ $message }}</div>
#enderror
These two ways of doing it

How To submit form by one submit button in foreach loop in laravel?

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>

separate each value with comma from multiple select in laravel

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

insert data through checkbox in laravel

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)
]);

Save multiple Items many-to-many Laravel 6

I'm trying to save multiple Product into Purchase by using Vuejs for frontend and Laravel for Backend and that is a many-to-many relationship between them.
In My Vuejs
<tr class="tablePurchase--td" v-for="(item, index) in items">
<td>{{item.name}}</td>
<td>{{item.code}}</td>
<td>
<input type="number" class="table-quantity" v-model="items[index].quantity">
</td>
<td>
<input type="number" class="table-quantity" v-model="items[index].unit_price" placeholder="0.00">
</td>
<td>
<input type="number" class="table-quantity" v-model="items[index].discount" placeholder="0.00">
</td>
<td>
<v-btn small color="red" outlined #click="removeItem(index)">
<v-icon>mdi-delete</v-icon>
</v-btn>
</td>
</tr>
In my Controller
public funtion store(Request $request) {
....
foreach($purchase->products as $item) {
$purchase->products()->attach($product_id, [
'unit_price' => $item['unit_price'],
'quantity' => $item['quantity'],
'discount' => $item['discount'],
]);
}
}
But, its got empty array... i even tried dd($request->items) and its return null
I'll appreciate all ur Help? Thanks
You should name all of your inputs as items[]
//...
<input type="number" class="table-quantity" name="items[]" v-model="items[index].unit_price" placeholder="0.00">
//...

Resources