Laravel 5 Composer update fails - laravel-5

After upgrading a Laravel project from 5.0 to 5.1.x, I can not run a composer update correctly.
The app itself works fine and no problems, but will need composer to work.
This is the error I am receiving after running sudo composer update
[LogicException]
The command defined in "Illuminate\Database\Console\Migrations\MigrateMakeCommand" cannot have an empty name.
Script php artisan clear-compiled handling the pre-update-cmd event returned with an error
[RuntimeException]
Error Output:
When trying to debug the file Illuminate\Database\Console\Migrations\MigrateMakeCommand I can not find any noticable error.
Any help would be greatly appreciated.

Laravel is giving its best to maintain the relationship first of all you need to have 2 model or you can say two table for relation and the respestive columns now you need to specify whether it has relation of one to one or one to many or many to many then you can simple specify it on model. As your tables are not specific tables so you can see the example like relation between users table and questions table relation here is one to many . One user can have many question while one question is belong to the user now In my Questions model the realtion looks like public function user() {
return $this->belongsTo('App\User');
}
now in the user model my code looks like
return $this->hasMany('App\Questions');
}
be sure you have maintained foreign key while migrating :) for more you can see here http://laravel.com/docs/5.0/eloquent#relationships hope this would help

Related

How to run Laravel Migrations, then Seeders, then more Migrations

I am re-building my Laravel app and, in the process, redesigning the data model.
I have a subset of my Migrations (35) I need to run to create the data model in the new app.
Then, I need to run some Seeders to populate the new tables.
Some of the new tables (12) have a column "old_id" where I place the "id" from the old data model to handle foreign keys/joins. I run a series of Update statements to change the foreign key values from the "old_id" to the new id.
Then, I want to run additional Migrations (12) that drop the "old_id" columns.
Here are the commands I'm running currently that do everything for me - clear DB, run migrations, populate data, and update keys.
php artisan migrate:reset
php artisan migrate:fresh --seed --seeder=DatabaseSeeder
I'm trying to find a way to only run a portion of my Migrations prior to executing DatabaseSeeder, and then run the remaining Migrations after (or as the last step of) the DatabaseSeeder.
Contents of DatabaseSeeder::class:
public function run()
{
$this->call([
// Seeders to populate data
UserSeeder::class,
AssociationSeeder::class,
... lots more classes ...
// Last Seeder class executes Update statements to update foreign keys
DatabaseUpdateSeeder::class,
]);
Thank you!
I ended up following the advice of the comment on the original post. While my previous "answer" also works, this is the right way to do it; separating round 1 and round 2 Migrations, and not executing Seeders from a Migration.
Here are my 2 commands (could be 3 if I wanted a separate command in the middle that only executes the Seeder), but adding the Seeder on the end of the command is supported syntax.
php artisan migrate:fresh --path=/database/migrations/batch-one --seed --seeder=DatabaseSeeder
php artisan migrate --path=/database/migrations/batch-two
I was able to achieve this by creating a Migration that calls my DatabaseSeeder. Here is how: https://owenconti.com/posts/calling-laravel-seeders-from-migrations
Then, after that Migration, I just have an additional Migration(s) to drop the "old_id" columns.
I also want to call out the comment from Tim on my original post with an alternative that would also work, but required multiple statements from the command line.

How to resolve Not unique table/alias error in laravel?

I am very new to the laravel,i am using l5-repository package for orderBy and sortedBy to sort columns ,while hitting API i am getting following error please help me to resolve the issue
my API URL :-http://localhost.com/v1/domain?limit=250&page=1&orderBy=users|name&sortedBy=desc
You probrably have an error in your table name. For default, laravel will look for cards table name for a model named Card. Also, take a look in your models name. I espect you misstype card.php because it should be Card.php. Laravel uses a lot of psr convention and Eloquent (who deal with relationships and that stuff). If you are not well versed into it, take a look in how to name classes. As Alireza said, take a look in the Laravel's documentation, it is awesome and complete. When you have a one to many realtionship, one of they should have a hasMany(...) instead belongsTo(....)

Can this block of code of mine can be improved

[This is to populate data from two tables that one has two foreign keys from the same column as reference on the other table]
https://i.stack.imgur.com/D8fiv.png
[This is my schema for the table with the foreign key]
https://i.stack.imgur.com/eYDL0.png
This is written in laravel and it is working however i have an itchy feeling that this is wrong
As someone else has commented, you should put the code in your question. More context might also be necessary as it's not clear what you are trying to return from your view(). Are you returning $citizen, $family, or both? It would also be helpful to include what version of Laravel you are using. I'll assume you are using the latest Laravel 8.
Your code should work but you are making more work for yourself if you don't utilize Laravel's relationships. If you do, all the query stuff you have in your code can be reduced to just a few short lines of code such as this:
public function view(Citizen $citizen)
{
$citizen->load('family');
return $citizen;
}
Database Migration
You can shorten your migration by using foreignId(). See docs for details
https://laravel.com/docs/8.x/migrations#foreign-key-constraints
Before:
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('citizens');
After:
$table->foreignId('client_id')->constrained();
Route Model Binding
I'm assuming your view() method is in your controller and is called from a route. Since the one parameter required is the client's id, you can use Route Model Binding and skip fetching the client from the database.
https://laravel.com/docs/8.x/routing#route-model-binding
public function view(Citizen $citizen)
{
// your code
}
Relationships
You seem to have relationships set up but you aren't using them to load data. You should be able to load the related objects without any manual queries.
If you have a belongsTo(One to Many) relationship on the Citizen model to the Family model, you can load it like this:
$citizen->load('family');
or just use it like this
$relationship = $citizen->family->relationship;
I don't know what the relationships between your models are but you should read up on the different relationship types in the docs.
https://laravel.com/docs/8.x/eloquent-relationships

Laravel Eager loading of a relationship bettween two models causes a relationship between other models return wrong results

I have two Eloquent models named Question and Answer with one to many relationship between them (one question has many answers). The hasMany relationship in Question.php is called answers.
I also have a User and a Company model with a many to many relationship between them which uses a pivot model, defined this way:
User.php
public function companies()
{
return $this->belongsToMany(Company::class)
->using(CompanyUser::class);
}
Company.php
public function users()
{
return $this->belongsToMany(User::class)
->using(CompanyUser::class);
}
When I retreive a question and lazy load its answers:
Question::find(58)->answers;
Everything is okay. The problem comes when I use eager loading:
Question::with('answers')->get();
Something strange happens. In the answers() relationship method of the Question model, I need to get the current user's first company in order to modify the relationship:
auth()->user()->companies->first();
Most of the users in my application have one company attaches to them, when using eager loading though auth()->user()->companies returns not one, but 134 companies even though in the database I have only 5 companies and the current user belongs to only one. When I dumped the contents of the auth()->user()->companies collection I saw that the first company model is exists 130 times and the other 4 companies are also included.
This happens only in the answers() method and only when using eager loading. Any ideas why?
Environment:
Laravel version: 6.20.6
PHP version: 8.0.1
Apache: 2.4.26
DB: 10.1.27-MariaDB
I think you need to get more information on the problem. What I normally do is insert ddd() and check the queries tab to see what queries Laravel is using to get the data. This might shed some more light on the problem.

Laraver - display model structure

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.

Resources