Laravel 11 角色和权限4--文章模块

新建模型文件

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

编写迁移文件

Database/Migrations/2024_12_13_075513_create_articles_table.php

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

执行迁移文件

php artisan migrate

创建文章控制器

go 复制代码
php artisan make:controller ArticleController -r

App/Http/Controllers/ArticleController.php

php 复制代码
<?php

namespace App\Http\Controllers;

use App\Models\Article;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ArticleController extends Controller
{
    /**
     * 文章首页
     */
    public function index()
    {
        $articles = Article::latest()->paginate(25);
        return view('articles.list', compact('articles'));
    }

    /**
     * 新增文章页面
     */
    public function create()
    {
        return view('articles.create');
    }

    /**
     * 执行文章新增
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'title' => 'required|min:20',
            'author' => 'required|min:3',
        ]);

        if ($validator->passes()) {
            $article = new Article();
            $article->title = $request->title;
            $article->text = $request->text;
            $article->author = $request->author;
            $article->save();

            return redirect()->route('articles.index')->with('success', '文章添加成功~~');
        } else {
            return redirect()->route('articles.create')->withErrors($validator)->withInput();
        }
    }

    /**
     * 文章编辑页面
     */
    public function edit(string $id)
    {
        $article = Article::findOrFail($id);
        return view('articles.edit', compact('article'));
    }

    /**
     * 执行文章编辑
     */
    public function update(Request $request, string $id)
    {
        $article = Article::findOrFail($id);

        $validator = Validator::make($request->all(), [
            'title' => 'required|min:20',
            'author' => 'required|min:3',
        ]);

        if ($validator->passes()) {
            $article->title = $request->title;
            $article->text = $request->text;
            $article->author = $request->author;
            $article->save();

            return redirect()->route('articles.index')->with('success', '文章编辑成功~~');
        } else {
            return redirect()->route('articles.edit')->withErrors($validator)->withInput();
        }
    }

    /**
     * 删除
     */
    public function destroy(Request $request)
    {
        $article = Article::find($request->id);

        if ($article == null) {
            session()->flash('error', '未找到权限~');
            return response()->json([
                'status' => false
            ]);
        }

        $article->delete();
        session()->flash('success', '文章删除成功~');
        return response()->json([
            'status' => true
        ]);

    }
}

文章首页

resources/views/articles/list.blade.php

xml 复制代码
<x-app-layout>
    <x-slot name="header">
        <div class="flex justify-between">
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                {{ __('文章列表') }}
            </h2>
            <a href="{{ route('articles.create') }}"
               class="bg-slate-700 text-sm rounded-md text-white px-3 py-2">新增</a>
        </div>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            {{-- 表单成功验证 --}}
            <x-message></x-message>

            <table class="w-full">
                <thead class="bg-gray-50">
                <tr class="border-b">
                    <th class="px-6 py-3 text-left" width="60">#</th>
                    <th class="px-6 py-3 text-left">标题</th>
                    <th class="px-6 py-3 text-left">作者</th>
                    <th class="px-6 py-3 text-left" width="180">创建时间</th>
                    <th class="px-6 py-3 text-center" width="180">操作</th>
                </tr>
                </thead>
                <tbody class="bg-white">

                @if($articles->isNotEmpty())
                    @foreach($articles as $article)
                        <tr class="border-b">
                            <td class="px-6 py-3 text-left">{{$article->id}}</td>
                            <td class="px-6 py-3 text-left">{{$article->title}}</td>
                            <td class="px-6 py-3 text-left">{{$article->author}}</td>
                            <td class="px-6 py-3 text-left">{{$article->created_at}}</td>
                            <td class="px-6 py-3 text-center">
                                <a href="{{ route('articles.edit',$article->id) }}"
                                   class="bg-slate-700 text-sm rounded-md text-white px-3 py-2 hover:bg-slate-600">编辑</a>
                                <a href="javascript:void(0);" onclick="deletePermission({{$article->id}})"
                                   class="bg-red-600 text-sm rounded-md text-white px-3 py-2 hover:bg-red-500">删除</a>
                            </td>
                        </tr>
                    @endforeach
                @endif

                </tbody>
            </table>
            <div class="my-3">
                {{--分页--}}
                {{$articles->links()}}
            </div>
        </div>
    </div>

    <x-slot name="script">
        <script type="text/javascript">
            {{--AJAX文章删除--}}
            function deletePermission(id) {
                if (confirm("您确定要删除么~~")) {
                    $.ajax({
                        url: '{{ route('articles.destroy') }}',
                        type: 'delete',
                        data: {id: id},
                        dataType: 'json',
                        headers: {
                            'x-csrf-token': '{{ csrf_token() }}'
                        },
                        success: function (response) {
                            window.location.href = '{{ route('articles.index') }}';
                        }
                    });
                }
            }
        </script>
    </x-slot>
</x-app-layout>

新增页面

resources/views/articles/list.blade.php

ini 复制代码
<x-app-layout>
    <x-slot name="header">
        <div class="flex justify-between">
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                文章 / 新增
            </h2>
            <a href="{{ route('articles.index') }}"
               class="bg-slate-700 text-sm rounded-md text-white px-5 py-3">权限列表</a>
        </div>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900">

                    <form action="{{ route('articles.store') }}" method="post">
                        @csrf

                        <div>
                            <label for="" class="text-sm font-medium">标题</label>
                            <div class="my-3">
                                <input value="{{old('title')}}" name="title" placeholder="请输入标题" type="text"
                                       class="border-gray-300 shadow-sm w-1/2 rounded-lg">

                                @error('title')
                                <p class="text-red-400 font-medium">{{ $message }}</p>
                                @enderror

                            </div>

                            <label for="" class="text-sm font-medium">内容</label>
                            <div class="my-3">
                                <textarea name="text" placeholder="请输入内容" id="text" class="border-gray-300 shadow-sm w-1/2 rounded-lg"
                                          cols="30" rows="10">{{old('text')}}</textarea>
                            </div>

                            <label for="" class="text-sm font-medium">作者</label>
                            <div class="my-3">
                                <input value="{{old('author')}}" name="author" placeholder="请输入作者" type="text"
                                       class="border-gray-300 shadow-sm w-1/2 rounded-lg">

                                @error('author')
                                <p class="text-red-400 font-medium">{{ $message }}</p>
                                @enderror

                            </div>

                            <button class="bg-slate-700 text-sm rounded-md text-white px-5 py-3">新增</button>
                        </div>
                    </form>

                </div>
            </div>
        </div>
    </div>
</x-app-layout>

编辑页面

resources/views/articles/edit.blade.php

ini 复制代码
<x-app-layout>
    <x-slot name="header">
        <div class="flex justify-between">
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                文章 / 编辑
            </h2>
            <a href="{{ route('articles.index') }}"
               class="bg-slate-700 text-sm rounded-md text-white px-5 py-3">权限列表</a>
        </div>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900">

                    <form action="{{ route('articles.update',$article->id) }}" method="post">
                        @csrf

                        <div>
                            <label for="" class="text-sm font-medium">标题</label>
                            <div class="my-3">
                                <input value="{{old('title',$article->title)}}" name="title" placeholder="请输入标题" type="text"
                                       class="border-gray-300 shadow-sm w-1/2 rounded-lg">

                                @error('title')
                                <p class="text-red-400 font-medium">{{ $message }}</p>
                                @enderror

                            </div>

                            <label for="" class="text-sm font-medium">内容</label>
                            <div class="my-3">
                                <textarea name="text" placeholder="请输入内容" id="text" class="border-gray-300 shadow-sm w-1/2 rounded-lg"
                                          cols="30" rows="10">{{old('text',$article->text)}}</textarea>
                            </div>

                            <label for="" class="text-sm font-medium">作者</label>
                            <div class="my-3">
                                <input value="{{old('author',$article->author)}}" name="author" placeholder="请输入作者" type="text"
                                       class="border-gray-300 shadow-sm w-1/2 rounded-lg">

                                @error('author')
                                <p class="text-red-400 font-medium">{{ $message }}</p>
                                @enderror

                            </div>

                            <button class="bg-slate-700 text-sm rounded-md text-white px-5 py-3">更新</button>
                        </div>
                    </form>

                </div>
            </div>
        </div>
    </div>
</x-app-layout>
相关推荐
YaHuiLiang2 小时前
小微互联网公司与互联网创业公司的技术之殇 - "新"技术的双刃剑
前端·后端·架构
Young丶3 小时前
SpringBoot Maven 项目 pom 中的 plugin 插件用法整理
spring boot·后端·maven
风月歌3 小时前
基于Web的足球青训俱乐部管理后台系统的设计与开发源码(springboot+mysql+vue)
java·前端·spring boot·后端·mysql·mybatis·源码
INFINI Labs3 小时前
Spring Boot 集成 Easysearch 完整指南
spring boot·后端·jenkins·client·easysearch
计算机-秋大田3 小时前
基于Spring Boot的社区老人健康信息管理系统的设计与实现(LW+源码+讲解)
java·spring boot·后端·课程设计
jjw_zyfx3 小时前
django vue3实现大文件分段续传(断点续传)
后端·python·django·vue
SomeB1oody4 小时前
【Rust自学】10.6. 生命周期 Pt.2:生命周期的语法与例子
开发语言·后端·rust
运维@小兵4 小时前
SpringBoot入门之创建一个Hello World项目
java·spring boot·后端
Anarkh_Lee5 小时前
Feed流系统全解析:构建实时、个性化的用户体验
后端·架构
Victor3565 小时前
Tomcat(121)Tomcat的会话超时故障排除
后端