mercredi 6 juin 2018

Fetch data from the database depending upon batch start and end dates and on time laravel

I am new to development and I am working on a school ERP on laravel. i have a table facultybatches which has the following columns:

id ,subject ,facultyId , startAt ,endAt ,batchStatus , remark , batchTime

this table consists of data about the faculties and the batches they will be taking , now i need to make sure the same teacher does not takes a different batch at same time , of if the time is same it should have different start and end date.

What is the most appropriate way of doing this.

My Controller;

 <?php

namespace App\Http\Controllers;
use Illuminate\Validation\Validator;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use Session;

class FacultyBatchController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // $faculties = DB::table('facultybatches')->get();
        $faculties = DB::table('facultybatches')
        ->join('faculty','facultybatches.facultyId' , '=' ,'faculty.facultyId')
        ->join('course','facultybatches.subject' , '=' ,'course.id')
        ->select('faculty.name','facultybatches.*','course.name AS course_name')->get();    
        // return $some;
        return view('services.viewfacultybatch',compact('faculties'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $courses = DB::select('select id,name from course');
        $faculties = DB::select('select facultyId,name from faculty');
        return view('services.addfacultybatch',compact('faculties','courses'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if($request->isMethod('POST')){
            $this->validate($request,[
                'facultyId' => 'required',
                'subjectname' => 'required',
                'startdate' => 'required',
                'enddate' => 'required',
                'batchStatus' => 'required',
                'remark' => 'required',
                'batchTime' => 'required'
                ]);


            // WHAT SHOULD I WRITE HERE

            return redirect()->route('facultybatch.index');

        }
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $batch = DB::table('facultybatches')->where('id', $id)->first();

        $courses = DB::select('select id,name from course');
        $faculties = DB::select('select facultyId,name from faculty');

        return view('services.editfacultybatch',compact('faculties','courses','batch'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if($request->isMethod('PUT')){
            $this->validate($request,[
                'facultyId' => 'required',
                'subjectname' => 'required',
                'startdate' => 'required',
                'enddate' => 'required',
                'batchStatus' => 'required',
                'remark' => 'required',
                'batchTime' => 'required'
                ]);

                DB::table('facultybatches')->where('id',$id)->update(    
                ['facultyId' => $request->facultyId,'subject' => $request->subjectname,'starAt' => $request->startdate,'endAt' => $request->enddate,'batchStatus' => $request->batchStatus,'remark' => $request->remark,'batchTime' => $request->batchTime]  );

            return redirect()->route('facultybatch.index');

        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        DB::delete('DELETE FROM facultybatches
        WHERE id = ?',[$id]);
    return redirect()->route('facultybatch.index');
    }

    public function facultybatchss(Request $request){

        $ids = $request->fb;
        DB::table("facultybatches")->whereIn('id',explode(",",$ids))->delete();

    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire