Calid

Experience

11,210

0 Best Reply Awards

  • Member Since 1 Year Ago
  • 108 Lessons Completed
  • 2 Favorites

15th September, 2018

Calid left a reply on Custom Redirect After Registration Validation Failure - Call To Undefined Method Illuminate\Http\RedirectResponse::validate() • 4 days ago

Thanks you @Snapey , added this to RegisterController.php

    /**
     *
     * Override Trait RegistersUsers : vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php
     *
    */
    public function register(Request $request)
    {
        $validator = $this->validator($request->all());

        if ($validator->fails()) {
            return redirect('/register')
            ->withErrors($validator)
            ->withInput();
        }

        event(new Registered($user = $this->create($request->all())));
        $this->guard()->login($user);
        return redirect($this->redirectPath());
    }

14th September, 2018

Calid started a new conversation Custom Redirect After Registration Validation Failure - Call To Undefined Method Illuminate\Http\RedirectResponse::validate() • 5 days ago

I am using modal for registration and login, I want to redirect user to /registration page if validation has an error. I am getting an error

Call to undefined method Illuminate\Http\RedirectResponse::validate()

Here is my RegisterController.php

<?php
namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Foundation\Auth\RegistersUsers;

use Illuminate\Http\Request;

use Validator;
use Auth;

class RegisterController extends Controller
{

    use RegistersUsers;
    
    protected $redirectTo = '/home';

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

    
    protected function validator(array $data)
    {
        $validator = Validator::make($data, [
            'username' => 'required|string|max:255',
            'email' => 'required|email|unique:users,email,'.Auth::id(),
            'password' => 'required|string|min:6',
            'country_id' => 'required|integer|exists:countries,id',
            'mobile' => 'required|numeric|min:6|max:15',
        ]);
        
        
        //return $validator;
        
        if ($validator->fails()) {
            return redirect('/register')
            ->withErrors($validator)
            ->withInput();
        }

    }

    
    
    protected function create(array $data)
    {
            $user = new User;
            $user->username = $data['username'];
            $user->email = $data['email'];
            $user->password = Hash::make($data['password']);
            $user->mobile = $data['email'];
            $user->country_id = $data['country_id'];
            $user->ip = request()->ip();

            if($user->save()) {
            return $user;
            }
    }
}

Thanks

29th August, 2018

Calid started a new conversation What Is The Most Efficient Way To Return Count Of Relation Records Without Loading Relation • 3 weeks ago

I have subcategory and ads with relations like this:

subcat.php

public function ads() {
        return $this->hasMany(Ad::class, 'subcat_id');
    }

ad.php

public function subcat() {
        return $this->belongsTo(Subcat::class, 'subcat_id');
    }

I am trying to get the count of ads for each subcategory without loading the relation items as they are in hundreds of thousands ...

13th July, 2018

Calid left a reply on Laravel 5.5 Middleware Question • 2 months ago

this is how to redirect in registration controller

class RegisterController extends Controller
{

    use RegistersUsers;

    protected $redirectTo = '/u';
    
    /**
     * conditional redirect user on login.
     */
    protected function redirectTo()
    {
        if (\Session::has('regcode')) {
            return route('RegistrationCode');
        }
        
        return $this->redirectTo;
    }


    public function __construct()
    {
        $this->middleware('guest');
    }
    
    /* rest of the code
    @
    @
    @
    @
    */
    
}

Calid left a reply on Laravel 5.5 Middleware Question • 2 months ago

this is how I done it

class LoginController extends Controller
{

    use AuthenticatesUsers;

    protected $redirectTo = '/u';
    
    /**
     * conditional redirect user on login.
     */
     
    protected function redirectTo()
    {
        if (\Session::has('regcode')) {
            return route('RegistrationCode');
        }
        
        if(Auth::user()->hasAnyRole(['admin', 'superadmin', 'sale'])) {
            return route('HomeAdmin');
        }
        
        if(Auth::user()->hasAnyRole(['student', 'free', 'webuser'])) {
            return route('HomeUser');
        }

    }
    
    /* rest of the code 
    @
    @
    @
    @
    */
    
}

Calid left a reply on Updating Database After Sending Email • 2 months ago

Thanks @Cronix , I thought of that but wasn't sure if this is the best way to do it and in case mail sent failed it will still update the database , I will give it a try.

@snapey yes I have the user ID in mailable, but wouldn't that require to fire the event every time I call Welcome Mailable class

class welcomeNewUser extends Mailable
{
    use Queueable, SerializesModels;

    public $user;
    public $data;
    

    public function __construct($user, $data)
    {
         $this->user = $user;
         $this->data = $data;
    }


    public function build()
    {
        $subject = 'Welcome To Our Website';
        $fromEmail = '[email protected]';
        $fromName = "Website Name";
        
        return $this->view('emails.welcomeUser')
                ->from($fromEmail, $site)
                ->replyTo($this->user->email, $this->user->firstname)
                ->subject($subject)
                ->with(['user', $this->user], ['data', $this->data]);
    }
}

Calid started a new conversation Updating Database After Sending Email • 2 months ago

I am trying to figure out how to update database after sending an email. In my application I need to update column on users table "email_sent" after sending welcome email.

Welcome email is not attached to any event and can be fired from different methods/controllers. what would be the best way to achieve this without adding extra code each time I am calling Welcome Mailable class ... Thank You

5th November, 2017

Calid left a reply on Is It Possible To Tell Laravel To Lowercase Or Trim All Inputs By Default? • 10 months ago

After reading through this post, this is how I done it, appreciate any feedback!

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\TransformsRequest as TransformsRequest;
use Closure;

class SanitizationData extends TransformsRequest
{
    
    protected $except = [
        'password',
        'password_confirmation',
    ];
    
    protected function transform($key, $value)
    {
        
        switch ($key) {
            case "name":
            case "title":
            case "meta_desc":
            case "meta_title":
            case "meta_keywords":
                return filter_var($value, FILTER_SANITIZE_STRING);
        break;
            case "body":
                return htmlentities($value, ENT_COMPAT, "UTF-8", FALSE);
        break;
            case "email":
                return filter_var($value, FILTER_SANITIZE_EMAIL);   
        break;
            case "url":
                return filter_var($value, FILTER_SANITIZE_URL);
        break;
            default:
                return $value;
        }
    }
}

24th August, 2017

Calid left a reply on Route Model Binding: Check If A Route Parameter Exists In Model • 1 year ago

@sugo , any luck figuring a clean way to do this?

28th May, 2017

Calid left a reply on Changing The Default Location Of Storage Symlink - Laravel 5.4 • 1 year ago

Thanks @jijoel , Yes on production site I have the app outside the domain public folder ...

Calid left a reply on Changing The Default Location Of Storage Symlink - Laravel 5.4 • 1 year ago

Thanks @bashy , it's my first Laravel project so still figuring things out, you pointed my attention to fix the problem. The issue was in filesystems.php has nothing with symlink.

@ehab.aboshehab , updating "disk" root and URL fixed the issue in config/filesystems.php

'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path().'/app/public',
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],

    ],

Make sure you have symlink in your public directory to do so just run.

php artisan storage:link

27th May, 2017

Calid left a reply on Changing The Default Location Of Storage Symlink - Laravel 5.4 • 1 year ago

I am on Linux/ Red Hat / Apache

Yes I had the app installed under sub folder domain.com/laravel, moved the index.php to domain home directory /index.php . Everything working just fine. I am trying to remove {/laravel/} from symlink so a link for avatar for example look like this :

https://domain.com/storage/users/avatar.png

pointing to

https://domain.com/laravel/storage/app/public/users/avatar.png

Calid left a reply on Changing The Default Location Of Storage Symlink - Laravel 5.4 • 1 year ago

Thanks @bashy but I am not sure I get you right, my current structure look like this

example.com example.com/laravel/ example.com/laravel/app example.com/laravel/bootstrap example.com/laravel/config . . .

I moved my index.php from ** example.com/laravel/public/ ** to ** example.com ** ...

Calid started a new conversation Changing The Default Location Of Storage Symlink - Laravel 5.4 • 1 year ago

Hello,

is there a clean way to change the default storage symlink location inside public directory. right now it is located at

http://example.com/**myfolder**/public/

so links to files look something like this:

http://example.com/myfolder/public/storage/file.ext

I placed public documents in main domain directory but can't move the public/storage my goal is to remove myfolder from the link to become

http://example.com/public/storage/file.ext

Thanks :)

26th May, 2017

Calid left a reply on New User Registration IP Address Laravel 5.4 • 1 year ago

Thank you guys :)

@cmgmyr spot on ... yes I forgot to set the ip column to fillable in the User model ...

Calid started a new conversation New User Registration IP Address • 1 year ago

Hello All, My first question on Laracast :)

How can I get the user IP address and post it to users table on registration, I tried:

Use Request;

class RegisterController extends Controller { . . . . .

protected function create(array $data)
{   
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    'ip' =>  request()->ip(),                       /* Get User IP Address */
    ]);
}

}

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.