How to just update a table and add new line with Laravel? - php

in the database table as below;
public function up()
{
Schema::create('current_adresses', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('current_name',50)->nullable();
$table->string('current_surname',50)->nullable();
$table->string('telephone',25)->nullable();
$table->timestamps();
});
}
I want to do as below;
public function up()
{
Schema::create('current_adresses', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('current_name',50)->nullable();
$table->string('current_surname',50)->nullable();
$table->string('gsm',25)->nullable();
$table->string('telephone',25)->nullable();
$table->timestamps();
});
}
how can I update the new column(gsm column) without refreshing(php artisan migrate:refresh)

Add new migration-
public function up()
{
Schema::table('current_adresses', function($table) {
$table->string('gsm',25)->nullable();
});
}
public function down()
{
Schema::table('current_adresses', function($table) {
$table->dropColumn('gsm');
});
}
See this link for better understanding.

Related

Laravel authentication with multiple roles

Hi I'm a beginner at laravel, I have to develop a project for human resources management and I have two roles admin and employee. I followed a tutorial at laracast to build roles and abilities table and everything seems to be working. Now I just installed laravel/ui package and I can see login and register which are working fine with the users I registered in the database. My problem now is that I don't know how to connect things together. How can I check if the logged in user is admin so the admin panel opens. Waiting for your replies. Here is the code;
Error I'm receiving: 419 page expired
This is what I tried but doesn't work
protected function authenticated(Request $request, $user)
{
// to admin dashboard
if(auth()->user()->roles()->name === 'admin') {
return redirect(route('admin'));
}
// to user dashboard
else if(auth()->user()-roles()->name === 'user') {
return redirect(route('home'));
}
abort(404);
}
Routes
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/admin', 'LoginController#authenticated')->name('admin');
users table
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->rememberToken();
$table->timestamps();
});
}
create roles table
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});
Schema::create('abilities', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});
Schema::create('ability_role', function (Blueprint $table) {
$table->primary(['role_id','ability_id']);
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('ability_id');
$table->timestamps();
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
$table->foreign('ability_id')
->references('id')
->on('roles')
->onDelete('cascade');
});
Schema::create('role_user', function (Blueprint $table) {
$table->primary(['user_id','role_id']);
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('role_id');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
});
}
User.php
public function roles()
{
if(is_string($role))
{
$role = Role::whereName($role)->firstOrFail();
}
return $this->belongsToMany(Role::class)->withTimestamps();
}
public function assignRole($role)
{
$this->roles()->sync($role, false);
}
public function abilities($role)
{
return $this->roles->map->abilities->flatten()->pluck('name')->unique();
}
Role.php
class Role extends Model
{
protected $guarded = [];
public function abilities()
{
return $this->belongsToMany(Ability::class)->withTimestamps();
}
public function allowTo($ability)
{
$this->abilities()->sync($ability,false);
}
}
Ability.php
{
protected $guarded = [];
public function roles()
{
if(is_string($ability))
{
$ability = Ability::whereName($ability)->firstOrFail();
}
return $this->belongsToMany(Role::class)->withTimestamps();
}
}
To management roles and permissions I'm using:
"santigarcor/laratrust"
You can learn more here: https://github.com/santigarcor/laratrust
You can use it like this:
use Illuminate\Support\Facades\Auth;
if (Auth::user()->isAbleTo('edit-user')) {}
if (Auth::user()->hasRole('admin')) {}
if (Auth::user()->isA('guide')) {}
if (Auth::user()->isAn('admin')) {}
This package provides a user interface for the santigarcor/laratrust package
"icweb/trusty"
You can learn more here: https://github.com/icweb/laratrust-ui
All tables and data will be auto created.

Laravel: How to solve "SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint"

I am making migrations where there is a particular migration that maps one table with various other tables using a foreign key. and I am getting the following error while running the migration.
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table bookmaps add constraint bookmaps_subject_id_foreign foreign key (subject_id) references subject (id) on delete cascade)
Here is the migration that is creating the error:
public function up()
{
Schema::create('bookmaps', function (Blueprint $table) {
$table->unsignedBigInteger('book_id')->unique();
$table->unsignedBigInteger('subject_id')->nullable();
$table->unsignedBigInteger('grade_id')->nullable();
$table->unsignedBigInteger('author_id')->nullable();
$table->unsignedBigInteger('catagory_id')->nullable();
$table->unsignedBigInteger('language_id')->nullable();
$table->timestamps();
});
Schema::table('bookmaps', function($table) {
$table->foreign('book_id')->references('id')->on('book')->onDelete('cascade');
$table->foreign('subject_id')->references('id')->on('subject')->onDelete('cascade');
$table->foreign('grade_id')->references('id')->on('grade')->onDelete('cascade');
$table->foreign('author_id')->references('id')->on('author')->onDelete('cascade');
$table->foreign('catagory_id')->references('id')->on('catagories')->onDelete('cascade');
$table->foreign('language_id')->references('id')->on('language')->onDelete('cascade');
});
}
Migrations for other related tables are:
public function up()
{
Schema::create('book', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('discription')->nullable();
$table->string('book_file')->nullable();
$table->string('card_image')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->timestamps();
});
Schema::table('book', function($table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
The table created by above migration is only mapped by foreign key rest of the below migrations are not mapped.
public function up()
{
Schema::create('subject', function (Blueprint $table) {
$table->id();
$table->string('subject_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('grade', function (Blueprint $table) {
$table->id();
$table->string('grade_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('author', function (Blueprint $table) {
$table->id();
$table->string('author_name');
$table->string('author_discription')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('grade', function (Blueprint $table) {
$table->id();
$table->string('grade_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('author', function (Blueprint $table) {
$table->id();
$table->string('author_name');
$table->string('author_discription')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('catagories', function (Blueprint $table) {
$table->id();
$table->string('catagories_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('language', function (Blueprint $table) {
$table->id();
$table->string('language_name');
$table->timestamps();
});
}
What could be the problem?
You have to create the subject migration before the bookmarks. When Laravel tries to migrate the bookmarks table, there is a foreign key linked to the subject table that it does not find.

Laravel - get data out from many to many relations

I have a many to many relation
public function products()
{
return $this->belongsToMany(Product::class); // relation between books and categories
}
public function parts()
{
return $this->belongsToMany(Part::class); // relation between books and categories
}
my migrations :
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->string('link')->default('anohe.com');
$table->string('pname')->default('product');
$table->timestamps();
$table->string('description',3000)->nullable();
$table->string('smalldescription',500)->nullable();
});
}
public function up()
{
Schema::create('parts', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
public function up()
{
Schema::create('part_product', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('product_id');
$table->unsignedInteger('part_id');
$table->timestamps();
$table
->foreign('product_id')
->references('id')
->on('products')
->onDelete('cascade');
$table
->foreign('part_id')
->references('id')
->on('parts')
->onDelete('cascade');
});
}
How can I get products that have a special part_id?
If your relations are correct, that should work:
$specialPartIds = [1];
$products = Product::whereHas('parts', function ($query) use ($specialPartIds) {
$query->whereIn('id', $specialPartIds);
})->get();
dd($products->toArray());

Cannot rename date time column with Laravel Migrtions

I want to simply rename my one column although it keeps saying
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'sendReminderCouple48' (SQL: ALTER TABLE sendIntakes CHANGE sendReminderCouple36 sendReminderCouple48 DATETIME DE
FAULT 'NULL')
Currently its sendReminderCouple36 - DATETIME - NULLABLE
All I want to do is rename it to sendReminderCouple48
public function up()
{
Schema::table('sendIntakes', function (Blueprint $table) {
$table->renameColumn('sendReminderCouple36', 'sendReminderCouple48');
});
}
public function down()
{
Schema::table('sendIntakes', function (Blueprint $table) {
$table->renameColumn('sendReminderCouple48', 'sendReminderCouple36');
});
}
Note: I do not want to do any strict changes in my config file.
This is probably related to issue #22050 on the Laravel framework. Are you running MariaDB as your database engine?
Following the advice in the thread, you might try changing the default value on the column before doing the rename call:
Schema::table('products', function (Blueprint $table) {
$table->string('name')->default(null)->change();
});
Schema::table('products', function (Blueprint $table) {
$table->renameColumn('name', 'description');
});
Schema::table('products', function (Blueprint $table) {
$table->string('description')->default(null)->change();
});
I fixed it by changing the type to a text - rename - then back to date time
public function up()
{
Schema::table('sendIntakes', function (Blueprint $table) {
$table->text('sendReminderCouple36')->default(null)->nullable()->change();
});
Schema::table('sendIntakes', function (Blueprint $table) {
$table->renameColumn('sendReminderCouple36', 'sendReminderCouple48');
});
Schema::table('sendIntakes', function (Blueprint $table) {
$table->datetime('sendReminderCouple48')->default(null)->nullable()->change();
});
}
public function down()
{
Schema::table('sendIntakes', function (Blueprint $table) {
$table->text('sendReminderCouple48')->default(null)->nullable()->change();
});
Schema::table('sendIntakes', function (Blueprint $table) {
$table->renameColumn('sendReminderCouple48', 'sendReminderCouple36');
});
Schema::table('sendIntakes', function (Blueprint $table) {
$table->datetime('sendReminderCouple36')->default(null)->nullable()->change();
});
}

Laravel migration foreign keys references to a primary key with two columns?

I am using Laravel 5.3 and MySQL.
How can I add in Laravel foreign keys references to a primary key with two columns?
Below are my migration scripts (under the database/migrations/ directory):
primary key with two columns
public function up()
{
Schema::create('X', function (Blueprint $table) {
$table->integer('a')->unsigned();
$table->integer('b')->unsigned();
$table->primary(['a', 'b']);
$table->timestamps();
});
}
and in another,
public function up()
{
Schema::create('Y', function (Blueprint $table) {
$table->increments('k');
$table->foreign('c')->references(['a', 'b'])->on('X')->onDelete('cascade');
$table->timestamps();
});
}
However, it doesn't work so: how can I achieve that?
Use Schema::table() instead of Schema::create() when adding foreign key constraints to your database.
Below, snippets illustrating the fix:
// File name: 2016_09_28_create_x_table.php
public function up()
{
// Create table X
Schema::create('X', function (Blueprint $table) {
$table->integer('a')->unsigned();
$table->integer('b')->unsigned();
$table->primary(['a', 'b']);
$table->timestamps();
});
}
// File name: 2016_09_28_create_y_with_foreignkey_table.php
public function up()
{
// Create table Y
Schema::create('Y', function (Blueprint $table) {
$table->increments('k');
$table->integer('c')->unsigned();
$table->timestamps();
});
// Add Foreign key
Schema::table('Y', function (Blueprint $table) {
$table->foreign('c')->references('a')->on('X')->onDelete('cascade');
});
}
Remember unsigned() should be applied on the c.
This is the only way that I found to simulate composite keys and FK pointing to composite keys working in Laravel 5.3 - I miss a more compacted solution in Laravel.
Anyway, here is my code
// File name: 2016_09_28_create_x_table.php
public function up()
{
// Create table X
Schema::create('X', function (Blueprint $table) {
$table->increments('j');
$table->integer('a')->unsigned();
$table->integer('b')->unsigned();
$table->unique(['a', 'b']);
$table->timestamps();
});
}
// File name: 2016_09_28_create_y_with_foreignkey_table.php
public function up()
{
// Create table Y
Schema::create('Y', function (Blueprint $table) {
$table->increments('k');
$table->integer('c')->unsigned();
$table->timestamps();
});
// Add Foreign key
Schema::table('Y', function (Blueprint $table) {
$table->foreign('c')->references('j')->on('X')->onDelete('cascade');
});
}
Foreign key should be set for 2 columns. foreign() should get also array
public function up()
{
Schema::create('X', function (Blueprint $table) {
$table->integer('a')->unsigned();
$table->integer('b')->unsigned();
$table->primary(['a', 'b']);
$table->timestamps();
});
}
public function up()
{
Schema::create('Y', function (Blueprint $table) {
$table->increments('k');
$table->integer('a');
$table->integer('b');
$table->foreign(['a', 'b'])->references(['a', 'b'])->on('X')->onDelete('cascade');
$table->timestamps();
});
}`

Categories