Tag Archives: laravel

When one logout redirect isn’t enough.

Our application has several different “sites,” which need various degrees of customization, although they all use mostly the same flow for our application. One area of customization is the login page. If they’re associated with our generic site, they would go to ourapp.com/generic and see the login. If they just go to ourapp.com, they get a different view. The challenge is that, on logging out, Laravel by default just redirects everybody to the same page.

I found this SO post that was close, but (perhaps because a 5.2 change?) it wasn’t quite working. Instead, this code works for me:

public function logout() {
    $redirect = (Auth::user()->site->loginPage()) ? Auth::user()->site->loginPage() : '/';
    return redirect($redirect);

I actually had to spend some time building a settings model for site settings, since we hadn’t had to use those before. I’d just built them manually, so today I’m going to work on building out some views so that they can be user-editable.

Always sending a variable to the view

As we’re building our app, we’re trying to make it international. We are storing the user’s language preference in the Session, but we need to have it as a variable in the views. I stumbled upon this SO post with an easy solution.

class BaseController extends Controller
  public function __construct()
    $lang = Session::get('lang');
    View::share('lang', $lang);

This makes the variable $lang available in our views. Nice and easy. Just remember to add “use View;” and, in our case, “use Session;” to the controller as well.

Password Reset Token in Laravel

My current task is to implement an email to send to users when their account is created manually. We’re using the default Laravel auth set-up, but this is outside of that scope. I mostly wanted to copy the password reset email. I could figure out everything but how to get the password reset token. I both needed to create it and needed to send it to the email that we’re sending to the created users. This link got me close, but they appear to be using an older version of Laravel. (We’re on 5.2.) I did a bit of digging through the folder and found that a couple of things had been renamed. The code to use:

$token = app(‘auth.password.broker’)->createToken($user);

Simple, but it definitely took some digging.

Laravel left outer join – sort of

Earlier this week, I added some functionality to the online game I’m building to have administrators be able to add a toy to a user’s collection. I was feeling proud of myself, until I realized that the script didn’t weed out toys the user already had, which would end up with duplications. So I had to figure out how to do that. Not being exactly sure how to describe what I wanted, I nevertheless entered a rather vague description into Google and hoped for the best.

Fortunately, other people have been trying to do what I was, and I was able to find some answers pretty easily. I was looking for a left outer join, basically, but with a condition. I wrote up some SQL and was able to get it to run in the terminal. It looked like this:

SELECT patterns.id, patterns.name
FROM patterns
ON patterns.id = toys.pattern_id AND toys.user_id = 1
WHERE toys.pattern_id IS NULL

Contrary to my expectations, this was actually the easy part. The next challenge was converting it into Laravel. I followed a couple of examples from StackOverflow, but I kept getting an empty result, and I’m still new enough to Laravel that I am not sure how to dig into the query it’s actually generating. I was eventually able to make it work. The code in the pattern model:

public function scopeUnknownPatterns($query, $userid){
    return $query
        ->leftJoin('toys',function($join) use ($userid){

Then, from the toy controller, I just use:

$patterns = Pattern::unknownPatterns($user->id)->get();

I was tripping up on the ->get() part, as the other scopes I’ve put into the model are limited and … for some reason I don’t really get yet don’t need it. One of the challenges of teaching yourself things is when you can’t actually answer your own questions!