在 Laravel 10 Migration 中添加索引

在本文中,我们将看到如何在 laravel 10 迁移中添加索引。在这里,我们将了解使用迁移的 laravel 10 添加索引。Laravel 模式构建器支持多种类型的索引。让我们讨论 laravel 10 迁移以创建索引。

那么,让我们看看如何在 laravel 10 迁移中添加索引,laravel 10 迁移向现有列添加索引,laravel 10 迁移删除索引,以及如何在 laravel 10 中添加索引。

Laravel 提供了一个 index() 方法,用于使用迁移向列添加索引。

句法:

$table->index('column_name');

例子:

$table->index('state');	

// Multiple columns
$table->index(['account_id', 'created_at']);

示例:简单索引

在这个例子中,我们将创建一个简单的索引。因此,创建一个迁移并向该列添加一个索引。

php artisan make:migration create_posts_table

移民:

<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('description');
            $table->timestamps();
  
            $table->index(['title', 'created_at']);
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

示例:唯一索引

以下示例创建一个新 email 列并指定其值应该是唯一的。

php artisan make:migration create_users_table

或者,您可以在定义列之后创建索引。为此,您应该调用 unique 架构构建器蓝图上的方法。此方法接受应接收唯一索引的列的名称

移民:

<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamps();
  
            $table->unique('email');
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

创建索引时,Laravel 会根据表名、列名和索引类型自动生成索引名,但你可以向该方法传递第二个参数来自己指定索引名

$table->unique('email', 'unique_email');

可用索引类型

Laravel 的模式构建器蓝图类提供了创建 Laravel 支持的每种索引类型的方法。每个索引方法都接受一个可选的第二个参数来指定索引的名称。

命令描述
$table->primary('id');添加主键。
$table->primary(['id', 'parent_id']);添加复合键。
$table->unique('email');添加唯一索引。
$table->index('state');添加索引。
$table->fullText('body');添加全文索引 (MySQL/PostgreSQL)。
$table->fullText('body')->language('english');添加指定语言 (PostgreSQL) 的全文索引。
$table->spatialIndex('location');添加空间索引(SQLite 除外)。

原文出处:https: //websolutionstuff.com/

#laravel #index 

What is GEEK

Buddha Community

在 Laravel 10 Migration 中添加索引
Seamus  Quitzon

Seamus Quitzon

1595205213

How to perform migration rollback in laravel

As we know that laravel migration provides very simple way to create database table structure. We need to create migration file and write table structure then migrate that migration. Sometimes we need to rollback that migration. So here we will discuss about the migration rollback in laravel.

We can run the rollback artisan command to rollback on a particular step. We can execute the following artisan command.

php artisan migrate:rollback --step=1

Every time when we will rollback, we will get the last batch of migration.

**Note: **This rollback command will work on laravel 5.3 or above version. For the version below 5.3, there is no command available for migration rollback in laravel.

We can also use the following command to rollback and re migrate.

php artisan migrate:refresh --step=2

It will rollback and remigrate last two migration.

You can also checkout the article for executing single migration by clicking on the link below.

How to migrate single migration in laravel

#laravel #how to perform rollback migration in laravel #laravel migration rollback #migration refresh in laravel #migration rollback batch in laravel #migration rollback for one specific migration #migration rollback in laravel

Fredy  Larson

Fredy Larson

1595202210

How to migrate single migration in laravel

Sometimes while working on a laravel application, we just need to migrate only single or specific migration. If we run normal migrate command it will migrate all the migrations written in the application. Here i will let you know to migrate single migration in laravel.

Migrations play very important role for aplication’s database structure. We just need to create migrations in the application for each table which you want and whenever we migrate these migrations. New table structure would be created.

For new developers or setting up new environment, we do not need to backup of table structure. We will just need to run the migrations. It will automatically create desired tables in the database connected from the laravel application.

We can run all the migrations using the command below.

php artisan migrate

Now if we want to run only one migration or a specific migration, we will need to define a path parameter and will need to specify the path of that migration file which we want to run like below.

php artisan migrate --path=/database/migrations/my_migration.php

It will migrate only my_migration.php file. Here, you will need to replace file name from your own migration file.

You can also learn how to add column in existing daatbase table through migration by clicking on the below.

#laravel #how to migrate single migration file #laravel migration #laravel single migration #migrate specific migration in laravel

Fredy  Larson

Fredy Larson

1595205933

Laravel create model migration and controller in a single command

In this tutorial, you will learn how to create a controller Or resource controller and model using the command line (CLI). You will also learn how to create a controller and model using one command only. This example tutorial also work with laravel version.

Let’s use the following ways to Create controller, model and migration laravel in one command in laravel:

1. Create model command

Use the php artisan make model for creating a model using the command line (CLI) :

 php artisan make:model Photo

This command is to create the photo model. After the created the model, it looks like this:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
    //
}

2. Create Controller command

Use the php artisan make:controller command for creating a controller using this command line:

 php artisan make:controller PhotoController

This command will create controller named photoController. It looks like as follow:

<?php
  
namespace App\Http\Controllers;
use Illuminate\Http\Request;
  
class PhotoController extends Controller
{
}

3. Create a Resource Controller Command

To create the resource controller in laravel, use the following command:

php artisan make:controller PhotoController --resource

PHP artisan make controller resource command creates a resource controller. It has already created some methods like index, update, edit, destroy, etc. It looks like this:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PhotoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

4. Command For Create Model and Controller

Use the php artisan make:model -mc for creating a controller and model, you can use this command as follow:

 php artisan make:model -mc Photo

This single command has been created as a photo controller and model.

5. Laravel make:model with migration and controller

If you want to create controller and model, so you can execute php artisan make:model -mc for creating a controller and model in command prompt:

 php artisan make:model Product -mcr

This single command has been created as a Product controller and model.

In this tutorial, you have successfully learned how to create a controller and model. Also, learn how to create a model and resource controller using one command.

#laravel #create controller resource and model in a command #laravel artisan command #laravel create model #laravel single command for model and migration #migration and controller

Seamus  Quitzon

Seamus Quitzon

1595201363

Php how to delete multiple rows through checkbox using ajax in laravel

First thing, we will need a table and i am creating products table for this example. So run the following query to create table.

CREATE TABLE `products` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
 `description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
 `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
 `updated_at` datetime DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Next, we will need to insert some dummy records in this table that will be deleted.

INSERT INTO `products` (`name`, `description`) VALUES

('Test product 1', 'Product description example1'),

('Test product 2', 'Product description example2'),

('Test product 3', 'Product description example3'),

('Test product 4', 'Product description example4'),

('Test product 5', 'Product description example5');

Now we are redy to create a model corresponding to this products table. Here we will create Product model. So let’s create a model file Product.php file under app directory and put the code below.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = [
        'name','description'
    ];
}

Step 2: Create Route

Now, in this second step we will create some routes to handle the request for this example. So opeen routes/web.php file and copy the routes as given below.

routes/web.php

Route::get('product', 'ProductController@index');
Route::delete('product/{id}', ['as'=>'product.destroy','uses'=>'ProductController@destroy']);
Route::delete('delete-multiple-product', ['as'=>'product.multiple-delete','uses'=>'ProductController@deleteMultiple']);

#laravel #delete multiple rows in laravel using ajax #laravel ajax delete #laravel ajax multiple checkbox delete #laravel delete multiple rows #laravel delete records using ajax #laravel multiple checkbox delete rows #laravel multiple delete

Adaline  Kulas

Adaline Kulas

1594166040

What are the benefits of cloud migration? Reasons you should migrate

The moving of applications, databases and other business elements from the local server to the cloud server called cloud migration. This article will deal with migration techniques, requirement and the benefits of cloud migration.

In simple terms, moving from local to the public cloud server is called cloud migration. Gartner says 17.5% revenue growth as promised in cloud migration and also has a forecast for 2022 as shown in the following image.

#cloud computing services #cloud migration #all #cloud #cloud migration strategy #enterprise cloud migration strategy #business benefits of cloud migration #key benefits of cloud migration #benefits of cloud migration #types of cloud migration