mercredi 26 août 2015

My database records do not reflect on artisan tinker console

I'm trying to develop a backend for a small service using Laravel. The main reason I've chosen this framework - it's a bit Railsesque.

I've not find any resource about naming conventions on Laravel, so followed the Rails route (models in singular form - Category, controllers CategoriesController, and according database table - categories). I firstly created migration, then made it run. And it created 4 database tables as I expect. Then I manually entered some records from an SQL dump file. Then declared table names inside models. The problem is, these records doesn't reflect on artisan console when I query them.

I have no serious experience with php frameworks, so a detailed answer is appreciated :)

Psy Shell v0.5.2 (PHP 5.6.12 — cli) by Justin Hileman
>>> App\Subcategory::all();
=> Illuminate\Database\Eloquent\Collection {#692
     all: [],
   }
>>> App\Category::all();
=> Illuminate\Database\Eloquent\Collection {#694
     all: [],
   }
>>> App\Category::count();
=> 0
>>> App\Subcategory::count();
=> 0

Model definitions

#########Category.php###########
namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = 'categories';
    protected $fillable = ['name', 'show_in_nav'];

}

#######Subcategory.php########
...

class Subcategory extends Model
{
    //
    protected $table = 'subcategories';
    protected $fillable = ['name', 'image_path', 'category_id'];
}
#########Product.php###########
....

class Product extends Model
{
    //
    protected $table = 'products';
    protected $fillable = ['name', 'image_path', 'subcategory_id', 'description'];
}

Some database records

mysql> SELECT * FROM subcategories LIMIT 5;
+----+--------+-------------+------------------+
| id | name   | category_id | image_path       |
+----+--------+-------------+------------------+
|  1 | Lorem  |           1 | /images/img1.jpg |
|  2 | Ipsum  |           2 | /images/img2.jpg |
|  3 | Dolor  |           1 | /images/img2.jpg |
|  4 | Amet   |           4 | /images/img4.jpg |
|  5 | Fabula |           5 | /images/img3.jpg |
+----+--------+-------------+------------------+
5 rows in set (0.00 sec)

mysql> SELECT id FROM categories LIMIT 5; 
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
+----+
5 rows in set (0.03 sec)



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1EkZHHD
via IFTTT

Aucun commentaire:

Enregistrer un commentaire