Create record with id - mysql

I need to import the data from a different database and transfer it to the new database. but the ID information of the members is required for the relationship. I have to carry them too.
Therefore, I have to fill in the ID column which is increased as AUTOINCREMENT.
My Migrate Controller
public function migrate(BackupUser $buser, User $user)
{
$backup_user = $buser->get();
foreach ($backup_user as $bulk) {
$user->create([
'id' => $bulk->uye_id,
'name' => $bulk->uye_nick,
'email' => $bulk->uye_mail,
'password' => $bulk->uye_sifre,
'created_at' => $bulk->uye_tarih
]);
}
}
when I do this, the AUTOINCREMENT increases normally and I can not get the ID information of the previous members.
What is the best way to do this?

By default, the id field for User is not fillable by mass assignment like you're doing. If you want to assign it, you must add 'id' to the array of fillable fields in the user class.
app/User.php
...
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'id', 'name', 'email', 'password',
];
...

I think you are trying to take database from joomla or wordpress to laravel.
If I need to do this . my approach would be to store first all base tables without id and then with stored id generated after insertion would be in related tables. So the steps would be
S1: store new `user` and get `id`
S2: relate with other table like `role` with `user_id`
Loop again till last row

Related

Laravel Many To Many Merges Pivot?

I must be going insane or be really tired. So I have this situation where I get a collection of all the Roles assigned to the User. That part goes ok.... however I noticed something super strange.
I am using Laravel 8 and PHP8 (not the strange part).
For some reason, I do not get only the result from the other table but also pivot data is merged in. I can't tell why this is happening. Here is the example:
Relationship on user model:
/**
* Relationship with roles model.
*
* #return BelongsToMany
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(
Role::class,
'role_user',
'user_id',
'role_id'
)->withTimestamps();
}
Relationship on the Role model:
/**
* Relationship with users table.
*
* #return BelongsToMany
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(
User::class,
'role_user',
'role_id',
'user_id'
)->withTimestamps();
}
In the user model, I have this.
$this->roles->each(function($role) {
dd($role);
});
I was expecting to get a dump of related model however for some weird reason what I get is pivot table merged with the model:
"id" => 7 // this is the relation ID from the pivot table
"display_name" => "Administrator" // this is from Role model
"code" => "admin" // role model
"description" => "Super User - can do everything in the system. This role should only be assigned to IT staff member." // role model
"created_at" => "2021-10-01 11:00:00" // pivot table
"updated_at" => null // pivot table
"deleted_at" => null // pivot table
"role_id" => 1 // pivot table
"user_id" => 2 // pivot table
Either I am doing something very wrong or I am missing something very obvious. Does anyone know what in the world is happening here?
Just to add: the data is from both places but the result is just a Role model as expected.
Should I not just get the role model without the pivot stuff in it? It is overriding my role model fields.
EDIT:
Parenthesis seems to make a difference. The data is still merged. However, when I do it like this looks like data from end model is merged (so it overrides) to data from the pivot. So I get correct ID.
$this->roles()->each(function($role) {
echo $role;
});
But this gives me this weird pivot merged version with wrong ID.
$this->roles->each(function($role) {
echo $role;
});
I know what that was exactly. Without thinking I've added the ID column into the pivot table.
This ID from pivot was overriding my ID from my end model. After I've removed it the problem is gone.
I don't know why Laravel would by default add these fields and merge with pivot columns... I guess it just does that for no reason. Although I don't understand what's the point if there is a separate mechanism to access the pivot table (pivot relationship on the model).
This makes me think I did something wrong. But yeah, hope it helps. If anyone knows why Laravel automatically adds pivot stuff, let me know.

Laravel create function insert only some of the fields in DB

I have socialite login for facebook and google. I'm trying to store values in db. Some of them are stored and some no. I think I have tried everything, but can't find problem and solution.
I have function:
public function CreateUser($user, $provider)
{
$authUser = User::where('social_id', $user->id)->first();
if($authUser)
{
return $authUser;
}
$token = $this->generateRandomString();
return User::create([
'name' => $user->name,
'email' => $user->email,
'social' => $provider,
'social_id' => $user->id,
'username' => $this->user_slug($user->name),
'user_token' => $token,
'earnings' => 0,
'user_type' => 'vendor',
'verified' => 1,
]);
}
All Values except social and social_id are stored in database. I tried to store different values in different fields.
For example, if I store $provider value in 'name' field it works. if I try to store $provider value or any other value in 'social' field. DB dosn't store it. Same for 'social_id' field.
I tried saving same value in different fields - It works
I tried to save different values in fields 'social' and 'social_id' - It dosn't work.
I tried to rename these 2 fields - Still dosn't work
If I edit these fields from "phpmyadmin" then it works.
I tried to set fields with different options - It dosn't change anything
DB Photo
you should define which model attributes you want to make mass assignable. You may do this using the $fillable property on the model. For example, let's make the name attribute of our User model mass assignable:
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['social', 'social_id'];
}

Doctrine - Database representation

I tried almost everything trying to make Entities that represent my database schema and still having full page errors. If someone can give me some light I would appreciated.
Basically we have 2 registration forms, one for the brand and one for the influencer. In each form we ask for the user information PLUS the specific information depending on which form page you are.
DATABASE SCHEMA:
Role (this table is already populated with the "type" of person they are, either administrator, brand or influencer)
id |
name
User (this table contains the general information of a person, email and password will be used to login)
id |
id_role FK|
name |
phone |
email |
password |
created_at |
status
Brand (this table contains the specific information of a brand which used the brand form for the registry)
id |
user_id FK|
name_brand |
position_id |
email_brand |
nif_brand |
name_firma |
phone_brand |
Position (this table is already populated with positions we display to be selected, e.g, CEO)
id |
name
District (this table is already populated with districts we display to be selected, e.g, NJ)
id |
name
Influencer (this table contains the specific information of a influencer which used the brand form for the registry)
id |
user_id FK |
surname |
date_of_birth |
email_brand |
district_id FK |
gender
MAIN POINTS:
I already tried to use Doctrine annotation and still didn't worked for me, not sure if it's because Im new to this or if Im not structuring the database/entities correctly or even the managing an entity with its associations into the database.
The idea is that ONE user/person can be EITHER a BRAND or INFLUENCER. That is achieved by the role_id which links to the role table. E.g, if i'm in the brand form and try add my information it will add the personal info in the users table and use the role_id 2 to link to the role table. The specific brand information given will then be inserted in the table BRAND using the user PRIMARY KEY to link both tables. (NOT SURE IF THERE'S A MISTAKE HERE, maybe the user should go through the role table and from that table to the specific table, which is not what i'm doing now). In the BRAND table I ask for it's info and coming from the POSITION SELECT in the form I get the FK position_id and link it to the table POSITION.
A different but similar thing happens with the influencer, which will select his district and then i use that district_id to link the districts table.
The problem is always the foreign key associations with doctrine and property's I need to define in the entities. I already don't declare the foreign keys because I'm aware that doctrine handles the FK relations internaly using objects but can't make this to work..
I would appreciate a lot all the help you guys can share.
Thank you.
I think you're trying to manually do what Doctrine can provide through inheritance.
A small setup that should work with your description of what you're trying to do.
Generic User setup
/**
* #ORM\Table(name="users")
* #ORM\Entity
*
* #ORM\MappedSuperclass
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="discr", type="string")
*/
class User
{
/**
* #var int
* #ORM\Id
* #ORM\Column(name="id", type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #var string
* #ORM\Column(name="name", type="string", length=255, nullable=false, unique=true)
*/
protected $name;
/**
* #var Position
* #ORM\OneToOne(...)
*/
protected $position; // TODO
// getters/setters & other properties
}
Brand user
/**
* #ORM\Table(name="brands")
* #ORM\Entity
*/
class BrandUser extends User
{
/**
* #var string
* #ORM\Column(name="specific_brand_property", type="string", length=255, nullable=false, unique=false)
*/
protected $specificBrandProperty;
// getters/setters & other properties
}
This small setup should already provide you with 2 Entity objects: User and BrandUser, where the latter expands the first.
The primary keys for BrandUser will be Foreign Keys to User and requirements of User are also requirements for BrandUser.
If you're using Fieldset and InputFilter classes you can have the BrandUserFieldset expand the Fieldset for use in your form (example below and more info in one of my repo's on this subject)
class UserFieldset
{
public function init()
{
parent::init();
$this->add([
'name' => 'id',
'required' => true,
'type' => Hidden::class,
]);
$this->add([
'name' => 'name',
'required' => true,
'type' => Text::class,
'options' => [
'label' => 'Name',
],
]);
$this->add([
'name' => 'position',
'required' => true,
'type' => ObjectSelect::class,
// other properties
]);
}
}
Extended for BrandUser
class BrandUserFieldset extends UserFieldset
{
public function init()
{
parent::init(); // <-- adds id, name, position
$this->add([
'name' => 'specificBrandProperty',
'required' => true,
'type' => Text::class,
'options' => [
'label' => 'Name',
],
]);
}
}
Thought to add some commands, they might help you out. My setup requires me to prefix commands with ./vendor/bin/doctrine-module, add what you need for your setup to the following:
orm:validate-schema - will check your Doctrine Annotation for errors. Will also return any errors (syntax and logic) it finds
orm:schema-tool:update - command to create/update your database schema
flag -f - will make :update execute for real
flag --dump-sql will make :update not execute, but have the SQL dumped in your Terminal
flags -f and --dump-sql may be combined
orm:mapping:describe “\Full\Namespace\To\Entity” - Shows all information Doctrine has about a specific Entity - including relations, relation mappings, discriminators, extending (child) and parent entities, et cetera
Also, have a look at a solution I had for a similar problem some time ago.

Relationships between tables in laravel using backpack package

I am using backpack CRUD package to create my website project in laravel 5.2
I want to establish a relationship between two tables. First table is called customer and second table is called transaction. Each customer has many transaction(1:N relationship).
Customer table record:
ID Name
123456 xyz
Transaction table record:
ID CustomerID
101010 123456
I know that I have to specify the relation in the customer model. But, how can I display the result of the relationship in CRUD ?
You should have relationships on both the Transaction and the Customer models, so you can do $customer->transactions and $transaction->customer:
class Customer extends Model
{
/**
* Get the comments for the blog post.
*/
public function transactions()
{
return $this->hasMany('App\Transactions', 'CustomerID', 'ID');
}
}
and
class Transaction extends Model
{
/**
* Get the comments for the blog post.
*/
public function customer()
{
return $this->belongsTo('App\Customer', 'CustomerID', 'ID');
}
}
Spend some time in the Eloquent Relationships Documentation. It's really important to understand them if you want to be a Laravel developer.
In order to display the relationship in the CRUD, you can then use Backpack's select column type to display it in the table view and select or select2 field types to display it in the add/edit views. Read the CRUD Example Entity to better understand how that works.
First of all when you are creating migrations for both tables, table which contain Foreign Key (FK) must have field like this:
public function up(){
$table->increments('id');
$table->integer('customerID')->unsigned();
}
After that you are need to call next command into console
php artisan migrate
Next is going next commands:
php arisan backpack:crud customers
php arisan backpack:crud transactions
After that you need to define functions in models which returns values from other tables. Customer models need to have next function
public function transactions(){
return $this->hasMany('Transaction');
}
Transaction model must have next function
public function customer() {
return $this->belongsTo('Customer');
}
Next you must add CRUD field in Customer controller to display
transactions in select box.
$this->crud->addField([
'label' => 'Transactions', // Label for HTML form field
'type' => 'select2', // HTML element which displaying transactions
'name' => 'customerID', // Table column which is FK for Customer table
'entity'=> 'customer', // Function (method) in Customer model which return transactions
'attribute' => 'ID', // Column which user see in select box
'model' => 'Transaction' // Model which contain FK
]);
Hope this helps :)
After you built onetomany relationship with transaction, you can get the results.
$customer=Customer::where(['id'=>'123456'])->with('transaction')
->first();
print_r($customer->Name); // gives the customer name
foreach($customer->transaction as $cid)
{
print_r($cid->CustomerID); // gives the customer id
}
Laravel Relationships Documentation is always helpful. Go through it.

How to use Zend Adapter to retrieve information from database

I'm trying to figure out the essentials of Zend Adapter. The tutorials and explanations are confusing and needs more clarification. Can someone give me simple examples to fully understand how to work with SQL queries and how to get the desired SQL results?
especially, I'm interested in learning how to get
-- column names
-- table names
-- fetchAll entries
and etc.
Thank you,
Like a lot of other people you seem to be having difficulty with the quickstart, try the tutorial from Rob Allen it helped me get started.
You have multiple choices on how to connect to your table, the confusion with Zend_Db often begins here.
The easiest way when using one DB, in your application.ini file add these lines at a minmum:
resources.db.adapter = "pdo_Mysql"
resources.db.params.username = "user_name"
resources.db.params.password = "password"
resources.db.params.dbname = "db_name"
Alternatively you can connect to a database almost anywhere in your code using the Zend_Db_Adapter:
//using a normal constructor
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
//using factory
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
List of supported Databases
Using this in your application can be as simple as:
//fetchAll using Zend_Db_Adapter and plain SQL
$sql = 'SELECT * FROM bugs WHERE bug_id = ?';
$result = $db->fetchAll($sql, 2);
you can list the tables in a database:
$tables = $db->listTables();
or you can get a full table description (including column names), I included the comment block from the function in Zend_Db_Adapter_Abstract:
/**
* Returns the column descriptions for a table.
*
* The return value is an associative array keyed by the column name,
* as returned by the RDBMS.
*
* The value of each array element is an associative array
* with the following keys:
*
* SCHEMA_NAME => string; name of database or schema
* TABLE_NAME => string;
* COLUMN_NAME => string; column name
* COLUMN_POSITION => number; ordinal position of column in table
* DATA_TYPE => string; SQL datatype name of column
* DEFAULT => string; default expression of column, null if none
* NULLABLE => boolean; true if column can have nulls
* LENGTH => number; length of CHAR/VARCHAR
* SCALE => number; scale of NUMERIC/DECIMAL
* PRECISION => number; precision of NUMERIC/DECIMAL
* UNSIGNED => boolean; unsigned property of an integer type
* PRIMARY => boolean; true if column is part of the primary key
* PRIMARY_POSITION => integer; position of column in primary key
*
* #param string $tableName
* #param string $schemaName OPTIONAL
* #return array
*/
$describTable = $db->describeTable('myTable');
This info should get you started, however I find that a lot of the real power of Zend_Db resides in the Zend_Db_Table, Zend_Db_Table_Row and especially the Zend_Db_Select classes.
I urge you to take some time and figure them out.
As an example of what you might expect from Zend_Db_Table and Zend_Db_select (when not using more advanced mappers and domain objects, hopefully those will come later):
//When using DbTable models that extend Zend_Db_Table_Abstract the model already
//knows the name of the table and has full access to the Db adapter, allowing your code to
//be very brief and descriptive.
class Application_Model_DbTable_Weekend extends Zend_Db_Table_Abstract
{
//name of table, required if classname is not the same as the table name
protected $_name = 'weekend';
//primary key column of table, a good idea especially if primary key is not 'id'
protected $_primary = 'weekendid';
public function getWeekend($weekendId) {
//create select object
$select = $this->select();
$select->where('weekendid = ?', $weekendId);//placeholder syntax
$result = $this->fetchRow($select);
if (!$result) {
throw new Exception('Could not find weekend ID ' . $weekendId);
}
return $result;//returns a single row object
}
public function fetchAllWeekend() {
$select = $this->select();
$result = $this->fetchAll($select);
return $result; //returns array of row objects (rowset object)
}
}
Rob Allen's Zf tutorial will explain how DbTable models are setup and how they Work.
hope this helps...