I am creating a new user. After creating, I need the ID of the user, which is automatically set by the database.
$user = new User();
$user->save();
dd($user->id);
But the response I get is null. Is there a way to get the ID of the user?
I use Sentinel to manage my users. I have adapted the users migration to my needs. Now it looks like this:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('first_name');
$table->string('last_name');
$table->string('username')->nullable()->unique();
$table->string('email')->nullable();
$table->string('password')->nullable();
$table->text('permissions')->nullable();
$table->string('doodle_token', 64)->nullable()->unique();
$table->timestamps();
$table->engine = 'InnoDB';
});
I'm using Laravel version 5.8
Related
Im using SQL uuid_short to build my laravel. is it possible to apply uuid to spaite laravel?
Schema::create($tableNames['permissions'], function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('permissions_uuid'); //(add by myself)
$table->string('name');
$table->string('guard_name');
$table->timestamps();
});
//My Uuid creation
DB::selectOne(DB::raw('select uuid_short() as uuid'))->uuid
I am in-progress to create a project in Laravel.
I have two Database Table, which is created by migrations:
I want to setup multiple foreign key in database schema.
Table User
Schema::create('tr_users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email');
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();
$table->string('password');
$table->unsignedBigInteger('departement_id')->nullable();
$table->foreign('departement_id')
->references('id_departement')->on('tr_departements')
->onUpdate('cascade')
->onDelete('cascade');
});
Table Departrements
Schema::create('tr_departements', function (Blueprint $table) {
$table->bigIncrements('id_departement');
$table->string('name');
$table->timestamps();
$table->unsignedBigInteger('created_by')->nullable();
$table->foreign('created_by')
->references('id')->on('tr_users')
->onDelete('cascade');
$table->unsignedBigInteger('updated_by')->nullable();
$table->foreign('updated_by')
->references('id')->on('tr_users')
->onDelete('cascade');
});
What I want to know is how to describe relation in Models?
Edit:
My plan to use this relation is every users is belongs to departement, and any departement has many users, and every users has only one departement
If you are seeking One-to-One relationship from user to department then relation would be like this:
In your User model code would be like this:
public function department()
{
return $this->hasOne('App\Department');
}
And in your department model:
public function user()
{
return $this->belongsTo('App\User');
}
For multiple relationship you can refer laravel documentation
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 am not well conversant with laravel 4 but am trying out.
How can I allow guests to add items to cart before login or registration, then keep their content on login in laravel 4.2?
This is my carts table
Schema::create('carts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('product_id')->unsigned();
$table->string('quantity');
$table->string('total');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('product_id')->references('id')->on('products');
});
My users table
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->boolean('active')->default(0);
$table->string('confirmation_code')->nullable();
$table->string('password',60);
$table->rememberToken();
$table->timestamps();
});
CartController is as below
$buyer = Auth::user()->id;
$product_id = Input::get('product_id');
$quantity = Input::get('quantity');
$product = Product::find((int)$product_id);
$total_price = $product->price * $quantity;
$count = Cart::where('product_id','=',$product_id)
->where('user_id','=',$buyer)->count();
if ($count)
{
return Redirect::back()->with('error','The product already exists in the cart');
}
Cart::create([
'user_id'=>$buyer,
'product_id'=>$product_id,
'quantity'=>$quantity,
'total'=>$total_price
]);
Anyone with an idea? Thanks in advance.
i tried to use sessions and i got right, thanks
you can follow laravel.com/docs/4.2/session
I have a 3 way pivot table that ties a role to a user for a specific organization. Here's how the relevant tables are set up:
//users table
Schema::create('users', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('username', 30)->index();
//more (irrelevant) fields here
$table->timestamps();
});
//roles table
Schema::create('roles', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name', 100)->index();
$table->string('description', 255)->nullable();
$table->timestamps();
});
//organizations table
Schema::create('organizations', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name', 255);
$table->string('slug', 100);
$table->text('description');
$table->timestamps();
});
//organization user role table
Schema::create('organization_user_role', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('role_id')->unsigned()->index();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('organization_id')->unsigned()->index();
$table->foreign('organization_id')->references('id')->on('organizations')->onDelete('cascade');
$table->timestamps();
});
Each user can belong to one or more organizations, with one or more roles. Roles are tied to both a user and an organization simultaneously. That is to say, a user must have a role in an organization in order to be associated with it.
This doesn't seem to fit the mold of the traditional many-to-many relationship pivot-table that works so well out-of-the-box with Eloquent. Can Eloquent handle this type of relationship, or do I need a new model dedicated to handling the relationship? Can someone please show me what the User, Organization, and Role models would look like to tie the 3-way pivot table relationships together with Eloquent?
Check this out: http://github.com/jarektkaczyk/Eloquent-triple-pivot
It works, however I wouldn't call it complete solution. Still you can build yours on top of that.