Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use App\Image;
  7.  
  8. class ImageController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index()
  16. {
  17. $images = Image::get();
  18. return view('images.index',compact('images'));
  19. }
  20. public function store(Request $request)
  21. {
  22. $this->validate($request, [
  23. 'title' => 'required',
  24. 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
  25. ]);
  26.  
  27.  
  28. $input['image'] = time().'.'.$request->image->getClientOriginalExtension();
  29. $request->image->move(public_path('images'), $input['image']);
  30.  
  31.  
  32. $input['title'] = $request->title;
  33. Image::create($input);
  34.  
  35. return back()
  36. ->with('success','Image Uploaded successfully.');
  37. }
  38. public function upload(Request $request)
  39. {
  40. $this->validate($request, [
  41. 'title' => 'required',
  42. 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
  43. ]);
  44.  
  45.  
  46. $input['image'] = time().'.'.$request->image->getClientOriginalExtension();
  47. $request->image->move(public_path('images'), $input['image']);
  48.  
  49.  
  50. $input['title'] = $request->title;
  51. Image::create($input);
  52.  
  53. return back()
  54. ->with('success','Image Uploaded successfully.');
  55. }
  56. public function destroy($id)
  57. {
  58. Image::find($id)->delete();
  59. return back()->with('success','Image removed successfully.');
  60. }
  61. }
  62. -----------------------------------------
  63.  
  64.  
  65. <!DOCTYPE html>
  66. <html>
  67. <head>
  68. <title>Image Gallery Example</title>
  69. <!-- Latest compiled and minified CSS -->
  70. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  71. <!-- References: https://github.com/fancyapps/fancyBox -->
  72.  
  73.  
  74. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css" media="screen">
  75. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  76. <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js"></script>
  77.  
  78.  
  79. <style type="text/css">
  80. .gallery
  81. {
  82. display: inline-block;
  83. margin-top: 20px;
  84. }
  85. .close-icon{
  86. border-radius: 50%;
  87. position: absolute;
  88. right: 5px;
  89. top: -10px;
  90. padding: 5px 8px;
  91. }
  92. .form-image-upload{
  93. background: #e8e8e8 none repeat scroll 0 0;
  94. padding: 15px;
  95. }
  96. </style>
  97. </head>
  98. <body>
  99.  
  100.  
  101. <div class="container">
  102.  
  103.  
  104. <h3>Laravel - Image Gallery CRUD Example toot</h3>
  105. <form action="{{ route('imagess.store') }}" class="form-image-upload" method="POST" enctype="multipart/form-data">
  106.  
  107.  
  108. {!! csrf_field() !!}
  109.  
  110.  
  111. @if (count($errors) > 0)
  112. <div class="alert alert-danger">
  113. <strong>Whoops!</strong> There were some problems with your input.<br><br>
  114. <ul>
  115. @foreach ($errors->all() as $error)
  116. <li>{{ $error }}</li>
  117. @endforeach
  118. </ul>
  119. </div>
  120. @endif
  121.  
  122.  
  123. @if ($message = Session::get('success'))
  124. <div class="alert alert-success alert-block">
  125. <button type="button" class="close" data-dismiss="alert">×</button>
  126. <strong>{{ $message }}</strong>
  127. </div>
  128. @endif
  129.  
  130.  
  131. <div class="row">
  132. <div class="col-md-5">
  133. <strong>Title:</strong>
  134. <input type="text" name="title" class="form-control" placeholder="Title">
  135. </div>
  136. <div class="col-md-5">
  137. <strong>Image:</strong>
  138. <input type="file" name="image" class="form-control">
  139. </div>
  140. <div class="col-md-2">
  141. <br/>
  142. <button type="submit" class="btn btn-success">Upload</button>
  143. </div>
  144. </div>
  145.  
  146.  
  147. </form>
  148.  
  149.  
  150. <div class="row">
  151. <div class='list-group gallery'>
  152.  
  153.  
  154. @if($images->count())
  155. @foreach($images as $image)
  156. <div class='col-sm-4 col-xs-6 col-md-3 col-lg-3'>
  157. <a class="thumbnail fancybox" rel="ligthbox" href="/images/{{ $image->image }}">
  158. <img class="img-responsive" alt="" src="/images/{{ $image->image }}" />
  159. <div class='text-center'>
  160. <small class='text-muted'>{{ $image->title }}</small>
  161. </div> <!-- text-center / end -->
  162. </a>
  163. <form action="{{ route('imagess.destroy', $image->id)}}" method="POST">
  164. <input type="hidden" name="_method" value="delete">
  165. {!! csrf_field() !!}
  166. <button type="submit" class="close-icon btn btn-danger"><i class="glyphicon glyphicon-remove"></i></button>
  167. </form>
  168. </div> <!-- col-6 / end -->
  169. @endforeach
  170. @endif
  171.  
  172.  
  173. </div> <!-- list-group / end -->
  174. </div> <!-- row / end -->
  175. </div> <!-- container / end -->
  176.  
  177.  
  178. </body>
  179.  
  180.  
  181. <script type="text/javascript">
  182. $(document).ready(function(){
  183. $(".fancybox").fancybox({
  184. openEffect: "none",
  185. closeEffect: "none"
  186. });
  187. });
  188. </script>
  189. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement