eloquent relation shows null - laravel

I have migrations for Users and Projects like this:
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->bigInteger('last_used_society_id');
$table->bigInteger('last_used_project_id');
$table->rememberToken();
$table->timestamps();
});
and
Schema::create('projects', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('eplan_name')->nullable();
$table->bigInteger('society_id');
$table->timestamps();
//$table->foreign('user_id')->references('id')->on('users');
});
Schema::table('projects', function (Blueprint $table) {
$table->foreign('society_id')->references('id')->on('societies');
});
table Users is altered after to add foreigns:
Schema::table('users', function (Blueprint $table) {
$table->foreign('last_used_society_id')->references('id')->on('societies');
$table->foreign('last_used_project_id')->references('id')->on('projects');
});
and in model of User i have this:
public function ActualProject(){
return $this->belongsTo('App\Models\Project', 'users_last_used_project_id_foreign');
}
public function ActualSociety(){
return $this->belongsTo('App\Models\Society', 'users_last_used_society_id_foreign');
}
but when i try to call $user->ActualProject it return null

why using 'users_last_used_project_id_foreign'?
just use the column's name as it is ...
public function ActualProject(){
return $this->belongsTo('App\Models\Project', 'last_used_project_id');
}

Related

Having problems with Laravel Migrations

I have added all my in constraints 2020_06_17_221942_create_constraints_table
but its not working
All migrations successfully done but without any table constraints the 2020_06_17_221942_create_constraints_table is :
Schema::table('seasons', function (Blueprint $table) {
$table->unsignedBigInteger('best_project_id');
$table->unsignedBigInteger('manager_id');
$table->foreign('best_project_id')->references('id')->on('projects');
$table->foreign('manager_id')->references('id')->on('users');
});
Schema::table('admins', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onDelete('cascade');;
});
Schema::table('projects', function (Blueprint $table) {
$table->foreign('study_major_id')->references('id')->on('study_majors');
$table->foreign('user_id')->references('id')->on('Users');
$table->foreign('season_id')->references('id')->on('Seasons');
})
Try adding "Blueprint" type-hinting to the migration function. It should work
For example:
Schema::table('seasons', function (Blueprint $table) {
$table->unsignedBigInteger('best_project_id');
$table->unsignedBigInteger('manager_id');
$table->foreign('best_project_id')->references('id')->on('projects');
$table->foreign('manager_id')->references('id')->on('users');
});
Replace your last query with this
Schema::table('projects', function (Blueprint $table) {
$table->unsignedBigInteger('study_major_id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('season_id');
$table->foreign('study_major_id')->references('id')->on('study_majors');
$table->foreign('user_id')->references('id')->on('Users');
$table->foreign('season_id')->references('id')->on('Seasons');
});

Error setting multiple auth - undefined constant

My code is:
public function up()
{
Schema::create('users', function (Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('email', 150)->unique();
$table->string('username', 150)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->boolean(‘isAdmin’)->nullable();
$table->string('password', 150); $table->rememberToken();
$table->timestamps(); });
}
Error in cmd
Code / Migrations
Can someone help me?
I already tried to switch the nullable on admin to default(false) but that didn't work
First of all put your code here not image. Change the line to
$table->boolean('isAdmin')->nullable()
you must use quotation.
You are using the wrong quotation inside your code, Use single quotes ' instead of ` on isAdmin
public function up()
{
Schema::create('users', function (Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('email', 150)->unique();
$table->string('username', 150)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->boolean(‘isAdmin’)->nullable();
$table->string('password', 150); $table->rememberToken();
$table->timestamps(); });
}
Change this line
$table->boolean(‘isAdmin’)->nullable();
to
$table->boolean('isAdmin')->nullable();
Please update your migration file like:
public function up() {
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email', 150)->unique();
$table->string('username', 150)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->boolean('isAdmin')->nullable();
$table->string('password', 150);
$table->rememberToken();
$table->timestamps();
});
}
Updated Answer
public function up() {
Schema::create('users', function ($table) {
$table->increments('id');
$table->string('name');
$table->string('email', 150)->unique();
$table->string('username', 150)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->boolean('isAdmin')->default(false);
$table->string('password', 150);
$table->rememberToken();
$table->timestamps();
});
}
Your function like:
public function index() {
return view('home');
}
public function admin() {
return view('admin');
}
Note: Mainly update isAdmin field in migration file

How to save user id and role id in userrole table while registering user in laravel?

I have a roles table, users table and users_role table. I want to store the user_id and roles_id while registering user. The roles should be 1 by when registering the user. How can i achieve this??
Roles Table
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
Users table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('image')->nullable();
$table->boolean('status')->default(0);
$table->rememberToken();
$table->timestamp('email_verified_at')->nullable();
$table->softDeletes();
$table->timestamps();
});
}
UserRoles Table
public function up()
{
Schema::create('users_role', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')-
>onDelete('cascade');
$table->unsignedInteger('role_id');
$table->foreign('role_id')->references('id')->on('roles')-
>onDelete('cascade');
$table->timestamps();
});
}
You can do this by:
$user = new User();
... insert data
$user->save();
$user->roles()->attach($roles);
Refer to documentation

Laravel through relations

I have multi level relation in my app
Category
|products
|product_items
|product_group
|product_attributes
Category Model
public function products(){
return $this->belongsToMany(Product::class);
}
Product Model
public function items()
{
return $this->hasMany(ProductItem2::class);
}
Item Model
public function attribute()
{
return $this->belongsToMany(AttributeValue::class,'product_groups','item_id','attribute_id')->withTimestamps();
}
Attribute value
public function attributeName(){
return $this->belongsTo(Attribute::class,'attribute_id');
}
all relations works fine but now I need filter my product by example 'colors'
so I tried
return $this->where('parent_id',0)->whereHas('childrenRecursive')->with('products.items.attribute.attributeName')->get();
put of course it return all categories with all product and product items etc
here is the migration files
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('image');
$table->string('cover')->nullable();
$table->string('url')->nullable();
$table->string('cover');
$table->text('url_cover');
$table->text('description')->nullabel();
$table->unsignedInteger('parent_id');
$table->boolean('elite')->default(0);
$table->timestamps();
});
Schema::create('category_product',function (Blueprint $table){
$table->integer('category_id');
$table->integer('product_id');
$table->primary(['product_id','category_id']);
});
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('description');
$table->string('serial_number');
$table->float('price');
$table->integer('discount');
$table->enum('type',['pound','percentage']);
$table->timestamp('start')->nullable();
$table->timestamp('end')->nullable();
$table->string('image');
$table->unsignedInteger('shop_id');
$table->boolean('recommendation');
$table->boolean('published');
$table->integer('preparing_days')->unsigned();
$table->integer('visits')->defualt(0);
$table->unsignedInteger('priorty')->defualt(0);
$table->timestamps();
});
Schema::create('product_items', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('product_id');
$table->double('price');
$table->string('image');
$table->unsignedInteger('amount');
$table->timestamps();
});
Schema::create('product_groups', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('attribute_id');
$table->unsignedInteger('item_id');
$table->timestamps();
});
Schema::create('attribute_values', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('attribute_id');
$table->string('value');
$table->timestamps();
});
Schema::create('attributes', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
all i need to return the attribute 'color' values like ['red,blue,black'] with each category
seems complicated and need help.

Relationship between Users, Settings, Setting_types Eloquent Laravel 5.4*

How would you go to create a relationship between the folowing tables:
Users table:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('firstname');
$table->string('surename');
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('country')->nullable();
$table->string('postcode')->nullable();
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Setting_type table:
Schema::create('setting_types', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Settings table:
Schema::create('settings', function (Blueprint $table) {
$table->increments('id');
$table->string('value');
$table->timestamps();
});
Setting_type_user table:
Schema::create('setting_type_user', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('type_id')->unsigned();
$table->integer('setting_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('type_id')->references('id')->on('setting_types');
$table->foreign('setting_id')->references('id')->on('settings');
$table->timestamps();
});
This is the result I want to get:
{"id":1,"value":"578943205.jpg","created_at":"2017-07-18 00:00:00","updated_at":null,"pivot":{"setting_id":1,"user_id":1,"type_id":1}}
It depends on many things, but it looks like you want to use many-to-many relationship here.
First, in settings pibot table change it to:
$table->unsignedInteger('user_id');
$table->unsignedInteger('type_id');
It's also a good idea to add foreign key constraints to the table:
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('type_id')->references('id')->on('setting_types');
Then define belongsToMany() relationship in both User and SettingType models. Since you don't follow naming conventions, you have to define foreign keys manually. In the User model:
public function settingTypes()
{
return $this->belongsToMany('App\SettingType', 'settings', 'user_id', 'type_id');
}
And in the SettingType model:
public function users()
{
return $this->belongsToMany('App\User', 'settings', 'type_id', 'user_id');
}

Resources