(Laravel and Vue ) two tables connect so that it can preform the data interger subtraction. It has conditions in dates

by Peejong   Last Updated November 26, 2018 10:04 AM - source

I’m developing a chicken poultry website using Laravel ,vue, and momentjs. I have my plan in my mind but I don’t know how to apply to my website.

This is the picture of Cycle History Cycle History and Mortality modal
Mortality modal

This is my plan.

If the user wants to fill the Mortality, he/she go to the mortality tab and open the mortality modal.

There are 1 date-picker and 3 textfields available in mortality modal.

Date-picker

  1. date_input - The user input the date of chicken die

Text Field

  1. number_of_mortality - The user input the number of died chicken. This data will deduct the population<-(this data is from the cycles table)
  2. chicken_age - It automatic display the age of the chicken. The equation for this is the count day/s from date_start_raise <-(this data is from the cycles table) to date_input <-(this data is from the moralities table).
  3. cause_of_death - optional.

If the date_input includes from the date_start_raise to date_end_raise <-(this data is from the cycles table), the number_of_mortality will deduct the population <-(this data is from the cycles table).


For example
There are 2 data in Cycle history

Data 1
date_start_raise: 2018-10-1
date_end_raise: 2018-10-26
Population: 9000

Data 2
date_start_raise: 2018-11-1
date_end_raise: 2018-11-26
Population: 9500

the user input the data to the mortality modal.

input_date : 2018-10-20

This data will connect to the Data 1 from the cycle because the value of date_input includes the raising date range of Data 1 (from date_start_raise to date_end_raise)

number_of_mortality: 18

age_of_chicken: 19

This data will automatic appear. ( The equation for this is the count day/s from date_start_raise = 2018-10-1 to date_input=2018-10-20.

after of filling data, the user click the add button so that the data will send to database.

The number_of_mortality deduct the population of Data 1. The value of population from Data 1 will be 8982 (9000 - 18 = 8982)

How can I achieve of my plan for my website especially:

  1. the condition of input_date that includes to the raising date range(from date_start_raise to date_end_raise),
  2. the subtraction of number_of_mortality to population(population - number_of_mortality) and
  3. auto appearing chicken_age(count days from date_start_raise to input_date)?

PS. there are 2 tables named cycles and moralities

cycles data

  1. date_start_raise
  2. date_end_raise
  3. population

moralities data

  1. date_input
  2. number_of_mortality
  3. chicken_age

Codes

CycleController.php

<?php

namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Cycle;
use Illuminate\Support\Facades\Auth;

class CycleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function __construct()
    {
        $this->middleware('auth:api');
    }

    public function index()
    {
        $cycles = Cycle::latest()->where(['user_id' => Auth::id()])->paginate(20);
       return $cycles; 
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, array(
            'date_start_raise' => 'required|date',
            'population' => 'required|numeric',
            'raising_days' => 'required|numeric',

         ) );

         return Cycle::create([
            'date_start_raise' => request('date_start_raise'),
            'population' => request('population'),
            'raising_days' => request('raising_days'),
            'date_end_raise' => request('date_end_raise'),
            'date_manure_collection' => request('date_manure_collection'),
            'date_cleaning' => request('date_cleaning'),
            'date_disinfection' => request('date_disinfection'),
            'date_rest_day' => request('date_rest_day'),
            'date_preparation' => request('date_preparation'),
            'user_id'     => Auth::id()

        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $cycle = Cycle::findOrFail($id);
        $this->validate($request, array(
            'date_start_raise' => 'required|date',
            'population' => 'required|numeric',
            'raising_days' => 'required|numeric',
            'date_end_raise' => 'required|date',
            'date_manure_collection' => 'required|date',
            'date_cleaning' => 'required|date',
            'date_disinfection' => 'required|date',
            'date_rest_day' => 'required|date',
            'date_preparation' => 'required|date',
         ) );
         $cycle->update($request->all());
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $cycle = Cycle::findOrFail($id);
        $cycle->delete();
    }
}

MortalityController.php

<?php

namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Cycle;
use Illuminate\Support\Facades\Auth;

class CycleController extends Controller

    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */

        public function __construct()
        {
            $this->middleware('auth:api');
        }

        public function index()
        {
            $cycles = Cycle::latest()->where(['user_id' => Auth::id()])->paginate(20);
           return $cycles; 
        }

        /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function store(Request $request)
        {
            $this->validate($request, array(
                'date_start_raise' => 'required|date',
                'population' => 'required|numeric',
                'raising_days' => 'required|numeric',

             ) );

             return Cycle::create([
                'date_start_raise' => request('date_start_raise'),
                'population' => request('population'),
                'raising_days' => request('raising_days'),
                'date_end_raise' => request('date_end_raise'),
                'date_manure_collection' => request('date_manure_collection'),
                'date_cleaning' => request('date_cleaning'),
                'date_disinfection' => request('date_disinfection'),
                'date_rest_day' => request('date_rest_day'),
                'date_preparation' => request('date_preparation'),
                'user_id'     => Auth::id()

            ]);
        }

        /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function show($id)
        {
            //
        }

        /**
         * Update the specified resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function update(Request $request, $id)
        {
            $cycle = Cycle::findOrFail($id);
            $this->validate($request, array(
                'date_start_raise' => 'required|date',
                'population' => 'required|numeric',
                'raising_days' => 'required|numeric',
                'date_end_raise' => 'required|date',
                'date_manure_collection' => 'required|date',
                'date_cleaning' => 'required|date',
                'date_disinfection' => 'required|date',
                'date_rest_day' => 'required|date',
                'date_preparation' => 'required|date',
             ) );
             $cycle->update($request->all());
        }

        /**
         * Remove the specified resource from storage.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function destroy($id)
        {
            $cycle = Cycle::findOrFail($id);
            $cycle->delete();
        }
    }

Vue (script Codes)

Cycles.vue

<script>
        export default {
                    data(){
                return{
                    editmode: false,
                    cycles : {},
                    form: new Form({
                        id: "",
                        date_start_raise: '',
                        population: '',
                        raising_days: '',
                        date_end_raise: '',
                        date_manure_collection:'',
                        date_cleaning: '',
                        date_disinfection: '',
                        date_rest_day: '',
                        date_preparation: '',
                        user_id:'',
                    })
                }
            },
             methods:{
                getResults(page = 1) {
                    axios.get('api/cycle?page=' + page)
                    .then(response => {
                    this.cycles = response.data;
                    });
                }, 
                isFutureDate(date_start_raise) {
                    const currentDate = new Date();
                    return date_start_raise > currentDate;
                },
                loadCycles(){
                    axios.get("api/cycle").then(({ data }) => (this.cycles = data ));
                },
                editModal(cycle){
                    this.editmode = true; 
                    this.form.reset();
                    $('#cycle_modal').modal('show');
                    this.form.fill(cycle);
                },
                openModal(){
                    this.editmode = false; 
                    this.form.reset();
                    $('#cycle_modal').modal('show');
                },
                updateCycle(){
                    this.$Progress.start();
                    this.form.put('api/cycle/'+this.form.id)
                    .then(() =>{
                        // success
                        $('#cycle_modal').modal('hide');
                         swal(
                            'Updated!',
                            'Information has been updated.',
                            'success'
                            )
                            this.$Progress.finish();
                             Fire.$emit('AfterCreate');
                    })
                    .catch(() =>{
                        this.$Progress.fail();
                        swal("Failed!", "There was something wrong.", "warning");
                    });
                },
                deleteCycle(id){
                    swal({
                        title: 'Are you sure?',
                        text: "You won't be able to revert this!",
                        type: 'warning',
                        showCancelButton: true,
                        confirmButtonColor: '#3085d6',
                        cancelButtonColor: '#d33',
                        confirmButtonText: 'Yes, delete it!'
                        }).then((result) => {

                            // Send request to the server
                             if (result.value) {
                                    this.form.delete('api/cycle/'+id).then(()=>{
                                            swal(
                                            'Deleted!',
                                            'Your data has been deleted.',
                                            'success'
                                            )
                                        Fire.$emit('AfterCreate');
                                    }).catch(()=> {

                                        swal("Failed!", "There was something wrong.", "warning");
                                    });
                             }
                        })
                },
                addCycle(){
                    this.$Progress.start();
                    this.form.post('api/cycle')
                    .then(()=>{
                    Fire.$emit('AfterCreate');  
                    $('#cycle_modal').modal('hide')

                        toast({
                            type: 'success',
                            title: 'New Cycle added in successfully'
                            })

                    this.$Progress.finish();
                    })
                    .catch(()=>{
                        this.$Progress.fail();
                        swal("Failed!", "There was something wrong.", "warning");
                    })
                }                       
             },
             computed:{
                 dateEndRaise: {get: function() {    
                this.form.date_end_raise = moment(this.form.date_start_raise).add(this.form.raising_days, 'days').format('YYYY-MM-DD');
                return this.form.date_end_raise;
                }},
                dateManureCollection: {get: function() {
                this.form.date_manure_collection = moment(this.form.date_end_raise).add(2, 'days').format('YYYY-MM-DD');
                return this.form.date_manure_collection ;
                }},
                dateCleaning: {get: function() {
                this.form.date_cleaning = moment(this.form.date_end_raise).add(9, 'days').format('YYYY-MM-DD');
                return this.form.date_cleaning ;
                }},
                dateDisinfection: {get: function() {
                this.form.date_disinfection = moment(this.form.date_end_raise).add(10, 'days').format('YYYY-MM-DD');
                return this.form.date_disinfection ;
                }},
                dateRest: {get: function() {
                this.form.date_rest_day = moment(this.form.date_end_raise).add(20, 'days').format('YYYY-MM-DD');
                return this.form.date_rest_day ;
                }},
                datePreparation: {get: function() {
                this.form.date_preparation= moment(this.form.date_end_raise).add(21, 'days').format('YYYY-MM-DD');
                return this.form.date_preparation ;
                }}            
             },     
             created() {
                this.loadCycles();
                Fire.$on('AfterCreate',() => {
                this.loadCycles();
            });
        }
    } 
    </script>

Mortalities.vue

<script>
    export default {
         data(){
            return{
                editmode: false,
                mortalities : {},
                form: new Form({
                    id: "",
                    date_input: '',
                    number_of_mortality: '',
                    chicken_age: '',
                    cause_of_death: '',
                    user_id:'',
                })
            }
        },
         methods:{
            getResults(page = 1) {
                axios.get('api/mortality?page=' + page)
                    .then(response => {
                    this.mortalities = response.data;
                });
            },
             isFutureDate(date_input) {
            const currentDate = new Date();
            return date_input > currentDate;
        },    
            loadMortalities(){
                axios.get("api/mortality").then(({ data }) => (this.mortalities= data));
            },
            editModal(mortality){
                this.editmode = true; 
                this.form.reset();
                $('#mortality_modal').modal('show');
                this.form.fill(mortality);
            },
             openModal(){
                this.editmode = false; 
                this.form.reset();
                $('#mortality_modal').modal('show');
            },
            updateMortality(){
                this.$Progress.start();
                this.form.put('api/mortality/'+this.form.id)
                .then(() =>{
                    // success
                    $('#mortality_modal').modal('hide');
                     swal(
                        'Updated!',
                        'Information has been updated.',
                        'success'
                        )
                        this.$Progress.finish();
                         Fire.$emit('AfterCreate');
                })
                .catch(() =>{
                    this.$Progress.fail();
                    swal("Failed!", "There was something wrong.", "warning");
                });
            },
             deleteMortality(id){
                swal({
                    title: 'Are you sure?',
                    text: "You won't be able to revert this!",
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, delete it!'
                    }).then((result) => {

                        // Send request to the server
                         if (result.value) {
                                this.form.delete('api/mortality/'+id).then(()=>{
                                        swal(
                                        'Deleted!',
                                        'Your data has been deleted.',
                                        'success'
                                        )
                                    Fire.$emit('AfterCreate');
                                }).catch(()=> {

                                    swal("Failed!", "There was something wrong.", "warning");
                                });
                         }
                    })
            },
            addMortality(){
                this.$Progress.start();
                this.form.post('api/mortality')
                .then(()=>{
                Fire.$emit('AfterCreate');  
                $('#mortality_modal').modal('hide')

                    toast({
                        type: 'success',
                        title: 'Mortality added in successfully'
                        })

                this.$Progress.finish();
                })
                .catch(()=>{
                    this.$Progress.fail();
                    swal("Failed!", "There was something wrong.", "warning");
                })
            }                        
         },     
        created() {
             Fire.$on('finding',() => {
                let query = this.$parent.search;
                axios.get('api/findMortality?q=' + query)
                .then((data) =>{
                    this.mortalities = data.data
                })
                .catch(() =>{

                })
            })
            this.loadMortalities();
            Fire.$on('AfterCreate',() => {
               this.loadMortalities();
           });
        }  
    }
</script>

Tags : php laravel


Related Questions


Appzcoder on Laravel

Updated September 14, 2016 09:01 AM

laravel or october cms

Updated July 11, 2016 08:01 AM

CMS with plugins/modules system

Updated February 20, 2019 11:04 AM