In Laravel blade, I'm writing a form where certain users can add the details of a book to a database. The HTML is below: pay attention to the 'photo' section.
<form action="submitAdd" method="get" class="form-inline" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label for="title ">Title: </label>
<input type="text" name="title " >
<label for="author">Author: </label>
<input type="text" name="author">
</div>
<div class="form-group">
<label for="book_id">Book ID: </label>
<input type="number" name="book_id">
</div>
<div class="form-group">
<label for="genre_id">Genre: </label>
<select name="genre_id">
<option name="Arts" value=1>Arts</option>
<option name="Engineering" value=2>Engineering</option>
<option name="Chemistry" value=3>Chemistry</option>
<option name="Philosopy" value=4>Philosophy</option>
<option name="Medicine" value=5>Medicine</option>
</select>
</div>
<div class="form-group">
<label for="status_id">Status: </label>
<select name="status_id">
<option name="Available" value=1>Available</option>
<option name="Requested" value=2>Requested</option>
<option name="Unavailable" value=3>Unavailable</option>
<option name="Alumni" value=4>Alumni</option>
</select>
</div>
<div class="form-group">
<label for="photo">Photo: </label>
<input type="file" name="photo" />
</div>
<div class="form-group">
<input type="submit" name="submit" value="Add Book" />
</div>
</form>
This invokes a controller's method, "AddBook", which is listed below
public function addBook(Request $request) {
$genres = [1, 2, 3, 4, 5];
$statuses = [1,2,3,4];
$validated = Validator::make($request->all(), [
"submit" => "required",
"book_id" => ["required", "integer", "regex:/^[0-9]+$/"],
"title" => ["required", "regex: /^[a-zA-Z’'. -]+$/"],
"author" => ["required", "regex: /^[a-zA-Z’'. -]+$/"],
"genre_id" => ["required", Rule::in([1, 2, 3, 4, 5]) ],
"status_id" => ["required", Rule::in([1, 2, 3, 4]) ],
"photo" => "required|image|file:jpeg,png,jpg,gif,svg|max:2048"
]);
$errors = $validated->errors();
if($validated->fails()) {
return redirect()->back()->withInput($request->all())->withErrors($errors);
}
$data = $request->all();
$image = "";
if ($request->hasFile("photo")) {
echo "<h2>". $request->photo . "</h2>";
$image = $request->file('photo');
$new_name = rand() . '.' . $image->getClientOriginalExtension(); //CAUSES ERROR
$image->move(public_path('photos'), $new_name);
} else {
echo "<h3>Request doesn't have photo</h3>";
}
try {
$insert =
DB::table('CCEAGpoc.dbo.Student')->insert([
['student_id' => $data['student_id'],
‘title' => $data['title'],
'author' => $data['author'],
'genre_id' => $data['genre_id'],
'photo' => $image,
'status_id' => $data['status_id']]
]);
return view ('submit/submitSuccess/addSuccess');//->with('message', $message);
} catch (Exception $ex) {
return redirect()->back()->withInput($request->all())->withErrors($errors);
}
}
The above code works for everything except photos.
When I make a submission, I get the following validation failures:
"The photo must be an image. The photo must be a file."
When I remove the photo validation, I get these errors instead:
"SQLSTATE[HY000]: General error: 20018 Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query. [20018] (severity 16) [insert into [CCEAGpoc].[dbo].[Student] ([course_id], [forename], [photo], [status_id], [student_id], [surname]) values ('2', 'w', '', '3', '10000029', 'w')] (SQL: insert into [CCEAGpoc].[dbo].[Student] ([course_id], [forename], [photo], [status_id], [student_id], [surname]) values (2, w, , 3, 10000029, w))"
When I remove the if-check that executes the photo checking, I get this error:
Call to a member function getClientOriginalExtension() on null
For this line:
$new_name = rand() . '.' . $image->getClientOriginalExtension();
I've looked up the solutions to these errors, but they haven't helped me. What else should I be doing?
dd($request->photo)
? – Kevin RED Oct 31 '18 at 17:56‘title'
You have a non ascii single quote in there, have you been editing with a word processor by any chance? – RiggsFolly Oct 31 '18 at 18:14