Skip to content

Commit 481f9ca

Browse files
committed
add image field to petition
1 parent b3bb6d2 commit 481f9ca

File tree

11 files changed

+207
-6
lines changed

11 files changed

+207
-6
lines changed

app/Http/Controllers/AdminPetitionController.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
class AdminPetitionController extends Controller
99
{
10-
//TODO best way?
10+
//TODO constantly had trouble with my authentication sticking
11+
//had to comment out this in development??
1112
public function __construct()
1213
{
1314
$this->middleware('auth');
@@ -63,6 +64,11 @@ public function store(Request $request)
6364
$petition->emailsubject = $request->emailsubject;
6465
$petition->emailbody = $request->emailbody;
6566

67+
if ($request->hasFile('image')) {
68+
$path = $request->image->store('public');
69+
$petition->image = str_replace('public/', '', $path);
70+
}
71+
6672
$petition->save();
6773
return redirect()->route('petition.show', $petition->id)
6874
->with('success','Petition created successfully');
@@ -146,6 +152,12 @@ public function update(Request $request, Petition $petition)
146152
$petition->emailsubject = $request->emailsubject;
147153
$petition->emailbody = $request->emailbody;
148154

155+
//TODO remove old files
156+
if ($request->hasFile('image')) {
157+
$path = $request->image->store('public');
158+
$petition->image = str_replace('public/', '', $path);
159+
}
160+
149161
$petition->save();
150162
return redirect()
151163
->route('petition.show', $petition->id)

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"type": "project",
77
"require": {
88
"php": ">=5.6.4",
9+
"intervention/image": "^2.3",
910
"laravel/framework": "5.4.*",
1011
"laravel/tinker": "~1.0"
1112
},

composer.lock

+171-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

database/migrations/2017_01_26_152129_create_petitions_table.php

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function up()
2121
$table->text('thankyou');
2222
$table->string('emailsubject');
2323
$table->text('emailbody');
24+
$table->string('image')->nullable();
2425
$table->timestamps();
2526
});
2627
}

public/css/custom.css

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

22
.button-form {
33
display: inline;
4+
}
5+
6+
.petition-image {
7+
width:100%;
48
}

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## Issues
66

77
* Having issues with sessions on my dev environment... couldn't get things like csrf token to validate. Temporarily disabled to work around this.
8-
8+
* Image / Media field requirement: I missed the boat on this and basically just added an image field to the petition table. It's clearly an underwhelming approach but I wanted to demonstrate at least basic file handling.
99

1010
## Further work needed
1111

resources/views/admin/petition/_form.blade.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
<label for="body" class="control-label">Body</label>
2020

2121
<textarea id="body" type="text" class="form-control" name="body" >{{ $petition->body }}</textarea>
22-
22+
</div>
23+
24+
<div class="form-group">
25+
<label for="image" class="control-label">Image</label>
26+
<input type="file" name="image" class="form-control" />
2327
</div>
2428

2529
<div class="form-group">

resources/views/admin/petition/create.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
<h1>New Petition</h1>
9-
<form class="form-horizontal" role="form" method="POST" action="{{ url('/admin/petition') }}">
9+
<form class="form-horizontal" role="form" method="POST" action="{{ url('/admin/petition') }}" enctype="multipart/form-data">
1010
<!-- TODO {{ csrf_field() }} -->
1111

1212
@include('admin.petition._form', ['petition' => new \App\Petition ])

resources/views/admin/petition/edit.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@include('messages')
66

77
<h1>Edit Petition</h1>
8-
<form class="form-horizontal" role="form" method="POST" action="{{ url('/admin/petition', $petition->id) }}">
8+
<form class="form-horizontal" role="form" method="POST" action="{{ url('/admin/petition', $petition->id) }}" enctype="multipart/form-data">
99
{{ method_field('PATCH') }}
1010
<!-- TODO {{ csrf_field() }} -->
1111

resources/views/admin/petition/show.blade.php

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
<dt>Body</dt>
2424
<dd>{!! $petition->body !!}</dd>
25+
26+
<dt>Image</dt>
27+
@if ($petition->image)
28+
<dd><img src="/storage/{{ $petition->image }}" class="petition-image" /></dd>
29+
@endif
2530
</dl>
2631

2732
<a class="btn btn-default" href="{{ URL::route('petition.public', $petition->id) }}">View Publicly</a>

resources/views/petition/show.blade.php

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
<h1>{{$petition->title}}</h1>
77
</div>
88

9+
@if ($petition->image)
10+
<img src="/storage/{{ $petition->image }}" class="petition-image" />
11+
@endif
12+
913
<div class='petition-body'>
1014
{!! $petition->body !!}
1115
</div>

0 commit comments

Comments
 (0)