Laravel Gotcha: Unresolvable dependency

December 18, 2018

Image for Laravel Gotcha: Unresolvable dependency from unsplash.com

I got the following error when creating a new controller via Artisan:

Unresolvable dependency resolving [Parameter #0 [<required> $repository ]] in class App\Http\Controllers\Controller

Here's the command and the controller code:

php artisan make:controller TestEmailController</pre>
<?php

namespace App\Http\Controllers;

use App\Mail\TestEmail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class TestmailController extends Controller
{

    /**
    * Test an email
    *
    * @param Request $request
    * @return Response
    */
    public function send(Request $request)
    {

    // Send mail order...
    // Mail::to('[email protected]')-&gt;send(new TestEmail())
    die('Check Mailtrap');
    }
}

The Fix

This happened because I didn't have a constructor set up in my new controller class:

<?php

namespace App\Http\Controllers;

use App\Mail\TestEmail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class TestmailController extends Controller
{

    public function __construct() {}

    /**
    * Test an email
    *
    * @param Request $request
    * @return Response
    */
    public function send(Request $request)
    {
    ...
    }