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

Posted

技术标签:

【中文标题】如何在 laravel 4.2 中为特定类别任务自动生成序列号?【英文标题】:How to auto-generate sequence number for particular category task in laravel 4.2? 【发布时间】:2015-11-06 05:17:16 【问题描述】:

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

categoryname/categoryshortname-1

网站/web-1 网站/web-2 网站/web-3 安卓/andr-1

1-如果类别表上有字段 categoryname,id,categorycode。

2-假设类别名称为网站,类别代码为 WEB。

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

4-分配任务类别代码后将插入任务表。

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

6-现在分配任务的用户应该看到类别代码为 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。

请帮帮我。我已尽力解释我的问题。

【问题讨论】:

【参考方案1】:

如果我猜对了,您想为website/web-n 等类别自动生成 slug,每个子类别总是从 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";

【讨论】:

@Himanshu,那么您应该对答案进行投票和/或将其标记为已接受。很高兴我帮助了你 =)

以上是关于如何在 laravel 4.2 中为特定类别任务自动生成序列号?的主要内容,如果未能解决你的问题,请参考以下文章

在 WooCommerce 中为特定产品类别自定义“添加到购物车”按钮

在Woocommerce中为特定产品类别使用自定义单一产品模板

在 Wordpress Datewise 中为特定类别创建存档

在 Laravel 4.2 中为每个公司创建目录(命名为 Today Date)并编写日志文件(命名为 CompanyName)

Laravel 4.2 Eloquent 获取特定用户和特定角色

如何在 Laravel 4.2 中创建自定义 DB Query Builder 方法