在laravel5中上传并显示图像

时间:2018-10-26 03:44:49

标签: laravel-5

我想问的是我的代码中有些奇怪吗?我只是解决不了。 这是我的模型Media.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Media extends Model{
public $primaryKey = 'id_media';
protected $table = 'media';
protected $fillable = ['image','nama_media', 'ukuran_media'];
}?>

这是我的控制器MediaController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Media;
use Image;
class MediaController extends Controller
{
public function index()
{
  $data ['result'] = \App\Media::where('nama_media', 'like', '%Billboard%')->paginate(5);
  return view('media/index')->with($data, compact('media'));
}
public function indexbando()
{
  $data ['result'] = \App\Media::where('nama_media', 'like', '%Bando%')->paginate(5);
  return view('bando/index')->with($data, compact('media'));
}
public function indexjpo()
{
  $data ['result'] = \App\Media::where('nama_media', 'like', '%JPO%')->paginate(5);
  return view('jpo/index')->with($data, compact('media'));
}
public function indexneonbox()
{
  $data ['result'] = \App\Media::where('nama_media', 'like', '%Neonbox%')->paginate(5);
  return view('neonbox/index')->with($data, compact('media'));
}
public function indexurbanfurniture()
{
  $data ['result'] = \App\Media::where('nama_media', 'like', '%Urban furniture%')->paginate(5);
  return view('urbanfurniture/index')->with($data, compact('media'));
}
public function indexurbanstreet()
{
  $data ['result'] = \App\Media::where('nama_media', 'like', '%Urban Street%')->paginate(5);
  return view('urbanstreet/index')->with($data, compact('media'));
}
public function indexvideotron()
{
  $data ['result'] = \App\Media::where('nama_media', 'like', '%Videotron%')->paginate(5);
  return view('videotron/index')->with($data, compact('media'));
}

public function create()
{
  $image = Media::latest()->first();
  return view('media/form',compact('image'));
}
public function store(Request $request)
{
  $originalImage = $request->file('filename');
  $thumbnailImage = Image::make('$originalImage')->getRealPath();
  $thumbnailPath = public_path().'/thumbnail/';
  $originalPath = public_path().'/images/';
  $thumbnailImage->save($originalPath.time().$originalImage->getClientOriginalName());
  $thumbnailImage->resize(150,150);
  $thumbnailImage->save($thumbnailPath.time().$originalImage->getClientOriginalName());
  $imagemodel = new Media();
  $imagemodel->filename=time().$originalImage->getClientOriginalName();
  $imagemodel->save();

  $rules = [
      'image'   =>'required|image|mimes:jpg,jpeg,bmp,png,JPG,JPEG,PNG',
      'nama_media' => 'required|max:100',
      'ukuran_media' => 'required|max:100',
      ];

  $this->validate($request, $rules);
  $input = $request->all();
  $status = \App\Media::create($input);

  if ($status) return redirect('/')->with('success', 'Data berhasil ditambahkan');
  else return redirect('/')->with('error', 'Data gagal ditambahan');
}
public function edit ($id)
{
  $data['result'] = \App\Media::where('id_media',$id)->first();
  return view('media/form')->with($data);
}
public function update(Request $request, $id)
{
  $rules = [
  'nama_media'  => 'required|max:100',
    'ukuran_media'  => 'required|max:100'
  ];

  $this->validate($request,$rules);
  $input = $request->all();
  $result = \App\Media::where('id_media', $id)->first();
  $status = $result->update($input);

  if ($status) return redirect('/')->with('success', 'Data berhasil diubah');
  else return redirect('/')->with('error', 'Data gagal diubah');
}
public function destroy(Request $request, $id)
{
  $result = \App\Media::where('id_media', $id)->first();
  $status = $result->delete();

  if ($status) return redirect('/')->with('success', 'Data berhasil dihapus');
  else return redirect('/')->with('error', 'Data gagal dihapus');
}

}

这是我的表单

@extends('templates/header')
@section('content')
<section class="content-header">
  <h1>
{{ empty($result) ? 'Tambah' : 'Edit' }} Data Media
<small>18communication</small>
</h1>
<ol class="breadcrumb">
<li><a href="{{ url('/') }}"><i class="fa fa-home"></i>Home</a></li>
<li>Data Billboard</li>
<li class="active">{{ empty($result) ? 'Tambah' : 'Edit' }} Data Billboard</li>
</ol>
</section>
<!-- Main Content -->
<section class="content">
@include('templates/feedback')
<!-- Default box -->
<div class="box">
<div class="box-header with-border">
<a href="{{ url('/') }}" class="btn bg-purple"><i class="fa fa-chevron-left"></i> Kembali</a>
</div>
<div class="box-body">
<form action="{{ empty($result) ? url ('media/add') : url ("media/$result->id_media/edit") }} " class="form-horizontal" method="post" >
{{ csrf_field() }}
@if (!empty($result))
{{ method_field('patch') }}
@endif
<div class="form-group">
<label class="control-label col-sm-2">Foto Media</label>
<div class="col-sm-10">
<input type="file" name="image" value="{{ @$result->image}}" enctype="multipart/form-data">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Nama Media</label>
<div class="col-sm-10">
<input type="text" name="nama_media" class="form-control" placeholder="Masukan nama media" value="{{ @$result->nama_media}}">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Ukuran Media</label>
<div class="col-sm-10">
<input type="text" name="ukuran_media" class="form-control" placeholder="Masukan ukuran media" value="{{ @$result->ukuran_media}}">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<button type="submit" class="btn btn-primary"><i class="fa fa-save"> Simpan</i></button>
</div>
</div>
</form>
<!-- /.box-body -->
</div>
<!-- /.box -->
</section>
<!-- /.content -->
@endsection

我不知道我的代码有点含糊。但是您可以尝试提供帮助吗?主要问题是我想在表单中显示广告牌的图像并将其显示在索引上,我已经尝试了很多次来解决它,但似乎并没有帮助错误变得比以前更严重

0 个答案:

没有答案