Thursday, 14 June 2018

Check if empty in collection


When using ->get() you cannot simply use any of the below:
if (empty($result)) { }
if (!$result) { }
if ($result) { }
Because if you dd($result); you'll notice an instance of Illuminate\Support\Collection is always returned, even when there are no results. Essentially what you're checking is $a = new stdClass; if ($a) { ... } which will always return true.
To determine if there are any results you can do any of the following:
if ($result->first()) { } 
if (!$result->isEmpty()) { }
if ($result->count()) { }
if (count($result)) { }

Monday, 7 May 2018

Date Format to Template and Validation

At Validation

  'dob' => 'required|date_format:"d/m/Y"|before:"2006-01-01"',


At View Template

  {{$submissions->created_at->format("F d, Y")}}


Reference

1. https://stackoverflow.com/questions/32080734/laravel-validation-date-does-not-match-the-format?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

2. http://php.net/manual/en/function.date.php

3. https://stackoverflow.com/questions/24441395/how-to-change-default-format-at-created-at-and-updated-at-value-laravel?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

Saturday, 5 May 2018

Return Old Input After Validation

From Controller (UserController)

  $validator = Validator::make($request->all(), [
          'first_name' => 'required|max:100',
          'last_name' => 'required|max:100',
          'gender' => 'required|numeric',
          'dob' => 'required|date|before:"2006-01-01"',
          'email' => 'required|email',
          'mobile' => 'required|numeric',
          'street' => 'required|max:255',
          'city' => 'required|alpha|max:20',
          'state' => 'required|numeric',
          'post_code' => 'required|numeric',
      ], $messages);


      if ($validator->fails()) {
          return redirect()->back()
                      ->withErrors($validator)
                      ->withInput($request->input());
      }



Thursday, 3 May 2018

Laravel Custom Login Validation

   public function __construct()
   {
      $this->middleware('guest')->except('logout');
   }

  /**
  * Get the failed login response instance.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\RedirectResponse
  */
  protected function sendFailedLoginResponse(Request $request)
  {
      $errors = [$this->username() => trans('auth.failed')];
      // Load user from database
      $user = \App\User::where($this->username(), $request->{$this->username()})->first();
      if($user==null){
        $errors = ['email' => 'Invalid Email. Please try again'];
      }else{
        // Check if user was successfully loaded, that the password matches
        // and active is not 1. If so, override the default error message.
        if (!Hash::check($request->password, $user->password)) {
            $errors = ['password' => 'Invalid Password. Please Try again'];
        }

      }

      if ($request->expectsJson()) {
          return response()->json($errors, 422);
      }
      return redirect()->back()
          ->withInput($request->only($this->username(), 'remember'))
          ->withErrors($errors);
  }

Reference :

https://gist.github.com/SaeedPrez/b3d35af0bb039e8b8d5caf85ec3b2476
https://laravel-news.com/login-validation

Monday, 30 April 2018

Redirected after Login or Register

in Login Controller, it was located under Illuminate\Foundation\Auth\AuthenticatesUsers;

then change this function to :

  /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function authenticated(Request $request, $user)
    {
      return redirect('/admin');
    }


while for Register Controller, it was located under Illuminate\Foundation\Auth\RegistersUsers;

then change this function to : 

    /**
     * The user has been registered.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function registered(Request $request, $user)
    {
        return redirect('/admin');
    }

however RegisterController has a constant variable   protected $redirectTo = '/home'; which you can just change their value to your target url.

reference 

https://stackoverflow.com/questions/44173616/laravel-5-4-how-do-i-find-logincontrollershowloginform

https://stackoverflow.com/questions/42177044/laravel-5-4-redirection-to-custom-url-after-login

Saturday, 28 April 2018

Friday, 27 April 2018

Merging Input Request

Often you want to pass value for input type hidden, this can be submitted by merging with another form request to the controller





Reference

https://stackoverflow.com/questions/23073633/laravel-change-input-value/36549464#36549464