웹 백엔드 개발 (CRUD)을위한 PHP 개발 프레임 워크 laravel5.5 소규모 프로젝트

  • 목록 항목

1. 장치 모델 생성

php artisan make:model Device

여기에 사진 설명 삽입

    //用户模型关联的表
    public $table="devices";
    //关联表的主键
    public $primaryKey="device_id";
    //禁用模型的时间戳
    public $timestamps =false;

2. 장치 리소스 컨트롤러 만들기

 php artisan make:Controller  DeviceController r  DeviceController --resource

여기에 사진 설명 삽입

web.php에 가입

Route::resource('/device',"DeviceController");

3. 장치 테이블 디자인

여기에 사진 설명 삽입

4. 장치 목록 리소스 컨트롤러

//获取10条
$devices = Device::paginate(10);
return view('device.list')->with('devices',$devices);

5. 장치 목록 페이지

<div class="container">

<h2>设备管理</h2>
<input type="button" onclick="location.href='/user/create'" class="btn btn-success" value="添加设备" />

<table class="table table-striped">
 <thead>
<tr>
<th>用户id</th>
<th>设备id</th>
<th>设备名字</th>
<th>设备类型</th>
<th>设备位置</th>
<th>设备数据</th>
<th>操作</th>
</tr>
 </thead>
 <tbody>
 @foreach($devices as $v)
 <tr>
 <td>{
   
   {$v->user_id}}</td>
  <td>{
   
   {$v->device_id}}</td>
  <td>{
   
   {$v->device_name}}</td>
   <td>{
   
   {$v->device_type}}</td>
    <td>{
   
   {$v->device_location}}</td>
     <td>{
   
   {$v->device_data}}</td>
     <td>
     <input type="button" value="编辑" onclick="location.href='/device/{
     
     {$v->id}}/edit'"/>
     <input type="button" value="删除" onclick="deleteOne({
     
     {$v->id}})" />
     </td> 
 </tr> 
 @endforeach 
 </tbody> 
</table>

{
   
   {$devices->links()}}

</div>
<script type="text/javascript">
function deleteOne(id)
{
     
     
	$.post("user/"+id,{
     
     "id":id,"_token":"{
     
     {csrf_token()}}","_method":"DELETE"},
		function(data){
     
     
			alert(data);
			location.reload();
		},'text');
	}
</script>


스크린 샷 실행

여기에 사진 설명 삽입

6. 장치 추가

기존 사용자 아래에만 장치를 추가 할 수 있으며 사용자가 없으면 추가가 실패합니다.

인터페이스 코드 :

@extends('layouts.app')

@section('content')
<div class="container">

<h2>添加新设备</h2>
<br>
<form action="/device" method="post">
{
   
   {csrf_field()}}
 <div class="form-group">
    <label for="user_id">用户ID</label>
    <input type="text" class="form-control" name="user_id" id="user_id" placeholder="请输入用户ID.....">
  </div>
 
  <div class="form-group">
    <label for="device_id">设备ID</label>
    <input type="text" class="form-control" name="device_id" id="device_id" placeholder="请输入设备ID.....">
  </div>

 <div class="form-group">
    <label for="device_name">设备名称</label>
    <input type="text" class="form-control" name="device_name" id="device_name" placeholder="请输入用户名称....">
  </div>

  <div class="form-group">
    <label for="device_type">设备类型</label>  
    <select  class="form-control"  name="device_type" id="device_type">
        <option value="传感器">传感器</option>
        <option value="继电器">继电器</option>
        <option value="其他">其他</option>                         
    </select>
  </div>
  
  <div class="form-group">
    <label for="username">设备位置</label>
    <input type="text" class="form-control" name="device_location" id="device_location" placeholder="请输入设备位置">
  </div>

  <button type="submit" class="btn btn-default" >提交</button>
</form>

</div>



@endsection

리소스 컨트롤러에서 함수 만들기

public function create()
    {
    
    
        //
        return view('device.create');

    }

제출할 때 저장 기능을 호출하십시오.

 //新建一个设备和用户
        $device = new Device();
        $user = new User();
        if($request->user_id!=null&&$request->device_id!=null&&$request->device_name!=null
           &&$request->device_type!=null&&$request->device_location!=null){
    
    
           //查找user表中是否存在该用户,存在即可添加设备,不存在不运行添加
            $user = User::find($request->user_id);
            if($user!=null)
            {
    
    
                $device->user_id=$request->user_id;
                $device->device_id=$request->device_id;
                $device->device_name=$request->device_name;
                $device->device_type=$request->device_type;
                $device->device_location=$request->device_location;
                //将该设备信息保存
                 $device->save();
                 //重定向至“/user路由”
                 return redirect("/device");
            }
            else{
    
    
                //
                return "用户不存在!";
            }
            

           }
        else{
    
    
           
            return "请检查表单是否填写完整!" ;
        }



        
    }

여기에 사진 설명 삽입

7. 장치 제거

인터페이스 코드 :

<input type="button" value="删除" onclick="deleteOne({
   
   {$v->device_id}})" />
<script type="text/javascript">
function deleteOne(id)
{
	$.post("device/"+id,{"id":id,"_token":"{
   
   {csrf_token()}}","_method":"DELETE"},
		function(data){
			alert(data);
			location.reload();
		},'text');
	}
</script>

장치 리소스 컨트롤러의 파괴 기능에서

     public function destroy($id)
    {
    
    
        $device=Device::find($id);
        $device->delete();
        return "Done";
    }

상호 작용

[외부 링크 이미지 전송에 실패했습니다. 소스 사이트에 핫 링크 방지 메커니즘이있을 수 있습니다. 이미지를 저장하고 직접 업로드하는 것이 좋습니다 (img-eWhIja0e-1607841855569) (C : \ Users \ jiangdada \ AppData \ Roaming \ Typora \ typora-user-images \ image-20201212135603574.png)]

8. 정보보기

장치 ID를 클릭하면 점프 경로

<td><a href="/device/{
     
     {$v->device_id}}">{
   
   {$v->device_id}}</a></td>

리소스 컨트롤러의 show 기능은 상세보기로 돌아가 매개 변수를 전달합니다.

public function show($id)
{
        $device=Device::find($id);
        return view('device.show')->with('device',$device);
}

자세히보기보기

@extends('layouts.app')
@section('content')
<div class="container">
<h2>设备详情</h2>
<p>用户备位置:{
    
    {
    
    $device->device_location}}</p>
</div>
@endsection

인터페이스는 다음과 같습니다.
1

9. 장치 편집

장치 편집을 클릭하여 장치의 이름과 위치를 편집합니다.

 <input type="button" value="编辑" onclick="location.href='/device/{
     
     {$v->device_id}}/edit'"/>

리소스 컨트롤러의 편집 기능이 편집보기로 이동합니다.

    public function edit($id)
    {
    
    
        return view('device.edit')->with('id',$id);
    }

인터페이스보기 편집

@extends('layouts.app')

@section('content')

<h2>编辑设备信息</h2>
<form action="/device/{
   
   {$id}}" method="post">
{
   
   {csrf_field()}}
{
   
   {method_field('PUT')}}
<div class="form-group">
    <label for="name">设备名称</label>
    <input type="text" class="form-control" id="device_name" name="device_name" placeholder="请输入设备名称">
  </div>
    <div class="form-group">
    <label for="location">设备位置</label>
    <input type="text" class="form-control" id="device_location" name="device_location" placeholder="请输入设备位置">
  </div>
  <input type="submit" value="提交"/>
</form>
@endsection

인터페이스 편집

여기에 사진 설명 삽입

리소스 컨트롤러의 업데이트 기능

    public function update(Request $request, $id)
    {
    
    
        //
        $device = Device::find($id);
        $device->device_name=$request->device_name;
        $device->device_location=$request->device_location;
        $device->save();
        return redirect("/device");
    }

추천

출처blog.csdn.net/JIANGYINGH/article/details/111115665