nahid / talk

Talk is a real-time users messaging and chatting system for Laravel.
MIT License
1.61k stars 327 forks source link

Cant setAuthUserId in Laravel 5.4 #61

Closed packytagliaferro closed 7 years ago

packytagliaferro commented 7 years ago

So I set up my MessageController.php like so:

<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Http\Request;
use Nahid\Talk\Facades\Talk;

class MessageController extends Controller
{

    public function __construct()
    {
        Talk::setAuthUserId( Auth::id() );
    }

    public function index()
    {
      $inboxes = Talk::getInbox();

      return $inboxes;
    }
}

If I try and dd( Talk::setAuthUserId( Auth::user() ) ) it returns false so the user is not being set. But if I hardcode an id in my construct like:

 public function __construct()
    {
        Talk::setAuthUserId( 2 );
    }

The user is set and I can get $inboxes = Talk::getInbox();

I know Auth::id() will return my user id of 2 so not sure what is going on

FooleanBool commented 7 years ago

Raised here also: https://github.com/nahid/talk/issues/54

   public function __construct()
    {        
        $this->middleware('auth');
        $this->middleware(function ($request, $next) {
            $id = Auth::user()->id;
            Talk::setAuthUserId($id);
            return $next($request);
        });

This seems to have worked for me (MessageController), found it on laracasts somewhere I think.

nahid commented 7 years ago

see the update documentation. You have to register Talk middleware and call it from controller constructor

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