关闭

laravel框架里简单的操作

标签: 框架
1088人阅读 评论(1) 收藏 举报

!!!配置域名,分页才可以使用

控制器层:

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\controller;
use Illuminate\Support\Facades\Redirect;
use DB;
use paginate;
use Input;
header("content-type:text/html;charset=utf-8");
class TestController extends Controller {

    /*
    |--------------------------------------------------------------------------
    | Test Controller
    |--------------------------------------------------------------------------
    |
    | This controller renders the "marketing page" for the application and
    | is configured to only allow guests. Like most of the other sample
    | controllers, you are free to modify or remove it as you desire.
    |
    */

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Show the application welcome screen to the user.
     *
     * @ 展示表单
     */
    public function index()
    {
       // echo "sadasd";die;
        return view('test');
    }

    /**
     * @执行添加
     */
    public function add(){
        $name=Input::get('name');
        $sex=Input::get('sex');
        $age=Input::get('age');
        $content=Input::get('content');
        //echo $name.$sex.$age.$content;
        //文件上传
        $file = Input::file('myfile');
        //var_dump($file);exit;
        if($file -> isValid()){
            //检验一下上传的文件是否有效.
            $clientName = $file -> getClientOriginalName();//初始名
            $tmpName = $file ->getFileName();//获取tmp文件下的文件名
            $realPath = $file -> getRealPath();//缓存在tmp文件夹下的文件的绝对路径
            $entension = $file -> getClientOriginalExtension();//上传后的文件后缀
            $newName = md5(date('ymdhis').$clientName).".".$entension;//文件上传的名
            $path = $file -> move('uploads',$newName);
            //print_r($path);exit;
        }
        //执行添加
        $add=DB::table('laravel')->insert(
            [
                'l_name' => $name,
                'l_sex' => $sex,
                'l_age' => $age,
                'l_content' => $content,
                'l_file'=>$newName
            ]
        );
        //判断
        if($add){
            echo "success";
            return Redirect()->action('TestController@lists');
        }
    }
    /**
     * 列表展示
     */
    public function lists(){
        $list = DB::table('laravel')->paginate(2);
        //print_r($list);exit;
        return view('list',array('list'=>$list));
    }
    /**
     * 删除
     */
    public function del(){
        $id=Input::get('id');
        //echo $id;
        $result=DB::table('laravel')->where('l_id', '=', $id)->delete();
        //print_r($result);
        if($result){
            echo "删除成功";
            return Redirect()->action('TestController@lists');
        }
    }
}

视图层的表单

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
</head>
<body>
<center>
    <h2><font color="red">表单展示</font></h2>
    <a href="{{URL('lists')}}">展示列表</a>
<form action="{{ URL('add') }}" method="post" enctype="multipart/form-data">
    <input name="_token" type="hidden" value="{{csrf_token()}}">
    <table>
        <tr>
            <td>姓名</td>
            <td><input type="text" name="name"/></td>
        </tr>
        <tr>
            <td>性别:</td>
            <td>
                <input type="radio" name="sex" value="男"/>男
                <input type="radio" name="sex" value="女"/>女
            </td>
        </tr>
        <tr>
            <td>年龄:</td>
            <td>
                <select name="age" id="">
                    <option value="20">20</option>
                    <option value="21">21</option>
                    <option value="22">22</option>
                    <option value="23">23</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>简介</td>
            <td><textarea name="content" id="" cols="30" rows="10"></textarea></td>
        </tr>
        <tr>
            <td>头像:</td>
            <td><input type="file" name="myfile"/></td>
        </tr>
        <tr>
            <td><input type="reset" value="重置"/></td>
            <td><input type="submit" value="提交"/></td>
        </tr>
    </table>
</form>
</center>
</body>
</html>

视图层的列表展示

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
</head>
<body>
<center>
    <h2><font color="blue">列表展示</font></h2>
    <a href="{{URL('test')}}">返回添加</a>
<table border="1">
    <tr>
        <td>编号</td>
        <td>姓名</td>
        <td>性别</td>
        <td>年龄</td>
        <td>用户简介</td>
        <td>用户头像</td>
        <td>操作</td>
    </tr>
    @foreach($list as $v)
    <tr>
        <td>{{$v->l_id}}</td>
        <td>{{$v->l_name}}</td>
        <td>{{$v->l_sex}}</td>
        <td>{{$v->l_age}}</td>
        <td>{{$v->l_content}}</td>
        <td><img src="../uploads/{{$v->l_file}}" alt="" width="100" height="100"/></td>
        <td><a href="{{URL('del?id='.$v->l_id)}}">删除</a></td>
    </tr>
    @endforeach
    <table>
        <tr>
            <!--{!! $list->render() !!}-->
            <?php echo $list->render(); ?>
        </tr>
    </table>
</table>
</center>
</body>
</html>


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1771次
    • 积分:56
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论