前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Laravel上传产品图片Uploading img

Laravel上传产品图片Uploading img

作者头像
ytkah
发布2018-08-01 11:04:21
4500
发布2018-08-01 11:04:21
举报
文章被收录于专栏:ytkahytkah

  这节我们讲Laravel产品图片上传,有很多方式可以实现,这里我们用intervention/image插件来进行。首先安装intervention/image插件,在命令行输入

代码语言:javascript
复制
composer require intervention/image

  安装完成后要修改config/app.php文件

代码语言:javascript
复制
//在$providers中添加一行
Intervention\Image\ImageServiceProvider::class,
//在$aliases中添加一行
'Image' => Intervention\Image\Facades\Image::class,

  发布配置,在命令行中输入

代码语言:javascript
复制
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"

  这时弹出Copied File [/vendor/intervention/image/src/config/config.php] To [/config/image.php]提示已经复制配置文件到config/image.php,你可以在这里进行设置。

  修改controller配置,文件在/app/Http/Controllers/ItemController.php,

代码语言:javascript
复制
if($request->hasFile('img'))
        {            
            $image = $request->file('img');         
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $location = public_path('img/' . $filename);
            Image::make($image)->save($location);
            $item->img = url('img/' . $filename);
        }

  修改create.blade.php文件,表格form要加一个参数enctype="multipart/form-data",选择图片改为<input type="file" name="img" >

代码语言:javascript
复制
@extends('layouts.app')

@if ($errors->any())
    <div class="alert alert-danger">
    	<strong>Errors:</strong>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

@section('content')
	<div class="container">
		<div class="row">
			<div class="col-md-8 col-md-offset-2">
				<div class="card card-default">
					<div class="card-header">Create New Item</div>
					<div class="card-body">
						<form method="POST" action="/items" aria-label="Register" enctype="multipart/form-data">
							@csrf
							<div class="form-group row">
								<label for="name" class="col-md-4 col-form-label text-md-right">Name</label>
								<div class="col-md-6">
									<input id="name" type="text" name="name" value="" required="required" autofocus="autofocus" class="form-control">
								</div>
							</div>
							<div class="form-group row">
								<label for="email" class="col-md-4 col-form-label text-md-right">Price</label>
								<div class="col-md-6">
									<input id="email" type="text" name="price" value="" required="required" class="form-control">
								</div>
							</div>
							<div class="form-group row">
								<label for="password" class="col-md-4 col-form-label text-md-right">Img</label>
								<div class="col-md-6">
									<input type="file" name="img" >
								</div>
							</div>
							<div class="form-group row">
								<label for="password-confirm" class="col-md-4 col-form-label text-md-right">Description</label>
								<div class="col-md-6">
									<input id="password-confirm" type="text" name="description" required="required" class="form-control">
								</div>
							</div>
							<div class="form-group row mb-0">
								<div class="col-md-6 offset-md-4">
									<button type="submit" class="btn btn-primary">Save</button>
								</div>
							</div>
						</form>
					</div>
				</div>	
			</div>	
		</div>	
	</div>	
@endsection
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-07-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档