• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
钗的php生涯
博客园    首页    新随笔    联系   管理    订阅  订阅
laravel 实现增 与查

//调用模型层

<?php
namespace App;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class teach extends Model
{
//数据表
    public $table='teach';
//定义主键
   public $primarkey='id';
//时间戳
    public $timestamps=true;
//白名单
    protected $fillable=['updated_at','created_at','name','sex','tel','id'];
}

 

 

 

<?php
namespace App\Http\Controllers;
use App\School;
use App\teach;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class TeachController extends Controller
{

     //首页
   public function index()
     {
         return view('teachers.teach');

     }

}

teach.blade.php:

@extends('layouts.app')

@section('content')
<a href="{{ route('teach.create') }}" style="padding:5px;border:1px dashed gray;">
+ New Teacher
</a>
@endsection

 

//实现添加

 

public function create()
{
    return view('teachers.into');
}

into.blade.php:

@extends('layouts.app')

@section('content')
<form action="{{ route('teach.store') }}" method="post">
{{ csrf_field() }}
{{--//防跨域攻击--}}
<label>姓名:</label>
<input type="text" name="name" style="width:100%;" value="{{ old('name') }}">
<label>性别:</label>
<input name="sex" rows="10" style="width:100%;">{{ old('sex') }}
<label>电话:</label>
<input name="tel" rows="10" style="width:100%;">{{ old('tel') }}
<button type="submit">OK</button>
</form>

@endsection


//接收数据 入库
public function store(Request $request){
     $teach=new Teach();
     $teach->name =$request->name;
     $teach->sex = $request->sex;
     $teach->tel = $request->tel;
     if( $teach->save()){
            return $this->into();
     }

}

//分页展示数据

public function into(){
     $users = DB::table('teach')->paginate(3);
      return view('teachers.index', ['users' => $users]);
}

 

index.blade.php:

 

@extends('layouts.app')

 

@section('content')
<a href="{{ route('teach.create') }}" style="padding:5px;border:1px dashed gray;">
+ New Teacher
</a>

 

@foreach ($users as $teacher)
    <div style="border:1px solid gray;margin-top:20px;padding:20px;">
     <h2>{{ $teacher->name }}</h2>
     <p>{{ $teacher->sex }}</p>
    <p>{{ $teacher->tel }}</p>
     <p>{{ $teacher->created_at }}</p>
     <p>{{ $teacher->updated_at }}</p>
      <a href="{{ route('teach.edit', $teacher->id ) }}">Edit</a>
<form action="{{ route('teach.destroy', $teacher->id) }}" method="post" style="display: inline-block;">
{{ csrf_field() }}
{{ method_field('DELETE') }}
     <button type="submit" style="color: #F08080;background-color: transparent;border: none;">Delete</button>
</form>

 

</div>
   @endforeach
@endsection

 

posted on 2019-03-14 18:42  php钗  阅读(153)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3