How to upload picture and store to database in Laravel 5 - laravel

I need to upload picture and store to database in Laravel 5.
My current code is:
Form:
<form method="POST" enctype="multipart/form-data" action="{{ url('products/new) }}">
{!! csrf_field() !!}
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" id="name" class="form-control" required>
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" id="image">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
Controller:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:100|unique:products',
]);
$input = $request->all();
Product::create($input);
return redirect('products');
}
The function $request->hasFile('image')) returns false.

hasFile('image') returns false because there is not an input with name 'image', only the id is 'image'
You should give it a name property, since that's the way the inputs are sent through http.

Instead of this:
<input type="file" id="image">
use this
<input name="image" type="file" id="image">
and in your validator use this:
$this->validate($request, [
'image' => 'required|max:100|unique:products',
]);

Related

Laravel $request->all() is empty

I start study laravel.
The route to contacts page
Route::match(['get', 'post'], '/contacts', [ 'uses' => 'Admin\ContactController#show', 'as' => 'contacts' ] );
class ContactController extends Controller {
public function show( Request $request ) {
print_r( $request->all() );
return view( 'default.contacts', [ 'title' => 'Contacts' ] );
}
}
Form
<form method="post" action="{{ route('contacts') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="inputEmail4">Name</label>
<input type="text" class="form-control" id="inputEmail4" placeholder="Name">
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
When i submit the form, i get an array with token.
Array
(
[_token] => JMTxTwh5Cb4sPeDjGcVetgTt2yGy6mDsFs6jW3Tx
)
What can be a problem?
Thanks for answer.
You are missing the name in your inputs. Please add to them.
<form method="post" action="{{ route('contacts') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="inputEmail4">Name</label>
<input type="text" class="form-control" name="name" id="inputEmail4" placeholder="Name">
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" name="address" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>

Laravel, adding data to database after verifying ReCaptcha

I'm using ReCaptcha in my Laravel project, done it with this
tutorial.
I need to create a page where user can post his message after checking captcha.
I have created a modal dialog where user can fill in data like this :
<form class="form-horizontal" action="" method="post">
<div class="form-group error">
<label for="messageName" class="col-sm-3 control-label">Name</label>
<div class="col-sm-9">
<input type="text" class="form-control has-error" id="name" name="name" placeholder="Your name" value=""
ng-model="message.name" ng-required="true">
<span class="help-inline"
ng-show="GBM.text.$invalid && GBM.text.$touched">Required</span>
</div>
</div>
<div class="form-group error">
<label for="messageEmail" class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
<input type="email" class="form-control has-error" id="email" name="email" placeholder="E-mail" value=""
ng-model="message.email" ng-required="true">
<span class="help-inline"
ng-show="GBM.email.$invalid && GBM.email.$touched">Required</span>
</div>
</div>
<div class="form-group error">
<label for="messageLink" class="col-sm-3 control-label">Web</label>
<div class="col-sm-9">
<input class="form-control" rows="3" class="form-control has-error" id="web" name="web" placeholder="Link for your web" value="" ng-model="message.web" ng-required="false" >
</div>
</div>
<div class="form-group error">
<label for="messageText" class="col-sm-3 control-label">Comment</label>
<div class="col-sm-9">
<textarea class="form-control" rows="3" class="form-control has-error" id="comment" name="comment" placeholder="Your comment" value="" ng-model="message.text" ng-required="true" ></textarea>
<span class="help-inline"
ng-show="GBM.text.$invalid && GBM.text.$touched">Required</span>
</div>
</div>
{!! csrf_field() !!}
<!-- recaptcha -->
{{Request::is('contactd')}}
<div class="form-group">
<div class="col-md-9">
<div class="g-recaptcha" data-sitekey="{{env('GOOGLE_RECAPTCHA_KEY')}}"></div>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-9 control-label"></label>
<div class="col-md-9">
<button type="submit" name="send" class="btn btn-primary btn-lg btn-block">Add new message <span class="fa fa-paper-plane-o"></span></button>
</div>
</div>
</form>
For a route I got it like this:Route::post('contact','ContactController#store');
And here is the problem, in my controller i got this code to verify captcha:
public function store(ReCaptchataTestFormRequest $request){
return "Captcha done right! ";}
And this code to save data to database
public function store(Request $request)
{
$this->validate($request, [ 'name' => 'required|max:255' ]);
$this->validate($request, [ 'email' => 'required | email' ]);
$this->validate($request, [ 'comment' => 'required' ]);
$ip = $_SERVER['REMOTE_ADDR'];
$browser = $_SERVER['HTTP_USER_AGENT'];
$guestbook = Guest_books::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
'web' => $request->input('web'),
'comment' => $request->input('comment'),
'ip' => $ip,
'browser' => $browser
]);
return $guestbook;
}
So the question is: What to write in Controller for project to verify Captcha and then post it to database?
The tutorial you've followed teaches you how to create a custom validation rule which you can then use when validating requests, either through a Form Request or directly in your controller.
The mistake you've made in your controller is that you've called validate multiple times, instead you should pass it an array containing all of your rules, including your recaptcha rule, e.g:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:255',
'email' => 'required|email',
'comment' => 'required',
'g-recaptcha-response' => 'required|recaptcha',
]);
// ...
}
Additionally, you should note that the store method should always return a redirect.

Laravel : no errors and no update on database happens after saving the edit

I have two tables, user and technicien, with a one to one relation. After editing technicien information through edit form and saving, no update happens on database and no errors as well.
Here is my code:
controllers
public function edit($id)
{
$technicien=technicien::find($id);
$user = $technicien->user;
return view('technicien.edit',['technicien'=>$technicien])->with('user',$user);
}
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update($request->get('user'));
$user->nom = $request->update('nom');
return redirect('/technicien');
}
View
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Technicien</h1>
<form action="{{ route('technicien.update', $technicien->technicien ) }}" method="update">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="nom">Nom</label>
<input id="nom" type="text" class="form-control" name="user[nom]" value="{{$user->nom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control" name="user[prenom]" value="{{$user->prenom}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-control" name="user[email]" value="{{$user->email}}" >
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name="moyenne_avis" class="form-control" value ="{{$technicien->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name="actif" class="form-control" value ="{{$technicien->actif}}" >
</div>
<div class="form-group">
<input type="submit" value="enregistrer" class="form-control btn btn-primary">
</div>
</div>
</form>
</div>
</div>
#endsection
route.php
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::patch('/technicien/{id}', 'TechnicienController#update')-
>name('technicien.update');
You just need to pass parameters to update function.
Read docs
public function update(Request $request, $id)
{
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update([
'nom' => $request->nom,
'premon' => $request->premon,
'email' => $request->email
]);
return redirect('/technicien');
}
Also from the docs
You should define which model attributes you want
to make mass assignable. You may do this using the $fillable property
on the model.

Storing registration data in database using laravel 5.4

Have been trying to create a user registration/login page following laracast video.
But when I try to register a user, it return to the same page without given any error as to why. I also noticed that the supplied info is not stored in the database.
Am so confused I don't know what am doing wrong you help will be really appreciated
Here is my view:
<div class="col-md-offset-1 col-md-8">
<h1 class="text-center ">Register</h1>
<form method="POST" action="register" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" email="email" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" required>
</div>
<div class="form-group">
<label for="password_confirmation">Password Confirmation</label>
<input type="password" class="form-control" name="password_confirmation" required>
</div>
<button type="submit" class="btn btn-primary"><i class="fa fa-reply"></i>Register</button>
</form>
</div>
This is my controller
public function create()
{
return view('registration.create');
}
public function store(Request $request)
{
//validate form
$this->validate(request(),[
'name' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed',
]);
//create and save user
$user = new User; //create new Post model object
$user->name = $request->name; //adding thing to the the Post object
$user->email = $request->email;
$user->password = $request->password;
//save user
$user->save(); //to save the new item into the DB
//$user = User::create(request(['name', 'email', 'password']));
//sign them in
auth()->login($user);
//redirect to the admin
return redirect('/admin');
}
While this is my route file:
Route::get('register', 'RegistrationController#create');
Route::post('register', 'RegistrationController#store');
Thanks a lot in advance
Check your form, there's an error in the email field. It says:
and should be:
<input type="email" class="form-control" name="email" required>
So, replace email="email" with name="email"
first you add this error block. so, you can find error easily.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Then, your mistake is in form action. please replace it to below code.
<form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">

dd($request->country_flag); returns null in laravel

This is my form to upload a file:
<div class="col-lg-6 col-lg-offset-3">
<form method="post" action="{{ route('admin.country.store') }}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="country_id">Country ID</label>
<input type="number" class="form-control" id="country_id" name="country_id">
</div>
<div class="form-group">
<label for="country_name">Country name</label>
<input class="form-control" type="text" id="country_name" name="country_name">
<p class="danger">{{ $errors->first('country_name') }}</p>
</div>
<div class="form-group">
<label for="alternate_title">Alternate Title</label>
<input class="form-control" type="text" id="alternate_title" name="alternate_title">
<p class="danger">{{ $errors->first('alternate_title') }}</p>
</div>
<div class="form-group">
<label for="country_flag">Country Flag</label>
<input class="" type="file" id="country_flag" name="country_flag">
<p class="danger">{{ $errors->first('country_flag') }}</p>
</div>
<div class="btn-group" role="group">
<button class="btn btn-default" type="reset">Reset</button>
<button class="btn btn-success" type="submit">Upload</button>
</div>
</form>
This is a function in my controller to handle form request.
public function store(Request $request)
{
$new_country = new SelectCountry();
$message = [
'required' => "This field can not be empty",
];
$this->validate($request, [
'country_name' => 'required',
'alternate_title' => 'required',
'country_flag' => 'required',
], $message);
dd($request->country_flag);
}
When I do dd($request->country_flag);, it returns null. It seems like file is not uploaded by the form.
What am I doing wrong?
I'm not sure if you can or not access a file like you are doing. Try this:
$file = $request->file('country_flas');
Try this
$input = $request->all();
$file = $input['country_flag'];
dd($file);

Resources