laravel开发新模块案例ChatGPT

  • laravel开发新模块案例
  • ChatGPT

    2024年06月03日 10时13分Token: 1528

    好的,以下是一个关于如何在Laravel应用中开发一个新模块的案例。这个示例将演示如何创建一个简单的博客模块,包括创建与管理文章的基本功能。

    首先,确保你已经安装Laravel

    复制代码
    composer create-project --prefer-dist laravel/laravel blogApp
    cd blogApp

    1. 创建迁移文件和模型

    我们需要一个数据库表来存储文章。使用Laravel的artisan命令来生成迁移和模型。

    复制代码
    php artisan make:model Article -m

    这个命令会创建app/Models/Article.php模型和database/migrations/xxxx_xx_xx_create_articles_table.php迁移文件。

    2. 定义数据库迁移

    打开database/migrations/xxxx_xx_xx_create_articles_table.php文件,并定义表结构:

    php 复制代码
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('content');
            $table->timestamps();
        });
    }

    然后运行迁移:

    复制代码
    php artisan migrate

    3. 设置模型

    编辑app/Models/Article.php文件,为文章模型添加一些必要配置:

    php 复制代码
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    
    class Article extends Model
    {
        use HasFactory;
    
        protected $fillable = ['title', 'content'];
    }

    4. 创建控制器

    创建一个控制器来处理文章的基本操作:

    复制代码
    php artisan make:controller ArticleController

    app/Http/Controllers/ArticleController.php中添加以下内容:

    php 复制代码
    namespace App\Http\Controllers;
    
    use App\Models\Article;
    use Illuminate\Http\Request;
    
    class ArticleController extends Controller
    {
        public function index()
        {
            $articles = Article::all();
            return view('articles.index', compact('articles'));
        }
    
        public function create()
        {
            return view('articles.create');
        }
    
        public function store(Request $request)
        {
            $validated = $request->validate([
                'title' => 'required|string|max:255',
                'content' => 'required|string',
            ]);
    
            Article::create($validated);
    
            return redirect()->route('articles.index');
        }
    
        public function show(Article $article)
        {
            return view('articles.show', compact('article'));
        }
    
        public function edit(Article $article)
        {
            return view('articles.edit', compact('article'));
        }
    
        public function update(Request $request, Article $article)
        {
            $validated = $request->validate([
                'title' => 'required|string|max:255',
                'content' => 'required|string',
            ]);
    
            $article->update($validated);
    
            return redirect()->route('articles.index');
        }
    
        public function destroy(Article $article)
        {
            $article->delete();
    
            return redirect()->route('articles.index');
        }
    }

    5. 定义路由

    routes/web.php中定义路由:

    php 复制代码
    use App\Http\Controllers\ArticleController;
    
    Route::resource('articles', ArticleController::class);

    6. 创建视图

    resources/views目录下,创建一个articles文件夹,并添加以下视图文件。

    index.blade.php
    php 复制代码
    @extends('layouts.app')
    
    @section('content')
        <h1>Articles</h1>
        <a href="{{ route('articles.create') }}">Create New Article</a>
        @foreach ($articles as $article)
            <div>
                <h2><a href="{{ route('articles.show', $article) }}">{{ $article->title }}</a></h2>
                <p>{{ Illuminate\Support\Str::limit($article->content, 100) }}</p>
                <a href="{{ route('articles.edit', $article) }}">Edit</a>
                <form action="{{ route('articles.destroy', $article) }}" method="POST" style="display: inline;">
                    @csrf
                    @method('DELETE')
                    <button type="submit">Delete</button>
                </form>
            </div>
        @endforeach
    @endsection
    create.blade.php
    php 复制代码
    @extends('layouts.app')
    
    @section('content')
        <h1>Create New Article</h1>
        <form action="{{ route('articles.store') }}" method="POST">
            @csrf
            <div>
                <label for="title">Title:</label>
                <input type="text" name="title" id="title" value="{{ old('title') }}">
                @error('title')
                    <div>{{ $message }}</div>
                @enderror
            </div>
            <div>
                <label for="content">Content:</label>
                <textarea name="content" id="content">{{ old('content') }}</textarea>
                @error('content')
                    <div>{{ $message }}</div>
                @enderror
            </div>
            <button type="submit">Create</button>
        </form>
    @endsection
    show.blade.php
    php 复制代码
    @extends('layouts.app')
    
    @section('content')
        <h1>{{ $article->title }}</h1>
        <p>{{ $article->content }}</p>
        <a href="{{ route('articles.index') }}">Back to Articles</a>
    @endsection
    edit.blade.php
    php 复制代码
    @extends('layouts.app')
    
    @section('content')
        <h1>Edit Article</h1>
        <form action="{{ route('articles.update', $article) }}" method="POST">
            @csrf
            @method('PUT')
            <div>
                <label for="title">Title:</label>
                <input type="text" name="title" id="title" value="{{ old('title', $article->title) }}">
                @error('title')
                    <div>{{ $message }}</div>
                @enderror
            </div>
            <div>
                <label for="content">Content:</label>
                <textarea name="content" id="content">{{ old('content', $article->content) }}</textarea>
                @error('content')
                    <div>{{ $message }}</div>
                @enderror
            </div>
            <button type="submit">Update</button>
        </form>
    @endsection

    7. 运行应用

    使用以下命令启动开发服务器:

    复制代码
    php artisan serve

    访问http://localhost:8000/articles,你应该能够看到文章的列表页面并可以进行创建、查看、编辑和删除操作。

    以上就是一个简单的Laravel博客模块开发案例。希望这能帮助你理解如何在Laravel中创建新的模块。

相关推荐
tg-zm8899963 天前
2025返利商城源码/挂机自动收益可二开多语言/自定义返利比例/三级分销理财商城
java·mysql·php·laravel·1024程序员节
luminaryhero4 天前
Laravel学习-01预备阶段-05 命令行
laravel
luminaryhero4 天前
Laravel学习-01预备阶段-02 Composer
laravel
xmode4 天前
常用自定义函数laravel版+thinkphp版
后端·php·laravel·thinkphp
JienDa5 天前
JienDa聊PHP:Laravel驱动的企业级图床系统架构设计与实战
系统架构·php·laravel
橘式不妙5 天前
解决使用IDE开发laravel项目无法智能提示eloquent的可调用方法的问题
php·laravel
依了个旧8 天前
🚀 告别手写注释!Laravel 自定义命令创建模型时自动生成 @property 属性提示
laravel
JaguarJack10 天前
Laravel 乐观锁:高并发场景下的性能优化利器
后端·php·laravel
life码农12 天前
在 Laravel框架 Blade 模板中显示原始的 {{ }} 符号的几种方法
php·laravel
JienDa20 天前
Laravel 11与UniApp实战:构建高性能电商API与移动端交互系统
laravel