webkenny's avatar

IDE and Laravel w/ Helper

I'm using PHPStorm and have installed the nifty package barryvdh/laravel-ide-helper but I'm going bananas and I'm sure it's my OCD kicking in. I also have the Laravel IDE extension enabled for the project. I watched the screencast @JeffreyWay did on using PHPStorm and I can't get something to resolve.

Let's say I'm returning responses from my controllers, building an API. My IDE complains about the response method, except in a single case:

Gets error Method json() not found on class \Illuminate\Http\Response

  • Response::json()
  • response()->json()
  • $response = new Response(); $response->json()

Does not get the error (Note the preceding slash)

  • \Response::json()

My questions are two-fold:

  1. Can I fix this problem somehow because it's driving me crazy?
  2. If I can't fix it and I stick with \Response how can that be correct when I run the code? It looks like it's resolving from the _ide_helper.php file that ships with the ide-helper when you run the artisan generate command.

I am still relatively new to Laravel and trying to follow docs, examples, and screencasts here but it's really difficult when everything I try doesn't resolve.

Any help would be most appreciated. Thanks folks!

0 likes
4 replies
webkenny's avatar

I thought it might be helpful to show some code in context. Here's a simplified example of what I am doing:

    /**
     * On POST, create a new account.
     *
     * @param  Request  $request
     * @return Response
     * @see http://docs.com/account#post
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->input('data'), [
            'subdomain' => 'required|unique:accounts|max:50',
            'name' => 'required'
        ]);

        if($validator->fails()) {
            return \Response::json(['errors' => $validator->errors()])->setStatusCode(422);
        }

        $account = new Account();
        $account->name = $request->input('data')['name'];
        $account->subdomain = $request->input('data')['subdomain'];
        $account->save();
        
        return \Response::json(['data' => array('id' => $account->id)])->setStatusCode(201);
    }
webkenny's avatar

Ok, I figured this out. The IDE gets confused when you use Illuminate\Http\Response at the top of the file. Removing that line lets everything in the response() helper resolve.

Then to keep your PHP doc blocks sane, simply replace @return Response with the fully namespaced @return \Illuminate\Http\Response.

Hope that helps someone. :)

thomaskim's avatar
Level 41

@webkenny That sounds like it's acting properly to me. If you are going to use the Response facade, you should add use Response, not use Illuminate\Http\Response.

1 like
webkenny's avatar

@thomaskim Yep. You're absolutely right. I am new so getting confused between all of the same keywords for different things. I think my last comment sums it up. :)

Please or to participate in this conversation.