laravel框架简单的增删改查和文件上传

laravel

最重要的就是路由     下面的是路由的写法

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

//Route::get('/','ForgotPasswordController@__construct');
Route::get('/',function (){
    return view('welcome');
});


Route::any('/price','TestController@index');
Route::any('/price/add','TestController@add');
Route::any('/price/show','TestController@show');
Route::any('/price/one','TestController@one');
Route::get('/price/del/{id}','TestController@del');
Route::get('/price/upd/{id}','TestController@upd');

控制器

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

use App\Http\Model\Price;
class TestController extends Controller{
    //展示
    public function index(){
        return view('price/form');
    }

    //添加入库
    public function add(Request $request){
        $name = Input::get('name');

        //文件上传                  名               文件夹名
        $path = $request->file('file')->store('file','public');

        $arr = ['name'=>$name,'price'=>$path];
        $user  = new Price();
        $user->add($arr);
        return redirect()->action('TestController@show');
    }

    //展示
    public function show(){
        $user  = new Price();
        $res=$user->sel();
        return view('price/show',['arr'=>$res]);
    }

    //删除
    public function del($id){
        $user  = new Price();
        $res=$user->del($id);
        if($res){
            return redirect()->action('TestController@show');
        }
    }


    //一条数据
    public function upd($id){
        $user  = new Price();
        $res=$user->one($id);
        return view('price/upd',['arr'=>$res]);
    }

    //修改
    public function one(Request $request){
        //接值
        $name = $request->name;
        $id= $request->id;
        $user  = new Price();
//        也可以这样接值
//        $name = Input::get('name');
//        $id = Input::get('id');
        $path = $request->file('file')->store('file','public');
        $arr = ['id'=>$id,'name'=>$name,'price'=>$path];
        $user->upd($arr);
        return redirect()->action('TestController@show');
    }
}

 model层的操作

<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class price extends Model
{
    //添加
    public function add($arr){
        DB::table('price')->insert($arr);
    }

    //展示
    public function sel(){
        $res=DB::table('price')->get();
        return $res;
    }

    //删除
    public function del($id){
        $res=DB::table('price')->where('id',$id)->delete();
        return $res;
    }

    //展示一个
    public function one($id){
        $res = DB::table('price')->find($id);
        return $res;
    }

    //修改

    public function upd($arr){
        DB::table('price')->update($arr);
    }
}

写表单

 

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<form action="price/add" method="post" enctype="multipart/form-data">
    {{ csrf_field() }}
    <input type="text" name="name"><br>
    <input type="file" name="file"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

展示页面

 

展示图片用       /storage/{{$v->price}}      

红色线标注出的文件用  命令行:php artisan storage:link 就可以出现了这个文件 然后用 /storage/{{$v->图片的字段}}  就可以出现图片了 

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<table border="1">
    <tr>
        <td>名称</td>
        <td>图片</td>
        <td>操作</td>
    </tr>
    <?php foreach ($arr as $v){?>
    <tr>
        <td>{{$v->name}}</td>
        <td><img src="/storage/{{$v->price}}" style="width: 100px;height: 70px"></td>
        <td>
            <a href="/price/del/{{$v->id}}">删除</a>
            <a href="/price/upd/{{$v->id}}">修改</a>
        </td>
    </tr>
<?php }?>

</table>

</body>
</html>

修改页面

 

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<form action="/price/one" method="post" enctype="multipart/form-data">
    {{ csrf_field() }}
    <input type="text" name="name" value="{{$arr->name}}"><br>
    <input type="file" name="file"><br>
    <img src="/storage/{{$arr->price}}" style="width: 100px;height: 70px"><br>
    <input type="hidden" name="id" value="{{$arr->id}}">
    <input type="submit" value="修改">
</form>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/baiyawen1/article/details/87639435