The Bug class doesn’t exist?

Asked

Viewed 268 times

0

I’m making the following mistake:

Reflectionexception in Container.php line 731: Class App Defeito does not exist

I’m using Laravel 5.4, I’ve checked apparently this all right.

Code: Defeito.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Defeito extends Model {

   protected $table = 'defeito';
   public $timestamps = false;

}

Code: Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use League\Flysystem\Exception;

class DefeitoController extends Controller 
{

    private $defeitos;

    public function __construct(Defei $defeitos) 
    {
        $this -> defeitos = $defeitos;
    }

    public function salvar(Request $request) {

      try {

        $defeito = new $this -> defeitos;
        $defeito -> assunto      =   $request -> input('assunto');
        $defeito -> duvida       =   $request -> input('duvida');
        $defeito -> satisfacao   =   $request -> input('satisfacao');
        $defeito -> contato      =   $request -> input('contato');

        $defeito->save();
        return response()->json(['status' => 1, 
                                'msg' => 'Defeito salvo com sucesso']);

      } catch (Exception $e ) {
        return response()->json(['status' => 0, 
                                 'msg' => 'Houve um erro ao salvar o defeito']);
      }
   }

}

Route: API

Route::post('clientes/salvar', 'ClienteController@salvar');

Route: Web

Route::get('/defeito', function () {
    return view('template/form-defeitos');
});

  • 1

    Would not be App\Models\Defeito?

  • no error: Undefined namespace Model.

  • Even with Models, plural?

  • that’s right .....

  • was all right, but now began to give this mistake.

  • Then edit your question and enter the codes. If it worked before, also put what you changed in the project.

  • in Routecollection.php line 161 at Routecollection->match(Object(Request)) in Router.php line 533, I just put some Forms.

  • All the places I’ve seen, the models in the Laravel framework are in the namespace Models, then you must have done something wrong. Edit the question and enter the codes.

  • 1

    Possible duplicate of Class not found Laravel

  • The model namespace must be namespace App\Models;

Show 5 more comments

1 answer

2


Import the Defect class in Controler with the operator use

use App\Defeito;

In the constructor correct the name of:

public function __construct(Defei $defeitos) {
      $this -> defeitos = $defeitos;
}

for:

public function __construct(Defeito $defeito) {
      $this -> defeito = $defeito;
}

If you are using DI in the constructor, you do not need to extend the "new" class within the save method again, since it was declared in the $this->defect object.

Corrected would look like this.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Defeito; //Verifique se é este o namespace correto

class DefeitoController extends Controller {

    private $defeito;

    public function __construct(Defeito $defeito) {
        $this -> defeito = $defeito;
    }

    public function salvar(Request $request) {

        try {

            $defeito -> assunto      =   $request -> input('assunto');
            $defeito -> duvida       =   $request -> input('duvida');
            $defeito -> satisfacao   =   $request -> input('satisfacao');
            $defeito -> contato      =   $request -> input('contato');

            $defeito->save();
            return response()->json(['status' => 1, 'msg' => 'Defeito salvo com sucesso']);

        } catch (\Exception $e ) {
            return response()->json(['status' => 0, 'msg' => 'Houve um erro ao salvar o defeito']);
        }
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.