Home Backend Development PHP Tutorial How to implement employee attendance leave function through PHP and Vue

How to implement employee attendance leave function through PHP and Vue

Sep 25, 2023 pm 01:39 PM
php vue staff attendance Leave function

How to implement employee attendance leave function through PHP and Vue

How to implement the leave function of employee attendance through PHP and Vue requires specific code examples

With the rapid development of enterprise informatization, employee attendance management has gradually become a An important part of business management. Among them, the leave function is one of the common needs in daily management of employees. This article will introduce how to implement the leave function of employee attendance through PHP and Vue, and provide some specific code examples.

First, we need to create a database table to store employees' attendance data and leave records. The following is a simplified structure of the employee table and leave table:

Employee table (employees):

  • id: employee ID
  • name: employee name
  • department: Department

Leave form (leaves):

  • id: Leave record ID
  • employee_id: Employee ID
  • leave_type: Leave type (for example: personal leave, sick leave, annual leave)
  • start_date: Leave start date
  • end_date: Leave end date
  • status: Leave status (for example : Pending approval, approved, rejected)

Next, we need to create a back-end PHP script to handle the business logic related to leave. The following is a sample code using PHP:

// 员工请假列表接口
$app->get('/leaves', function($request, $response, $args) {
    $db = $this->get('db');

    $leaves = $db->table('leaves')
        ->join('employees', 'leaves.employee_id', '=', 'employees.id')
        ->select('leaves.*', 'employees.name')
        ->get();

    return $response->withJson($leaves);
});

// 创建员工请假记录接口
$app->post('/leaves', function($request, $response, $args) {
    $db = $this->get('db');
    $data = $request->getParsedBody();

    $leave = $db->table('leaves')->insert([
        'employee_id' => $data['employee_id'],
        'leave_type' => $data['leave_type'],
        'start_date' => $data['start_date'],
        'end_date' => $data['end_date'],
        'status' => '待审批'
    ]);

    return $response->withJson(['success' => true]);
});

// 更新员工请假记录接口
$app->put('/leaves/{id}', function($request, $response, $args) {
    $db = $this->get('db');
    $data = $request->getParsedBody();

    $leave = $db->table('leaves')->where('id', $args['id'])->update([
        'status' => $data['status']
    ]);

    return $response->withJson(['success' => true]);
});
Copy after login

Here, we use the Slim framework to create a simple API interface and use Eloquent as the ORM to operate the database. You can choose the appropriate tool based on the actual development environment and framework.

Next, we need to create a front-end Vue component to display employees' leave information and provide the function of creating and updating leave records. The following is sample code for a simplified Vue component:

<template>
    <div>
        <table>
            <tr>
                <th>员工ID</th>
                <th>员工姓名</th>
                <th>请假类型</th>
                <th>开始日期</th>
                <th>结束日期</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            <tr v-for="leave in leaves">
                <td>{{ leave.employee_id }}</td>
                <td>{{ leave.name }}</td>
                <td>{{ leave.leave_type }}</td>
                <td>{{ leave.start_date }}</td>
                <td>{{ leave.end_date }}</td>
                <td>{{ leave.status }}</td>
                <td>
                    <button v-if="leave.status === '待审批'" @click="approveLeave(leave.id)">
                        批准
                    </button>
                    <button v-else-if="leave.status === '已批准'" @click="rejectLeave(leave.id)">
                        拒绝
                    </button>
                </td>
            </tr>
        </table>
        <form @submit.prevent="createLeave">
            <input type="text" v-model="newLeave.employee_id" placeholder="员工ID" required>
            <input type="text" v-model="newLeave.leave_type" placeholder="请假类型" required>
            <input type="date" v-model="newLeave.start_date" placeholder="开始日期" required>
            <input type="date" v-model="newLeave.end_date" placeholder="结束日期" required>
            <button type="submit">提交请假申请</button>
        </form>
    </div>
</template>

<script>
export default {
    data() {
        return {
            leaves: [],
            newLeave: {
                employee_id: '',
                leave_type: '',
                start_date: '',
                end_date: ''
            }
        };
    },
    mounted() {
        this.getLeaves();
    },
    methods: {
        getLeaves() {
            // 使用Vue Resource或axios等工具发送GET请求获取请假数据
        },
        createLeave() {
            // 使用Vue Resource或axios等工具发送POST请求创建请假记录
        },
        approveLeave(id) {
            // 使用Vue Resource或axios等工具发送PUT请求更新请假记录的状态为已批准
        },
        rejectLeave(id) {
            // 使用Vue Resource或axios等工具发送PUT请求更新请假记录的状态为已拒绝
        }
    }
};
</script>
Copy after login

In this example, we use axios as the tool to send HTTP requests. You can also use other similar tools such as Vue Resource or fetch API.

Finally, add this Vue component to your application and ensure that the routing of the backend PHP script is configured correctly.

Through the above implementation, we can display the employee's leave request list on the front-end page, and provide the function of creating and updating leave request records. The backend PHP script is responsible for handling requests and database operations.

Summary: Implementing the leave function of employee attendance through PHP and Vue can effectively improve the efficiency and accuracy of attendance management. This example provides a simple implementation and provides some concrete code examples. You can make corresponding adjustments and expansions according to your own needs and actual development environment. Hope this article helps you!

The above is the detailed content of How to implement employee attendance leave function through PHP and Vue. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

How to jump to the div of vue How to jump to the div of vue Apr 08, 2025 am 09:18 AM

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.

How to jump a tag to vue How to jump a tag to vue Apr 08, 2025 am 09:24 AM

The methods to implement the jump of a tag in Vue include: using the a tag in the HTML template to specify the href attribute. Use the router-link component of Vue routing. Use this.$router.push() method in JavaScript. Parameters can be passed through the query parameter and routes are configured in the router options for dynamic jumps.

See all articles