热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

curd操作php代码,Laravel5.6中的CURD操作(代码示例详解)

在本篇文章中,我将给大家分享laravel5.6版本中的基本crud(创建,读取,更新和删除)应用程序模块。你可以按照下面的步骤在lara

在本篇文章中,我将给大家分享laravel 5.6版本中的基本crud(创建,读取,更新和删除)应用程序模块。你可以按照下面的步骤在laravel 5.6中创建CRUD应用程序。

890d2ff40bf3ca6e7057fb4f78f82a51.png

Laravel是一个流行的开源PHP MVC框架,具有许多高级开发功能。如果你是laravel 5.6应用程序中的学习者或初学者,更多地了解或学习crud应用程序总是有很大帮助的。(相关laravel视频教程:《最新laravel商城实战视频教程》)

下面我将创建insert(插入)、update(更新)、delete(删除)和view(查看)和产品的分页示例。你只需创建新产品,查看产品,编辑产品并从列表中删除产品即可。

第1步:安装Laravel 5.6

可以在终端中运行 create-project 命令来安装 Laravel:composer create-project --prefer-dist laravel/laravel blog

第2步:数据库配置

完成安装后,我们将为laravel 5.6的crud应用程序进行数据库配置,例如数据库名称,用户名,密码等。所以,让我们打开.env文件并填写相关信息,如下:

.envDB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

第3步:创建产品表和模型

我们将为产品创建crud应用程序。所以我们必须使用Laravel 5.6 php artisan命令创建产品表的迁移(migrations),首先使用以下命令:php artisan make:migration create_products_table --create=products

在执行此命令之后,你可以在路径database/migrations中找到一个文件,并且必须将以下代码放在migrations文件中以用于创建products表。

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('products', function (Blueprint $table) {

$table->increments('id');

$table->string('name');

$table->text('detail');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('products');

}

}

第4步:添加resource路由

在这个步骤中,我们需要为产品crud应用添加resource路由。所以打开routes / web.php文件并添加以下路由。

routes/web.phpRoute::resource('products','ProductController');

第5步:创建ProductController

现在,我们应该创建一个新的控制器ProductController。因此要运行以下命令并创建新的控制器。下面的控制器用于创建resource控制器。

创建ProductControllerphp artisan make:controller ProductController --resource --model=Product

在下面的命令之后,你将在这个路径app/Http/Controllers/ProductController.php中找到新的文件。

在这个控制器中,默认情况下将创建7个方法如下所示:

1)index()

2)create()

3)store()

4)show()

5)edit()

6)update()

7)destroy()

因此,让我们复制下面的代码并将其放到ProductController.php文件中。

app/Http/Controllers/ProductController.php

namespace App\Http\Controllers;

use App\Product;

use Illuminate\Http\Request;

class ProductController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$products = Product::latest()->paginate(5);

return view('products.index',compact('products'))

->with('i', (request()->input('page', 1) - 1) * 5);

}

/**

* Show the form for creating a new resource.

*

* @return \Illuminate\Http\Response

*/

public function create()

{

return view('products.create');

}

/**

* Store a newly created resource in storage.

*

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{

request()->validate([

'name' => 'required',

'detail' => 'required',

]);

Product::create($request->all());

return redirect()->route('products.index')

->with('success','Product created successfully.');

}

/**

* Display the specified resource.

*

* @param \App\Product $product

* @return \Illuminate\Http\Response

*/

public function show(Product $product)

{

return view('products.show',compact('product'));

}

/**

* Show the form for editing the specified resource.

*

* @param \App\Product $product

* @return \Illuminate\Http\Response

*/

public function edit(Product $product)

{

return view('products.edit',compact('product'));

}

/**

* Update the specified resource in storage.

*

* @param \Illuminate\Http\Request $request

* @param \App\Product $product

* @return \Illuminate\Http\Response

*/

public function update(Request $request, Product $product)

{

request()->validate([

'name' => 'required',

'detail' => 'required',

]);

$product->update($request->all());

return redirect()->route('products.index')

->with('success','Product updated successfully');

}

/**

* Remove the specified resource from storage.

*

* @param \App\Product $product

* @return \Illuminate\Http\Response

*/

public function destroy(Product $product)

{

$product->delete();

return redirect()->route('products.index')

->with('success','Product deleted successfully');

}

}

OK,运行下面命令后,你会找到app/Product.php,并将下面的内容放入Product.php文件中:

app/Product.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'name', 'detail'

];

}

第6步:创建Blade文件

现在我们进入最后一步。在这一步中,我们只需要创建blade文件。所以我们主要需要创建布局文件,然后创建新的文件夹“products”,然后创建crud app的blade文件。最后需要创建以下blade文件:

1) layout.blade.php

2) index.blade.php

3) show.blade.php

4) form.blade.php

5) create.blade.php

6) edit.blade.php

让我们创建下面的文件,并放入下面的代码。

resources/views/products/layout.blade.php

Laravel 5.6 CRUD Application

@yield('content')

resources/views/products/index.blade.php@extends('products.layout')

@section('content')

Laravel 5.6 CRUD Example from scratch

Create New Product

@if ($message = Session::get('success'))

{{ $message }}

@endif

NoNameDetailsAction

@foreach ($products as $product)

{{ ++$i }}{{ $product->name }}{{ $product->detail }}

Show

Edit

@csrf

@method('DELETE')

Delete

@endforeach

{!! $products->links() !!}

@endsection

resources/views/products/show.blade.php@extends('products.layout')

@section('content')

Show Product

Back

Name:

{{ $product->name }}

Details:

{{ $product->detail }}

@endsection

resources/views/products/create.blade.php@extends('products.layout')

@section('content')

Add New Product

Back

@if ($errors->any())

Whoops! There were some problems with your input.

@foreach ($errors->all() as $error)

{{ $error }}

@endforeach

@endif

@csrf

Name:

Detail:

Submit

@endsection

resources/views/products/edit.blade.php@extends('products.layout')

@section('content')

Edit Product

Back

@if ($errors->any())

Whoops! There were some problems with your input.

@foreach ($errors->all() as $error)

{{ $error }}

@endforeach

@endif

@csrf

@method('PUT')

Name:

Detail:

{{ $product->detail }}

Submit

@endsection

现在,我们准备运行我们的crud应用程序的例子,所以运行以下命令快速运行:php artisan serve

最后你就可以在浏览器上打开下面的网址进行查看测试:http://localhost:8000/products

本篇文章就是关于Laravel 5.6中的CURD操作即创建,读取,更新和删除操作,希望对需要的朋友有所帮助!



推荐阅读
  • 本文详细介绍了MySQL表分区的创建、增加和删除方法,包括查看分区数据量和全库数据量的方法。欢迎大家阅读并给予点评。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • WhenIusepythontoapplythepymysqlmoduletoaddafieldtoatableinthemysqldatabase,itdo ... [详细]
  • 本文介绍了如何使用PHP代码将表格导出为UTF8格式的Excel文件。首先,需要连接到数据库并获取表格的列名。然后,设置文件名和文件指针,并将内容写入文件。最后,设置响应头部,将文件作为附件下载。 ... [详细]
  • 本文提供了关于数据库设计的建议和注意事项,包括字段类型选择、命名规则、日期的加入、索引的使用、主键的选择、NULL处理、网络带宽消耗的减少、事务粒度的控制等方面的建议。同时还介绍了使用Window Functions进行数据处理的方法。通过遵循这些建议,可以提高数据库的性能和可维护性。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • 本文详细介绍了MysqlDump和mysqldump进行全库备份的相关知识,包括备份命令的使用方法、my.cnf配置文件的设置、binlog日志的位置指定、增量恢复的方式以及适用于innodb引擎和myisam引擎的备份方法。对于需要进行数据库备份的用户来说,本文提供了一些有价值的参考内容。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 关于我们EMQ是一家全球领先的开源物联网基础设施软件供应商,服务新产业周期的IoT&5G、边缘计算与云计算市场,交付全球领先的开源物联网消息服务器和流处理数据 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 本文介绍了在使用Laravel和sqlsrv连接到SQL Server 2016时,如何在插入查询中使用输出子句,并返回所需的值。同时讨论了使用CreatedOn字段返回最近创建的行的解决方法以及使用Eloquent模型创建后,值正确插入数据库但没有返回uniqueidentifier字段的问题。最后给出了一个示例代码。 ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有