When I'm submitting the image, it shows the following error:
Call to a member function getClientOriginalExtension() on null
I do not know, where the mistake is.
This is my controller named ProductController:
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'brand' => 'required',
'detail' => 'required',
'size' => 'required',
'type' => 'required',
'price' => 'required',
'image' => 'required',
]);
$image = $request->file('image');
$new_name = rand().'.'.$image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
$form_data = array(
'image' => $new_name,
'name' => $request->input('name'),
'size' => $request->input('size'),
'type' => $request->input('type'),
'price' => $request->input('price'),
'detail' => $request->input('detail'),
'brand' => $request->input('brand'),
);
Product::create($form_data);
return redirect()->route('product.index')->withSuccess('Done');
}
The error occours on this line: $new_name = rand().'.'.$image->getClientOriginalExtension();
This is my form from where I am submitting the image:
<form id="myForm" method="post" action="{{ route('product.store') }}">
#csrf
<div class="user-box">
<input type="text" name="name" required="">
<label>Name</label>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="brand" required="">
<label>Brand</label>
#error('brand')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="price" required="">
<label>Price</label>
#error('price')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box" style=" border-bottom: 1px solid white">
<span style="font-weight: bold; color: white; font-size: 16px; margin-bottom: 30px ">Size</span>
<select name="size" id="size" style="width: 40px; font-size: 16px; margin-bottom: 20px ">
<option value="50 ML">50 ML</option>
<option value="100 ML">100 ML</option>
<option value="200 ML">200 ML</option>
<option value="500 ML">500 ML</option>
<option value="1 L">1 L</option>
<option value="4 L">4 L</option>
<option value="10 L">10 L</option>
<option value="20 L">20 L</option>
</select>
#error('size')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="type" required="">
<label>Type</label>
#error('type')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="detail" required="">
<label>Detail</label>
#error('detail')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
Image<input type="file" name="image" required="">
<label>Image</label>
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<button class="a" id="a" value="submit" name="submit">Submit</button>
</form>
your server is not receiving the file that you are uploading,
Try adding the enctype='multipart/form-data' to the form in blade file.
<form id="myForm" method="post" action="{{ route('product.store') }}" enctype='multipart/form-data'>
Add attribute enctype="multipart/form-data" to your form tag like below:
<form id="myForm" method="post" action="{{ route('product.store') }}" enctype="multipart/form-data">
Related
my 3 inputs address, number and name_of_firm is showing null value but i fill the value in form.
This is my form
<form id="myForm" method="post" action="{{ route('dealer.store') }}">
#csrf
<div class="user-box">
<input type="text" name="name" required="">
<label>Name</label>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="name_of_firm" required="">
<label>Name Of Firm</label>
#error('name_of_firm')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="number" required="">
<label>Number</label>
#error('number')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="address" required="">
<label>Address</label>
#error('address')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="email" name="email" required="">
<label>Email</label>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="password" name="password" required="">
<label>Password</label>
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<button class="a" id="a" value="submit" name="submit">Submit</button>
</form>
This is my controller. the name of my controller is DealerController from where i storing data in users table
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required',
'name_of_firm' => 'required',
'address' => 'required',
'number' => 'required',
]);
$user = User::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
'password' => Hash::make($request->input('password')),
'name_of_firm' => $request->input('name_of_firm'),
'address' => $request->input('address'),
'number' => $request->input('number'),
]);
return redirect()->route('dealer.index')->withSuccess('done');
}
this is my User model. there are only 3 field that are fillable.
protected $fillable = [
'name',
'email',
'password',
];
This is pic of database.
enter image description here
That's because of Laravel mass-assignment and you should add every field you want to fill with input in $fillable array.
change your fillable array like below:
protected $fillable = [
'name',
'email',
'password',
'name_of_firm',
'address',
'number'
];
Replace your $fillable to this
protected $guarded = [
'id',
];
It'll resolve the issue
If you are using $fillable try to add the other fields also, but if you don't want to add other fields try to use $guarded:
$fillable
protected $fillabel = [
'name',
'email',
'password',
'name_of_firm',
'address',
'number'
];
$guarded
protected $guarded = [];
Route [staff] not defined.
This is my web.php (route).
Route::get('/staff', function () {
return view('staff');
});
Route::resource('/staff', StaffController::class);
This is my controller. The index, create page is in same page.
public function index()
{
$staffs = Staff::all();
return view('staff', compact('staffs'));
}
public function create()
{
return view('staff');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'gender' => 'required',
'salary' => 'required',
]);
$staff = new Staff();
$staff->name = $request->name;
$staff->gender = $request->gender;
$staff->salary = $request->salary;
$staff->save();
return redirect()->route('staff')->withSuccess('Done');
}
And this is my staff.blade.php where after click on submit error occuring (route not defined).
<form id="myForm" method="post" action="{{ route('staff.store') }}">
#csrf
<div class="user-box">
<input type="text" name="name" required="">
<label>Name</label>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="gender" required="">
<label>Gender</label>
#error('gender')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="salary" required="">
<label>Salary</label>
#error('salary')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<button class="a" id="a" value="submit" name="submit">Submit</button>
</form>
There are no any name route called staff, it would be staff.index :
return redirect()->route('staff.index')->withSuccess('Done');
See Resource Route Name
1st remove your get route all is covered by resource
Route::resource('/staff', StaffController::class);
this code generate route names like
Route Name
staff.index
staff.create
staff.store
staff.show
staff.edit
staff.update
staff.destroy
so in your code you need to do this
return redirect()->route('staff.index')->withSuccess('Done');
ref link https://laravel.com/docs/8.x/controllers#actions-handled-by-resource-controller
I have a simple form where the user should be able to only fill the inputs he wants but if the form hasn't at least one input filled, it should return a error message saying that the form can't be empty if he clicks the submit button.
Here's my form:
#if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<strong>{{ $message }}</strong>
</div>
#endif
</center>
<form action="{{ route('send-update') }}" method="POST" enctype="multipart/form-data"> #csrf
<label>Nome do projeto:</label>
<div class="input-group mb-3"><br>
<input type="text" class="form-control" name="project_name" aria-label="Username" aria-describedby="basic-addon1">
</div>
#error('project_name')
<div class="text-danger" style="float:left; margin-top:-10px" role="alert">
<small> {{$message}}</small>
</div>
#enderror
<br>
<label>Descrição:</label>
<div class="input-group mb-3"><br>
<textarea class="form-control" name="desc" aria-label="With textarea"></textarea>
</div>
#error('desc')
<div class="text-danger" style="float:left; margin-top:-10px" role="alert">
<small> {{$message}}</small>
</div>
#enderror
<br>
<label>Tem alguma imagem que queira mudar/inserir?</label>
<div class="input-group mb-3">
<input type="file" name="img" accept="image/*" class="form-control" id="inputGroupFile01">
</div>
#error('img')
<div class="text-danger" style="float:left; margin-top:-10px" role="alert">
<small> {{$message}}</small>
</div>
#enderror
<div class="input-group mb-3">
<input type="text" class="form-control" name="img_desc" placeholder="Diga-nos onde pretende mudar/adicionar">
</div>
#error('img_desc')
<div class="text-danger" style="float:left; margin-top:-10px" role="alert">
<small> {{$message}}</small>
</div>
#enderror
<br>
<label>Tem mais informações? Insira um arquivo .txt, docx ou pdf</label>
<div class="input-group mb-3">
<input type="file" name="ficheiro" accept=".xlsx,.xls,.doc, .docx,.ppt, .pptx,.txt,.pdf" class="form-control" id="inputGroupFile01">
</div>
#error('ficheiro')
<div class="text-danger" style="float:left; margin-top:-10px" role="alert">
<small> {{$message}}</small>
</div>
#enderror
<br>
<input type="hidden" name="id" value="{{ request('id') }}">
<center> <button type="submit" class="btn btn-secondary ">Fazer pedido de atualização</button></center><br>
</form>
And here's the controller:
public function sendUpdate(Request $request){
$id = $request['id'];
$order = Order::where('id', $id)->first();
$validator = Validator::make($request->all(), [
'project_name' => ['nullable', 'string', 'max:255', 'regex:/^[a-zA-Z]+$/u]'],
'desc' => ['nullable', 'string', 'max:255', 'regex:/^[a-zA-Z]+$/u]'],
'img' => ['nullable', 'image', 'mimes:jpeg,png,jpg,svg', 'max:1024'],
'img_desc' => ['nullable', 'string', 'max:255', 'regex:/^[a-zA-Z]+$/u]'],
'ficheiro' => ['nullable', 'csv,txt,xlx,xls,pdf', 'max:2048'],
]);
if (request()->hasFile('ficheiro')){
if (request()->file('ficheiro')->isValid())
{
$fileName = $request->file('ficheiro')->getClientOriginalName();
$path = $request->file('ficheiro')->storeAs('files', $fileName, 'public');
}}
if (request()->hasFile('img')){
if (request()->file('img')->isValid())
{
$imageName = $request->file('img')->getClientOriginalName();
$pathImg = $request->file('img')->storeAs('files', $imageName, 'public');
}}
Update::create([
'customer_name' => $order->customer_name,
'customer_email' => $order->email,
'project_name' => $order->project_name,
'project_new_name' => $request['project_name'],
'description' => $request['desc'],
'image' => $pathImg ?? '',
'image_desc' => $request['img_desc'],
'ficheiro' => $path ?? '',
]);
return back()->with('success','Obrigado! Entraremos em contacto consigo em breve!');
}
I have no idea how to check if all inputs are empty in the controller . I've already tried this below, but it doesn't work. It keeps sending the form anyway.
if(count($request->all()) < 0) {
return dd('request all input empty.');
}
You can do it this way.
if(empty(array_filter($request->all()))){
//All fields are empty.
}
In my Laravel-5.8 project consisting of Sales application, I have this codes:
Controller:
public function create()
{
$suppliers =Supplier::all();
$categories = Category::all();
$taxes = Tax::all();
$units = Unit::all();
return view('product.create', compact('categories','taxes','units','suppliers'));
}
public function store(Request $request)
{
$request->validate([
'name' => 'required|min:3|unique:products|regex:/^[a-zA-Z ]+$/',
'serial_number' => 'required',
'model' => 'required|min:3',
'category_id' => 'required',
'sales_price' => 'required',
'unit_id' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'tax_id' => 'required',
]);
$product = new Product();
$product->name = $request->name;
$product->serial_number = $request->serial_number;
$product->model = $request->model;
$product->category_id = $request->category_id;
$product->sales_price = $request->sales_price;
$product->unit_id = $request->unit_id;
$product->tax_id = $request->tax_id;
if ($request->hasFile('image')){
$imageName =request()->image->getClientOriginalName();
request()->image->move(public_path('images/product/'), $imageName);
$product->image = $imageName;
}
$product->save();
foreach($request->supplier_id as $key => $supplier_id){
$supplier = new ProductSupplier();
$supplier->product_id = $product->id;
$supplier->supplier_id = $request->supplier_id[$key];
$supplier->price = $request->supplier_price[$key];
$supplier->save();
}
return redirect()->back()->with('message', 'Product Created Successfully');
}
view
<div class="row mt-2">
<div class="clearix"></div>
<div class="col-md-10">
<div class="tile">
<h3 class="tile-title">Product</h3>
<div class="tile-body">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form method="POST" action="{{route('product.store')}}" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="form-group col-md-6">
<label class="control-label">Product Name</label>
<input name="name" class="form-control #error('name') is-invalid #enderror" type="text" placeholder="Product Name">
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Serial Number</label>
<input name="serial_number" class="form-control #error('serial_number') is-invalid #enderror" type="number" placeholder="Enter Tax Name">
#error('serial_number')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Model</label>
<input name="model" class="form-control #error('name') is-invalid #enderror" type="text" placeholder="Enter Tax Name">
#error('model')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Category</label>
<select name="category_id" class="form-control">
<option>---Select Category---</option>
#foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
#error('category_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Sale Price</label>
<input name="sales_price" class="form-control #error('sales_price') is-invalid #enderror" type="number" placeholder="Enter Tax Name">
#error('sales_price')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Unite</label>
<select name="unit_id" class="form-control">
<option>---Select Unit---</option>
#foreach($units as $unit)
<option value="{{$unit->id}}">{{$unit->name}}</option>
#endforeach
</select>
#error('unit_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Image</label>
<input name="image" class="form-control #error('image') is-invalid #enderror" type="file" >
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Tax </label>
<select name="tax_id" class="form-control">
<option>---Select Tax---</option>
#foreach($taxes as $tax)
<option value="{{$tax->id}}">{{$tax->name}} %</option>
#endforeach
</select>
#error('tax_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="tile">
<div id="example-2" class="content">
<div class="group row">
<div class="form-group col-md-5">
<select name="supplier_id[]" class="form-control">
<option>Select Supplier</option>
#foreach($suppliers as $supplier)
<option value="{{$supplier->id}}">{{$supplier->name}} </option>
#endforeach
</select>
</div>
<div class="form-group col-md-5">
<input name="supplier_price[]" value="{{old('supplier_price')}}" class="form-control #error('supplier_price') is-invalid #enderror" type="number" placeholder="Enter Sales Price">
<span class="text-danger">{{ $errors->has('additional_body') ? $errors->first('body') : '' }}</span>
</div>
<div class="form-group col-md-2">
<button type="button" id="btnAdd-2" class="btn btn-primary float-right"><i class="fa fa-plus"></i></button>
<button type="button" class="btn btn-danger btnRemove float-right"><i class="fa fa-trash"></i></button>
</div>
</div>
</div>
</div>
<div class="form-group col-md-4 align-self-end">
<button class="btn btn-primary" type="submit"><i class="fa fa-fw fa-lg fa-check-circle"></i>Update</button>
</div>
</form>
</div>
</div>
</div>
</div>
When I submitted the form, I got this error:
htmlspecialchars() expects parameter 1 to be string, array given (View:...
Then when I removed this:
value="{{old('supplier_price')}}"
from:
<div class="form-group col-md-5">
<input name="supplier_price[]" value="{{old('supplier_price')}}" class="form-control #error('supplier_price') is-invalid #enderror" type="number" placeholder="Enter Sales Price">
<span class="text-danger">{{ $errors->has('additional_body') ? $errors->first('body') : '' }}</span>
</div>
the error vanished.
I wanted to use it for the input field to retain its value after any error in validation. Where did I get it wrong and how do I correct it?
Thank you
supplier_price is an array since you define it as name="supplier_price[]"
your old() function will output an array while the mustache function {{}} expects a string.
If I understand your code properly you should be better off removing the [] from name="supplier_price[]"
I am currently developing a simple Bookstore application with a few numbers of users on which sending emails are not needed because it will be implemented in local system so is there any way to customize laravel-auth for password reset function by adding a few security questions fields where user can reset his/her password without sending reset links via email.
Any kind of help will be highly appreciated.
here I tried the below code but id did not work
Code in web.php
Route::post('/main/checklogin', 'UserController#chekQuestions');
Code in userContoller
public function chekQuestions(Request $request)
{
$request->validate( [
'email' => 'required|string|email',
'answerQuestionOne' => 'required|string|confirmed',
'answerQuestionTwo' => 'required|string'
] );
$user = User::first();
if($user->email == $request->email && $user->answerQuestionOne == $request->answerQuestionOne && $user->answerQuestionTwo == $request->answerQuestionTwo )
{
// $userEmail = DB::table( 'password_resets' )->where( 'token', $user->token );
// return view('auth.password.reset',compact($userEmail));
return view('auth.password.reset');
}
return response()->json( [
'error' => true,
'message' => 'We cannot find a user with that Email Address'
], 404 );
}
Code in reset password.blade
<div id="register" class="animate form registration_form">
<section class="login_content">
<form method="POST" action="{{ url('/main/checklogin') }}" >
#csrf
<h3>د پټ نو بیا راګرځولو لپاره لاندی امنتی پوښتنو ته ځواب ورکړی </h3>
<div class="form-group has-feedback">
<input id="email" type="email" placeholder=" ایمل" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<div class="form-control-feedback">
<i class="fa fa-envelope-o text-muted"></i>
</div>
</div>
<div>
<input id="answerQuestionOne" placeholder="لومړۍ امنیتي پوښتنه" type="text" class="form-control #error('answerQuestionOne') is-invalid #enderror" name="answerQuestionOne" value="{{ old('answerQuestionOne') }}" required autocomplete="answerQuestionOne" autofocus>
#error('answerQuestionOne')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div>
<input id="answerQuestionTwo" placeholder="دوهمه امنیتي پوښتنه " type="text" class="form-control #error('answerQuestionTwo') is-invalid #enderror" name="answerQuestionTwo" value="{{ old('answerQuestionTwo') }}" required autocomplete="answerQuestionTwo" autofocus>
#error('answerQuestionTwo')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<button type="submit" class="btn btn-default btn-block">خوندی کړی </button>
<div class="clearfix"></div>
<div class="separator">
<p class="change_link">
تاسو دمخه غړی یاست ننوتل
</p>
<div class="clearfix"></div>
<br />
</form>
</section>
</div>
You don't need Laravel implementation for this. Just Find a user with the given email and check the answers. After that update the user record with the new password.
In order to fetch user you should do this:
$data = $request->validate( [
'email' => 'required|string|email',
'answerQuestionOne' => 'required|string|confirmed',
'answerQuestionTwo' => 'required|string'
] );
$user = User::where(['email' => $data['email'])->first();
After this just check the answers.
You also need to take the new password from user.