I am trying to use Model Factories to generate some data while I work on an App. I have created the data which I can see in the database, however, when searching with tinker it can't find anything.
I have manually added an entry using Sequel Pro and Tinker can find that. What am I doing wrong?
So, for example:
id 25 was inserted by the model factory
id 26 was manually inserted by myself
$c = App\Models\Contact::find(25); retruns null
$c = App\Models\Contact::find(26); retruns the correct record
The problem could be in the way that you are creating your models using the Model Factories.
Try to use the method ->create() instead of ->make() on you model creation.
Its strange, Basically $c = App\Models\Contact::find(25);, And
$c = App\Models\Contact::find(26);
are same for PHP, Apache, Mysql/MariaDB, It doesn't make any sense. But any way there are a few ways to check it out !
Restart your local server lamp/wamp/xampp/mamp.
Stop your php artisan serve and restart it.
Review your model factories.
Check DB table for id 25. Remove views
cacahe php artisan view:clear.
Hope it helps.
Related
Using Laravels Baum package for nested sets I try to fetch the relational model on getAncestorsAndSelf() but does not work
$tree = \App\GroupDeprecator::where('id', $this->team_id)->with('lead')->first();
dd($tree); //this one shows the user object
dd($tree->getAncestorsAndSelf());//this one does not shows the user object
What went wrong in this case?
How you solved it? the code looks fine and should work, unless it has been manually updated in the database, I would do:
GroupDeprecator::rebuild(true)
to update the lft, rgt and depth columns
I have some tables that are already created in the database. The migrations seem to require that you first destroy the table and then create it. From what I understand, the creation process generates a "model" at the same time as well as "getters" and "setters" for the fields.
The problem is that I have a database full of tables already. I would like to access the tables that have already been created using Laravel as an interface. Is there a way to generate a model, getters and setters for tables that already exist?
How can I best do this?
TIA
The default model generator doesn't create any getters or setters. All it does is create a basic model class with the correct model name based on the table name. You can create the models without actually creating the migration. The command is php artisan make:model ModelName.
So the answer to your question is no, there isn't any. You need to manually create each model and add the methods yourself. You might try your luck with some laravel package which might do this for you.
Note : Laravel Eloquent is an ActiveRecord implementation. You don't need any getters or setters to work with the table columns. Even a completely empty model with the correct name to match the table would be enough to start using Eloquent.
i have an existing database and all my models are defined already. I would like to scaffold Controllers and Views because it's really time consuming.
i found this which sound good: infyom
They say in the documentation that you can use it from an existing database but any command keeps asking me for fields, (as it is trying to create a model from scratch), what i want to to use my existing models.
I found the "scaffold from table" option in the documentation:
php artisan infyom:scaffold Equipement --fromTable --tableName=Equipement
But it says:[ErrorException]
Undefined index: Equipement
i guess it did not find my existing model.
I was wondering, what do they call datatable anyway ? I can see in their config file they have a folder for that, maybe i should define my models there somehow ?
Thanks for anyone who can help on that. (or providing an alternative solution for scaffolding from existing database/models)
You must add id and:
updated_at` TIMESTAMP NULL DEFAULT NULL,
deleted_at` TIMESTAMP NULL DEFAULT NULL
in your table.
Im learning laravel.
My question is about some simple way do display model structure. I have little experience with django and as i remember, structure for each model was placed inside model files.
Yet in laravel, i need to put starting structure inside migration file:
$table->increments('id');
$table->timestamps();
$table->string('name')->default('');
Then if i want to add some new field, i will place this field in next migration file, etc.
So, is there any way to see some kind of summary for model? Maybe some bash command for tinker?
There are a bunch of options for you to choose from.
If you would like to show a summary of a model while you are in tinker, you can call toArray() on an instance of your model.
Ex:
$ php artisan tinker;
>>> $user = new App\User(['email' => 'john#doe.com', 'password' => 'password]);
>>> $user->toArray();
If you are trying to see a summary of a model displayed on your webpage, just var_dump or dd(...) an instance of your model after calling toArray() on it, and you'll get the same result as above, just in your web browser.
If you are looking for a way to show the table structure without creating any Model instances, you can display the table structure in your terminal, the exact command depending on what database you are using.
For example in MySQL you would do something like:
mysql> show COLUMNS from USERS;
It might also be a good idea to get a GUI app, I like Sequel Pro (for Mac).
P.S. I would just add that you should only have separate migrations for adding new fields when you are already in production and can't lose data from your database. While you are still in development and don't care about your data, it is much better to call php artisan migrate:rollback, add the new field to your create migration, and then php artisan migrate again, rather than making tons of new migration files.
If I have tables in doctrine for user_1, user_2, etc. is there a way to dynamically set the table name in Doctrine for a single User model?
It's weird, I know. I'm trying to create an interface to a WordPress database (because WP has little to no API for directly accessing posts), and WP creates duplicate tables for each site, so there's a wp_posts, wp_comments, wp_2_posts, wp_2_comments, etc.
Here's what I ended up doing:
$post = new WordPressPost();
$post->setTableName('wp_'.$user_id.'_posts');
If it could, you would have to run migrations for each added/deleted user.
I am curious; why would you EVER need something like that?
I don't know how WP works, but here is the thing; each site should use it's OWN database, not to share it with others.