S
S
Sergey Chazov2019-04-07 13:12:18
Laravel
Sergey Chazov, 2019-04-07 13:12:18

How to fix Call to a member function setCookie() on null error?

There is an intermediary that checks whether the user is an employee of the company and, if false, then throws him to the Idea page, and if true, then closes and transfers control to the controller.
But an error occurs: when the result of the check is false, then I see the error Call to a member function setCookie() on null.
How to deal with this error?
Here is the intermediary code:

use App\User;
class UserCompanies
{
     public function handle($request, Closure $next)
    {
      $userID  = Auth::user()->id;
      $userModel = User::find($userID);
      $id=$request->route('id');
      $postsHasMany = $userModel->companies();
      $companies = $postsHasMany->where('id','like',$id)->count();
  if ($companies==0) {
            return view('idea');
        }
  return $next($request);
    }
}

And this is the controller that calls this middleware
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Company;
use Auth;
use App\User;

class CompanyController extends Controller
{
public function __construct()
    {
        $this->middleware('company:id');
    }
    public function companyHome($id)
{
    $company=Company::find($id);
    return view('companyIndex', compact($company))->with([
        'company'=>$company
    ]);
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2019-04-07
@JhaoDa

Because the middleware cannot return a view, you need to wrap it in a response or do a redirect.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question