Beruflich Dokumente
Kultur Dokumente
Laravel is very strong web platform.
You can use it to realize your dreams.
After you read this e-book,
You will be able learn to use Image-File UPLOAD-EDIT Operations in
Laravel 5.
Lets start
1. Create buttonstore Project
( Note: If you did not have composer you can download it from this address
http://getcomposer.org/composer.phar )
Please pay attention, Before we type our command, you must be on localhost directory
like that
After you download Xampp programme on your computer,
Open command prompt programme,
Type this command
composer create-project laravel/laravel buttonstore prefer-dist
When you go to http://localhost/buttonstore/public/ address, you will able to see this
picture
2. Create Database for buttonstore
3. Database setup for buttonstore
Now You can go buttonstore/.env and open the .env file and you must change
the configuration like that
Then open config/database.php file and set the database settings like that
4. Create ButtonController for buttonstore
You can also follow ButtonController.php file from here
<?php namespace App\Http\Controllers;
use App\Button;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ButtonController extends Controller {
public function index() {
$buttons = Button::all();
return view(buttons.index,compact(buttons));
}
public function create() {
return view(buttons.create);
}
public function store(Request $request) {
$this->validate($request, [
title => required
]);
if ($request->hasFile(image)) {
$file = $request->file(image);
$file_name = $file->getClientOriginalName();
$random_name = rand(1,999999999999999999999);
$file_name = $random_name.-.$file->getClientOriginalName();
$file->move(img/, $file_name);
$data[image] = $file_name;
}
Button::create($data);
return redirect()->route(buttons.index);
}
public function show($id) {
$button = Button::find($id);
return view(buttons.show,compact(button));
}
public function edit($id) {
$button = Button::find($id);
return view(buttons.edit,compact(button));
}
public function update(Request $request, $id) {
$this->validate($request, [
title => required
]);
Button::find($id)->update($request->all());
$button = Button::find($id);
// Image Update codes
if ($request->hasFile(image)) {
$file = $request->file(image);
$file_name = $file->getClientOriginalName();
$random_name = rand(1,999999999999999999999);
$file_name = $random_name.-.$file->getClientOriginalName();
$file->move(img/, $file_name);
$button->image = $file_name;
}
$button->save();
return redirect()->route(buttons.index);
}
public function destroy($id) {
Button::find($id)->delete();
return redirect(buttons);
}
}
5. Create Button model
Type this command like that
Php artisan make:model Button
6. Install Form and Html Facades
Type this command Composer require illuminate/html
Then open config/app.php file and add in providers
Illuminate\Html\HtmlServiceProvider,
like that
Also add these codes in aliases in the same file like that
Form => Illuminate\Html\FormFacade,
Html => Illuminate\Html\HtmlFacade,
7. Setting Routes.php file
8. Insert imaginary data
Then write these codes to create a template.blade.php under layout folder.
You can also follow these codes from here.
<!doctype html>
<html lang=en>
<head>
<meta charset=UTF-8>
<title>Button Store</title>
<link rel=stylesheet href=https://maxcdn.bootstrapcdn.com/bootstrap
/3.3.4/css/bootstrap.min.css>
</head>
<body>
<div class=container>
@yield(content)
</div>
</body>
</html>
Then Create index.blade.php file under buttons folder like that
You can also follow the codes from here
@extends(layout/template)
@section(content)
<h1>Button Store</h1>
<a href={{url(/buttons/create)}} class=btn btn-success>Create Button</a>
<hr>
<table class=table table-striped table-bordered table-hover>
<thead>
<tr class=bg-info>
<th>Id</th>
<th>Title</th>
<th>Images</th>
<th colspan=3>Actions</th>
</tr>
</thead>
<tbody>
@foreach ($buttons as $button)
<tr>
<td>{{ $button->id }}</td>
<td>{{ $button->title }}</td>
<td><img src={{asset(img/.$button->image)}} height=35 width=30></td>
<td><a href={{url(buttons,$button->id)}} class=btn btn-primary>Read</a></td>
<td><a href={{route(buttons.edit,$button->id)}} class=btn btnwarning>Update</a></td>
<td>
{!! Form::open([method => DELETE, route=>[buttons.destroy, $button->id]]) !!}
{!! Form::submit(Delete, [class => btn btn-danger]) !!}
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
Then Create show.blade.php file under buttons folder like that
You can also follow the codes from here
@extends(layout/template)
@section(content)
<h1>Button Show</h1>
<form class=form-horizontal>
<div class=form-group>
<label for=image class=col-sm-2 control-label>Image {{ $button->id }}</label>
<div class=col-sm-10>
<img src={{asset(img/.$button->image)}} height=180 width=150 class=imgrounded>
</div>
</div>
<div class=form-group>
<label for=title class=col-sm-2 control-label>Title</label>
<div class=col-sm-10>
<input type=text class=form-control id=title placeholder={{$button->title}}
readonly>
</div>
</div>
<div class=form-group>
<div class=col-sm-offset-2 col-sm-10>
<a href={{ url(buttons)}} class=btn btn-primary>Back</a>
</div>
</div>
</form>
@stop
To Create create.blade.php file under buttons folder like that
You can also follow the codes from here
@extends(layout.template)
@section(content)
<h1>Create New Button</h1>
{!! Form::open(array(route => buttons.store,class => form,files => true)) !!}
<div class=form-group>
{!! Form::label(Title, Title:) !!}
{!! Form::text(title,null,[class=>form-control]) !!}
</div>
<div class=form-group>
{!! Form::label(Image, Image:) !!}
{!! Form::file(image,null,[class=>form-control]) !!}
</div>
<div class=form-group>
{!! Form::submit(Save, [class => btn btn-primary form-control]) !!}
</div>
{!! Form::close() !!}
@stop
To Create edit.blade.php file under buttons folder like that
<div class=form-group>
{!! Form::label(Title, Title:) !!}
{!! Form::text(title,null,[class=>form-control]) !!}
</div>
<div class=form-group>
{!! Form::label(Image, Image:) !!}
{!! Form::file(image, null) !!}
</div>
<div class=form-group>
{!! Form::submit(Update, [class => btn btn-primary]) !!}
</div>
{!! Form::close() !!}
@stop
Note : Also you must create a new folder called img under