Home > database >  In Laravel controller file, I would like to change the date format of regDate from 'Y-m-d'
In Laravel controller file, I would like to change the date format of regDate from 'Y-m-d'

Time:09-28

I would like to format "regDate" to 'dd/mm/YYYY' from 'YYYY/mm/dd'. Appreciate any help. Thanks.

if ($request->keyword != "") {
  $students = Student::where
  ("name","LIKE","%" . $request->keyword . "%")
  ->orWhere("nric","LIKE","%" . $request->keyword . "%")
  ->orWhere("address","LIKE","%" . $request->keyword . "%")
  ->orWhere("telNo","LIKE","%" . $request->keyword . "%")
  ->orWhere("email","LIKE","%" . $request->keyword . "%")
  ->orWhere("regDate","LIKE","%" . $request->keyword . "%")
  ->get();
}

        

CodePudding user response:

Please set your date format like below and check again.

date('d/m/Y', strtotime($request->keyword));

if ($request->keyword != "") {
  $students = Student::where
  ("name","LIKE","%" . $request->keyword . "%")
  ->orWhere("nric","LIKE","%" . $request->keyword . "%")
  ->orWhere("address","LIKE","%" . $request->keyword . "%")
  ->orWhere("telNo","LIKE","%" . $request->keyword . "%")
  ->orWhere("email","LIKE","%" . $request->keyword . "%")
  ->orWhere("regDate","LIKE","%" . date('d/m/Y', strtotime($request->keyword)) . "%")
  ->get();
}

CodePudding user response:

if (DateTime::createFromFormat('d/m/Y', $request->keyword) === true) {
   $request->keyword = Carbon::createFromFormat('d/m/Y', $request->keyword)->format('Y-m-d')
}

if ($request->keyword != "") {
  $students = Student::where
  ("name","LIKE","%" . $request->keyword . "%")
  ->orWhere("nric","LIKE","%" . $request->keyword . "%")
  ->orWhere("address","LIKE","%" . $request->keyword . "%")
  ->orWhere("telNo","LIKE","%" . $request->keyword . "%")
  ->orWhere("email","LIKE","%" . $request->keyword . "%")
  ->orwhereDate("regDate","=","$request->keyword")
  ->get();
}

Check value if date you can convert to your format and then you can send query

  • Related