Route::resource('articles', 'ArticlesController');
Route::get('/articles', 'ArticlesController@index')->name('articles.index');
Route::get('/articles/{id}', 'ArticlesController@show')->name('articles.show');
Route::get('/articles/create', 'ArticlesController@create')->name('articles.create');
Route::post('/articles', 'ArticlesController@store')->name('articles.store');
Route::get('/articles/{id}/edit', 'ArticlesController@edit')->name('articles.edit');
Route::patch('/articles/{id}', 'ArticlesController@update')->name('articles.update');
Route::delete('/articles/{id}', 'ArticlesController@destroy')->name('articles.destroy');
php artisan make:controller ArticlesController
@extends('layouts.art')
@section('content')
<form class="form-horizontal" method="post" action="{{route('blog.store')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">标题</label>
<div class="col-sm-8">
<input type="title" class="form-control" id="title" name="title">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">内容</label>
<div class="col-sm-8">
<textarea class="form-control" rows="5" id="content" name="content"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">创建</button>
</div>
</div>
</form>
@endsection
php artisan make:migration create_articles_talbe --create=articles
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->longText('content');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('articles');
}
php artisan migrate
php artisan make:model Article
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
//对应的表
protected $table = 'articles';
//通过model可以写入的字段
protected $fillable = [
'title', 'content',
];
}
public function store(Request $request)
{
//数据验证 错误处理
$this->validate($request,[
'title'=>'required|max:50',
'content'=>'required|max:500',
]);
// 1 orm方式写入
$article = Article::create([
'title'=>$request->title,
'content'=>$request->content,
]);
//2 或者
/* $article = new Article();
$article->title =$request->title;
$article->content = $request->content;
$article->save();*/
//3 db方式写入
//insert()方法返回值为true 和 false
//$res = DB::table('articles')->insert(['title'=>$request->title,'content'=>$request->content]);
return redirect()->route('blog.index');
}
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
use App\Article;
public function index()
{
$articles = Article::orderBy('created_at','asc')->get();
return view('articles.index', ['articles'=>$articles]);
}
@extends('layouts.art')
@section('content')
<a class="btn btn-primary" href="{{route('blog.create')}}" rel="external nofollow" >添加文章</a>
@foreach($articles as $article)
<div class="panel panel-default">
<div class="panel-body">
{{$article->title}}
<a href="{{route('blog.show',$article->id)}}" rel="external nofollow" class="btn btn-info">阅读</a>
<a href="{{route('blog.edit', $article->id)}}" rel="external nofollow" class="btn btn-info">修改</a>
<form action="{{ route('blog.destroy', $article->id) }}" method="post" style="display: inline-block;">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger">删除</button>
</form>
</div>
</div>
@endforeach
{!! $articles->render() !!}
@endsection
@extends('layouts.art')
@section('content')
<form class="form-horizontal" method="post" action="{{route('blog.update',$article->id)}}">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">标题</label>
<div class="col-sm-10">
<input type="title" class="form-control" id="title" name="title" value="{{ $article->title }}">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">内容</label>
<div class="col-sm-10">
<textarea class="form-control" rows="5" id="content" name="content"> {{ $article->content }}</textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">修改</button>
</div>
</div>
</form>
@endsection
//展示修改模板
public function edit($id)
{
$article = Article::findOrFail($id);
return view('art.edit',['article'=>$article]);
}
//执行修改
public function update(Request $request, $id)
{
$this->validate($request, [
'title' => 'required|max:50',
'content'=>'required|max:500',
]);
$article = Article::findOrFail($id);
$article->update([
'title' => $request->title,
'content' => $request->content,
]);
return redirect()->route('blog.index');
}
<form action="{{ route('blog.destroy', $article->id) }}" method="post" style="display: inline-block;">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger">删除</button>
</form>
public function destroy($id)
{
$article = Article::findOrFail($id);
$article->delete();
return back();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有