I have a Laravel app with the users table. I want it (the table) to have username column instead of name column, i.e. structure like this:
| id | username | email | password | ...etc. |
| --- | ---------- | ------------------ | ------------- | ------- |
| 1 | Username 1 | email1#example.net | xEvehgv#52... | ... |
| 2 | Username 2 | email2#example.net | dkjbg#%R!D... | ... |
How can I do this with the help of Laravel 9 without having problems with form data validation and third-party packages? Thank you in advance!
Note: changing the column name in the users table is not a good idea because a lot of packages use the default name of the table. Instead, you can add a new column with your customer name.
For example:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('username');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Otherwise, you can achieve this by going to database/migrations/_create_users_table.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('username');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
And also you have to run migrations
php artisan migrate
or execute this command:
php artisan migrate:refresh
I've just found the answer to my question here. Thank you, everyone, for paying attention to this question.
Related
I'm trying to make the id start from a certain value. It's working fine when I don't specify the startingValue(1000)
This is my products table schema
Schema::create('products', function (Blueprint $table) {
$table->id()->startingValue(1000);
$table->foreignId('store_id')->constrained()->onDelete('cascade');
$table->foreignId('category_id')->constrained()->onDelete('cascade');
$table->string('name');
$table->float('price');
$table->string('specification');
$table->string('description');
$table->timestamps();
});
This is my favorites table schema
Schema::create('favorites', function (Blueprint $table) {
$table->foreignIdFor(Product::class)->constrained()->onDelete('cascade');
$table->foreignIdFor(User::class)->constrained()->onDelete('cascade');
});
It gives me this error
Thanks 🙏
Good Day
I have a Laravel 7 project that's having some issues, first i have this error
The solution that i found for that error is to edit 'strict' value to false in config/database.php. This fixed the error. But now i just realized that every time I create a user the value of the id is always "0" example
Here is my migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('phone_number');
$table->string('client_address');
$table->rememberToken();
$table->timestamps();
});
}
it is because of your id field in table is not AuthoIncrement
first of all remove users table in database
then remove create_users_table record in migrations table in database
and then change your migration to this
$table->bigIncrements('id');
and then php artisan migrate
i have three table:
Suppliers
id | name | type
1 | Test1 | Seller
2 | Test2 | Manufacturer
Products
id | name
1 | product1
2 | product2
Prices
id | supplier_id | product_id | price
1 | 1 | 1 | 1000
2| 2 | 1 | 2000
each supplier have many products and each products belong to many suppliers.
Now each supplier can have a separate price for each product.
i have a question that how to create model and table for this scenario?
You will have two tables with models:
Product and Supplier
Within /Database/Migrations create_products table:
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
create_suppliers table:
Schema::create('suppliers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('type');
$table->timestamps();
});
You will need a pivot table, so create it with Artisan command:
php artisan make:migration create_product_supplier_table
A quick note: Pivot names should be in alphabetical order to fit laravel name convention. 'P' letter comes first and then 'S' (product_supplier) in this case.
product_supplier table:
Schema::create('product_supplier', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('product_id');
$table->unsignedBigInteger('supplier_id');
$table->integer('price');
$table->timestamps();
});
Now, you will need the following relation declarations in two models
Within App\Product class:
protected $guarded = [];
public function suppliers()
{
return $this->belongsToMany(Supplier::class)->withPivot('price');
}
And also in App\Supplier class:
protected $guarded = [];
public function products()
{
return $this->belongsToMany(Product::class)->withPivot('price');
}
You set it up now. Let's give it a try:
$supplier = App\Supplier::create(['name' => 'supplier1', 'type' => 'seller']);
$product = App\Product::create(['name' => 'product1']);
$supplier->products()->attach($product, ['price' => 80]);
Call it back:
$supplier->products; // it will give you the products that attached to supplier.
So, the any supplier can have any product with any price tag.
In my case i have a User
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('address_id')->nullable();
$table->foreign('address_id')->references('id')->on('addresses');
$table->string('first_name');
$table->string('insertion')->nullable();
$table->string('last_name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('phone_number');
$table->string('avatar')->nullable();
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
And i have a Address
Schema::create('addresses', function (Blueprint $table) {
$table->increments('id');
$table->string('city');
$table->string('street');
$table->string('zip_code');
$table->string('state');
$table->string('house_number');
$table->timestamps();
});
Is it possible to create a new user and add a address to it in the same Nova User Resource?
What can we add in the fields function of the Resource?
Do i need to overwrite the default create method? Or can this be fixed with a belongsTo ?
Currently i got it to work with using
morhpOne
method but this is not what i want. I first have to create a User and after that i can add an Address.
This should be simple, in your Nova User model add
BelongsTo::make('Address');
The Address field should represent the function in your User model that returns the Address relation
So in your user there should be
public function address()
{
return $this->belongsTo(Address::class);
}
I hope that that is what you are looking for, I will be happy to update my answer if given more information.
I have two models
Stations
Operators
I am currently trying to "save" several Operators to a Station
but i want also to be able to "save" the same Operator to another Station.
Example:
+---------------------------------+
| Station | Operator(s) |
|---------------------------------|
| Munich | Lufthansa |
| | KLM |
| | Air Malta |
|---------------------------------|
| Berlin | Lufthansa |
| | KLM |
|---------------------------------|
|------- etc ---------------|
|---------------------------------|
My Stations Table:
Schema::create('stations', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 100);
$table->timestamps();
});
My Stations Model:
public function operators() {
return $this->hasMany(Operators::class);
}
My Operators Table:
Schema::create('operators', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 100)->unique();
$table->string('email', 100);
$table->boolean('notify')->default(false);
$table->timestamps();
});
My Operators Model:
public function stations() {
return $this->belongsTo(Stations::class);
}
Here i must say that i am creating the Station and trying to add the Operators:
In StationsController:
After receiving the Ids of the Operators and the Name of the Station:
$station = new Stations;
$station->name = request('name');
$station->save();
foreach (request('operators') as $operator) {
$tempOperator = Operators::find($operator);
$station->operators()->associate($tempOperator)->save();
}
The response is:
"Call to undefined method Illuminate\Database\Query\Builder::associate()"
I know there is something wrong with the relations but i cannot figure it out... Thank you in advance
Rollback your migration php artisan migrate:rollback
change your operators table migration like this,
Schema::create('operators', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 100)->unique();
$table->string('email', 100);
$table->boolean('notify')->default(false);
$table->timestamps();
});
you have to create a mapping table like,
Schema::create('station_operators', function (Blueprint $table) {
$table->increments('id');
$table->integer('stations_id')->unsigned();
$table->integer('operators_id')->unsigned();
$table->timestamps();
$table->foreign('stations_id')->references('id')->on('stations');
$table->foreign('operators_id')->references('id')->on('operators');
});
Run migrate php artisan migrate
Your Stations Model:
public function StationOperators() {
return $this->hasMany(StationOperators::class);
}
Your Operators Model:
public function StationOperators() {
return $this->hasMany(StationOperators::class);
}
Your StationOperators Model:
public function Stations() {
return $this->belongsTo(Stations::class);
}
public function Operators() {
return $this->belongsTo(Operators::class);
}
For associate,
$station = new Stations;
$station->name = request('name');
$station->save();
foreach (request('operators') as $operator) {
// $tempOperator = Operators::find($operator);
// $station->StationOperators()->associate($tempOperator)->save();
$data = [
'stations_id' => $station->id,
'operators_id' => $operator,
];
$stationOperator = new \App\StationOperators();
$stationOperator->save();
}
You need an intermediate StationsOperators table.
Then try updating your operators model like this:
public function stations() {
return $this->belongsToMany(Stations::class)->using(StationsOperators::class);
}
Remember:
All custom models used to represent intermediate tables of relationships must extend the Illuminate\Database\Eloquent\Relations\Pivot class.
This is a Many-Many relation. You have to use pivot table.
#ref: https://laravel.com/docs/5.5/eloquent-relationships#many-to-many
The associate method only available for belongsTo relationship, so it won't work for many to many relationship.
You can use the attach method for many to many, here is the example from laravel doc https://laravel.com/docs/5.5/eloquent-relationships#updating-many-to-many-relationships