如何在laravel 4.2中自动生成特定类别任务的序列号


How to auto-generate sequence number for particular category task in laravel 4.2?

*对不起,我不知道怎么问这个问题。例子——

categoryname/categoryshortname-1

网站/web-1网站/web-2网站/web 3android/andr-1

1-如果存在包含字段categoryname,id,categorycode的类别表

2-设类别名称为Website,类别代码为WEB

3-假设任何用户将任务分配给任何其他成员,那么他将选择类别代码(WEB)并分配任务。

4-分配任务类别代码后会插入到任务表中。

5之后,如果用户将带有类别码(ANDR)的任务分配给同一用户。

6-Now分配给任务的用户应该看到类别代码为WEB-1,对于android任务应该看到类别代码为Andr2

$tasktime = new Tasktime();
$tasktime->TaskTitle = Input::get('tasktitle');
$tasktime->Description_Task = Input::get('taskdescribe');
$tasktime->Estimated_Time = $case;
$tasktime->Task_Status = Input::get('status');
$tasktime->Priority_Task = Input::get('priority');
$tasktime->Assignee_Id = Input::get('Assignee_Id');
$tasktime->categorycode = Input::get('cateorycode');
$tasktime->Task_DueDate = Input::get('duedate');
$tasktime->Task_created_by = Auth::user()->firstname;
$tasktime->Created_User_Id = Auth::user()->id;
$tasktime->tasktype = Input::get('tasktype');
$tasktime->save();
// send mail to assignee id
$assigneeUser = User::find(Input::get('Assignee_Id'));
Mail::send('emails.send', array('TaskTitle' => Input::get('tasktitle'), 'Priority_Task' => Input::get('priority')), function ($message) use ($assigneeUser) {
    $message->to($assigneeUser->email)->subject('verify');
});
return Redirect::to('index')->with('message', 'Email has been sent to assignee related to work');

此代码与插入到任务表中的类别代码有关。

如果用户根据分类代码分配任务,我想要分类代码按顺序排列,如Web1 Web2 Andr1 Web3。

请帮帮我,我已经尽力解释我的问题了

如果我理解对了,您想要自动生成像website/web-{n}这样的类别,每个子类别总是从1开始?然后:

$table = $tasktime->getTable(); // table with tasks
$category = Input::get('cateorycode');
$max = DB::table($table)
           ->where('Assignee_Id', Input::get('Assignee_Id'))
           ->where('categorycode', $category)
           ->select('count(id) as sub_count')->first();
$max = ($max != null) ? intval($max['sub_count']) : 0;
$max++;
$generatedSlug = "$category-{$max}";
// web-1, web-2, andr-1, web-3
// [] + web = web-1, web-2, andr-1, web-3, web-4
// [] + andr = web-1, web-2, andr-1, web-3, web-4, andr-2

代码未检查,可能需要稍微修改。

乌利希期刊指南。

也许,更可读的解决方案:

$assignee = Input::get('Assignee_Id');
$category = Input::get('cateorycode');
$max = Tasktime::where('Assignee_Id', $assignee)
               ->where('categorycode', $category)
               ->value('count(id)');
$next = (intval($max) ?: 0) + 1;
$generatedSlug = "$category-{$next}";