php - Class not recognized - laravel Eloquent Model

483

I am trying to update my database using Eloquent model but the class is not recognized.

First I created my table using migration and that worked fine.. Below is the code use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;

class CreatePaintings extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
 public function up()
 {
    Schema::create('paintings',function($thepainting){
            $thepainting->increments('id');
            $thepainting->string('title');
            $thepainting->string('artist');
            $thepainting->integer('year');
            $thepainting->timestamps();

    });
 }

 /**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('paintings');
}
}

Next, I created a class "paint" using the model. But note that the recent version of laravel don't have the model folder specified explicitly. So when I ran the code below on the command prompt I was able to create the paint class

php artisan make:model paint

Lastly, I tried updating the created table, paintings, via routes.php using the code below..

Route::get('/', function() 
{
$paintings = new Paint;
$paintings->title = 'Emmanuel';
$paintings->artist = 'D. DoRight';
$painitngs->year = 2014;
$paintings->save();

return view('trynn');

});


 Route::get('about/directions', function() 
 {
 return "Direction content goes here";
  });

 Route::get('about/{theSubject}', function($theSubject) 
  {  
  return $theSubject. " content goes here";

  });

please I am new to laravel, so I will appreciate any help to get this resolved. I am presently stranded. Lest I forget, The error message is shown below


Whoops, looks like something went wrong.

1/1 FatalErrorException in routes.php line 18: Class 'Paint' not found

in routes.php line 18

232

Answer

Solution:

Ah, this is a simple error. Can you copy-paste the whole content of the file in if that is not all of it.

Also the error for this is because you haven't brought in the class from the autoloader. In Laravel, everything is namespaced. In your Paint Model, at the top you will seenamespace App, if you moved the Paint model into say a models directory, you need to namespace the classApp\Models;

To fix this error, at the top of theroutes.php file, writeuse App\Paint;. But you should really send that route to a controller to separate the code from the routes.

Let me know if that helps.

583

Answer

Solution:

it's normal the class Pain not found, check what you done here :

php artisan make:model paint

and on your routes file :

$paintings = new Paint;

try to correct your model class name to this Paint , and when you want to instanciate it don't forget the namespace by default for the generated model class , so you routes file will look like this

Route::get('/', function() 
{
      $paintings = new App\Paint;
      $paintings->title = 'Emmanuel';
      $paintings->artist = 'D. DoRight';
      $painitngs->year = 2014;
      $paintings->save();
      return view('trynn');

});

People are also looking for solutions to the problem: mysql - PHP header redirect not working after update query

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.