Laravel(CURD)

下图为我自己的路由配置及控制器,模型目录
这个是路由的方法

这是其中的一部分目录

以下为控制器代码(因为个人比较懒,所以把新增,修改写在了一起)
其中的UserInfo为模型

<?php
namespace App\Http\Controllers;
use App\UserInfo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class UserController extends Controller{

	public function getInfo(){

		$re = UserInfo::SHowUserInfo();
     
		return view('User/read',['info'=>$re]);

	}

	public function delete(){

		$id = Input::get('id');

		$re = UserInfo::DeleteInfo($id);

		if($re){

			echo "<script>alert('删除成功');location.href='info';</script>";
		}
	}

	public function create(){

		return view('User/create');

	}

	/*新增,修改*/
	public function edit(){

		if(Request()->isMethod('get')){//如果获取ID的方式为get,则为修改

			if(!Input::has('id')){

				return view('User/create');

			}else{

				$re = UserInfo::edits('student',Input::get('id'));
		
				return view('User/create',['res'=>$re]);
			}

		}elseif(Request()->isMethod('post')){

			if(!Input::has('id')){//如果id不存在进行新增

				$hobby = implode(',',Input::post('hobby'));

				$array = array('sname'=>Input::post('sname'),'sex'=>Input::post('sex'),'hobby'=>$hobby,'address'=>Input::post('address'));

				$re = UserInfo::InsertInfo('student',$array);

				if($re){

					echo "<script>alert('新增成功');location.href='info';</script>";
				}

			}else{//否则进行修改

				$hobby = implode(',',Input::post('hobby'));

				$array = array('sname'=>Input::post('sname'),'sex'=>Input::post('sex'),'hobby'=>$hobby,'address'=>Input::post('address'));

				$re = UserInfo::updates('student',$array,Input::post('id'));

				if($re){

					echo "<script>alert('修改成功');location.href='info';</script>";
				}

			}

		}

	}
}

以下为模型代码(初学laravel,使用的是DB)

<?php
namespace App;
use Illuminate\Support\Facades\DB;

class UserInfo{

	public static function ShowUserInfo(){

		//$re = DB::select("select * from student");
		$re = DB::table('student')->get();

		return $re;

	}

	public static function DeleteInfo($id){

		$re = DB::table('student')->where('sid','=',$id)->delete();

		return $re;
	}

	public static function InsertInfo($table,$array){

		$key="";
        $value="";
        foreach($array as $k => $v){
            $value.="'$v',";
            $key.=$k.',';
        }
        $key = rtrim($key,',');
        $value = rtrim($value,',');
        $sql = "insert into $table($key) values($value)";
        $data = DB::insert($sql);
        return $data;

	}

	public static function edits($table,$id){

		$re = DB::table($table)->where('sid','=',$id)->first();

		return $re;

	}

	public static function updates($table,$array,$id){

		$value="";
        foreach($array as $k => $v){
            $value.="$k='$v',";
        }
        $value = rtrim($value,',');
        $sql = "update $table set $value where sid = $id";
        $data = DB::update($sql);
        return $data;

	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值