Error setting multiple auth - undefined constant - laravel

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

Related

eloquent relation shows null

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');
}

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.

laravel db migrate getting error

When I run php artisan migrate, I am getting an error like this:
In 2017_12_26_045926_create_table_articles.php line 41:
Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting ',' or
')'
This is my articles tables:
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(false);
$table->boolean('is_active')->default(false);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
Schema::table('articles', function($table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
I am adding foreign key for articles and comments, but the articles tables when migrate is giving errors like above. What's wrong?
The error is because you are using the schema class again which is missing the closing tag ")};" and there is no need to use Schema class again you can use the same object to add a foreign key to the table.
Try the below code :
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(false);
$table->boolean('is_active')->default(false);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
OR
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(false);
$table->boolean('is_active')->default(false);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
Schema::table('articles', function($table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
}); //closing Schema class tag
}); //closing Schema class tag
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
You dont need to alter table when you are creating the table to put foreigns.
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned(); // Or $table->unsignedInteger('user_id');
$table->integer('category_id')->unsigned();
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(0);
$table->boolean('is_active')->default(0);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
However, you can do that correctly with this code
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('category_id')->unsigned();
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(0);
$table->boolean('is_active')->default(0);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
});
Schema::table('articles', function($table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
You're missing the closure on Schema::create
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(false);
$table->boolean('is_active')->default(false);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
});

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