I'm trying to create a picture for every contact.
this is the store() method in my controller:
public function store()
{
$attributes = request()->validate([
'user_id' => ['required'],
'avatar' => ['required',],
'ime' => ['required', 'min:3'],
'prezime' => ['required', 'min:3'],
'broj' => ['required', 'min:3'],
]);
Kontakt::create($attributes);
return redirect('/imenikk');
}
this is the create.blade.php:
<form method="POST" action="/imenikk">
{{ csrf_field() }}
<div class="container text-center">
<label for="ime">Ime</label>
<input type="text" class="form-control text-center" name="ime"
placeholder="Ime">
<label for="prezime">Prezime</label>
<input type="text" class="form-control text-center" name="prezime"
placeholder="Prezime">
<label for="broj">Broj</label>
<input type="text" class="form-control text-center" name="broj"
placeholder="Broj">
<br>
<input type="file" name="avatar" accept="image/*">
<br>
<input type="hidden" name="user_id" value='{{$user_id}}'>
<button type="submit" class="btn btn-primary">Dodaj
</div>
</form>
And this is on show.blade.php:
<img src="data:image/jpg;base64,{{ chunk_split(base64_encode($imenikk->avatar)) }}" height="500" width="500">
<img src ="data:image/jpeg;base64,{{base64_encode($imenikk->avatar)}}" height="200" width="200">
Neither of these 2 are working.. I get this but the image doesn't load:
<img src="data:image/jpg;base64,SU1HXzYxODQuSlBH" height="500" width="500">
This is how my db looks when I create a contact with picture:
Also I have managed to decode this
img src="data:image/jpg;base64,SU1HXzYxODQuSlBH
and I get the image name when I decode it yet the page still doesn't display the image.
It seems your base64 code is wrong.
Try to get the real base64 code from image file, and store in database.
public function store()
{
$attributes = request()->validate([
'user_id' => ['required'],
'avatar' => ['required',],
'ime' => ['required', 'min:3'],
'prezime' => ['required', 'min:3'],
'broj' => ['required', 'min:3'],
]);
$file = request()->file('avatar');
$imagedata = file_get_contents($file->getRealPath());;
$base64 = base64_encode($imagedata);
$attributes['avatar'] = $base64;
Kontakt::create($attributes);
return redirect('/imenikk');
}
then store it in your field,
and display it like this:
<img src={{ "data:image/jpg;base64,".$imenikk->avatar }} height="500" width="500">
Related
I'm making a form so that users can request to create a new account to the admin, but every time I try to send data, it always fails, even though I've checked every part of my code, can anyone help me?
the form code :
<form method="POST" action="{{ url('/request-akun/make-request') }}">
#csrf
<div class="form-group col-md-12">
<label for="name">Nama</label>
<input type="text" name="name" class="form-control" id="name">
</div>
<div class="form-group col-md-12">
<label for="email">Email</label>
<input type="text" name="email" class="form-control" id="email">
</div>
<div class="form-group col-md-12">
<label for="password">Password</label>
<input type="text" name="password" class="form-control"
id="password">
</div>
<div class="form-group col-md-12">
<label for="password_confirm">Konfirmasi Password</label>
<input name="password_confirm" type="text" class="form-control"
id="password">
</div>
<button type="submit" class="btn btn-primary pull-right"
style="margin-right: 15px;">Request</button>
</form>
this is the controller :
public function requestAccount(Request $request){
$request->validate([
'name' => 'required|string|max:255',
'ra_instansi_id' => 'required',
'email' => 'required|string|email|max:255',
'password' => 'required',
'password_confirm' => 'required'
]);
if ($request->get('password') == $request->get('password_confirm')){
$data = new requestAkun();
$data->name = $request->get('name');
$data->ra_user_id = Auth::id();
$data->ra_instansi_id = Auth::user()->instansiID;
$data->email = $request->get('email');
$data->password = $request->get('password');
$data->status = "pending";
$data->save();
return Redirect::to('/request-akun/request-lists')->with('success','Request Berhasil dikirim');
} else {
return Redirect::to('/request-akun/request-lists')->with('error','Password tidak sama');
}
}
this is the model code:
class requestAkun extends Model
{
use HasFactory, Uuids;
protected $table = 'request_akun';
public $timestamps = true;
protected $fillable = [
'name',
'ra_instansi_id',
'ra_user_id',
'email',
'password',
'status'
];
}
this is the route :
Route::prefix('request-akun')->group(function(){
Route::post('/make-request',[userController::class,'requestAccount'])->name('make-request');
});
Error is you don't have ra_instansi_id input in your form.
Not showing any validation error message in your form so better show errors so you check which validation failed.
3.Instead of checking password match in if condition you can change input name password_confirm to password_confirmation so you can change like below
<input name="password_confirmation" type="text" class="form-control"
id="password_confirmation">
and in your validation
$validator=Validator::make($request->all(),[
'name' => 'required|string|max:255',
'ra_instansi_id' => 'required',
'email' => 'required|string|email|max:255',
'password' => 'required|confirmed',
'password_confirmation' => 'required'
]);
dd($validator->errors());
I want to store the selected checks in a single variable and send it to the database, I am only using migrations, others that other data is sent in the migration.
Pd: I get this error by the array:
ErrorException
Array to string conversion
My template.
<form action="{{ route('save') }}" method="POST">
#csrf
<div class="row">
<div class="col-md-6 mt-5">
<select name="Sede" class="form-select" aria-label="Default select example">
<option selected name="Sede">Campus Sede</option>
<option value="Cancún">Cancún</option>
<option value="Chihuahua">Chihuahua</option>
</select>
</div>
<div class="col-md-6 mt-5">
<select name="Alcance" class="form-select" aria-label="Default select example">
<option selected name>Alcance</option>
<option value="Local">Local</option>
<option value="Nacional">Nacional</option>
</select>
</div>
<div class="col-md-6 mt-5">
<label for="">Nombre del Proyecto</label>
<input type="text" name="Nombre" class="form-control col-12">
</div>
<div class="col-md-6 mt-5">
<label for="">Cierre de Inscripciones</label>
<input data-date-format="yyyy/mm/dd" id="datepicker" name="Cierre" class="form-control col-12">
</div>
<div class="col-md-6 mt-5">
<input type="checkbox" name="Areas[]"id="ingenieria" value="Ingeniería" />
<label for="ingenieria">Ingeniería</label>
<input type="checkbox" name="Areas[]"id="humanidades" value="Humanidades" />
<label for="humanidades">Humanidades</label>
<input type="checkbox" name="Areas[]"id="negocios" value="Negocios" />
<label for="negocios">Negocios</label>
<input type="checkbox" name="Areas[]" id="salud" value="Salud" />
<label for="salud">Salud</label>
</div>
<div class="col-md-12 mt-5">
<label for="">Cual es el departamento donde impactara directamente el proyecto?</label>
<input type="text" name="P1" class="form-control col-12">
</div>
<div class="row form-group mt-5">
<button type="submit" class="btn btn-success col-md-2 offset-5">Guardar</button>
</div>
</form>
My controller:
public function save(Request $request){
$validator = $this->validate($request, [
'Sede'=> 'required|string|max:255',
'Alcance'=> 'required|string|max:255',
'Nombre'=> 'required|string|max:255',
'Cierre'=> 'required|date',
'Areas'=> 'required',
'P1'=> 'required|string|max:255',
'P2'=> 'required|string|max:255',
'P3'=> 'required|string|max:255',
'P4'=> 'required|string|max:255'
]);
$data = request()->except('_token');
Project::insert($data);
return back()->with('datosGuardados', 'Datos almacenados, Correctamente');
}
My Model:
class Project extends Model
{
protected $fillable = ['Sede','Alcance','Nombre','Cierre','Areas','P1','P2','P3','P4'];
protected $casts = [
'Areas' => 'array'
];
}
My migration:
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('Sede');
$table->string('Alcance');
$table->string('Nombre');
$table->date('Cierre');
$table->string('Areas');
$table->string('P1');
$table->string('P2');
$table->string('P3');
$table->string('P4');
$table->timestamps();
});
}
enter image description here
As you see the error is due to the checkbox that I don't know how to send it as a single array and store it in the database, I would appreciate any help. I was investigating and I did not find anything
$validator = $this->validate($request, [
'Sede'=> 'required|string|max:255',
'Alcance'=> 'required|string|max:255',
'Nombre'=> 'required|string|max:255',
'Cierre'=> 'required|date',
'Areas'=> 'required',
'P1'=> 'required|string|max:255',
'P2'=> 'required|string|max:255',
'P3'=> 'required|string|max:255',
'P4'=> 'required|string|max:255'
]);
// Add this line
$area = [];
foreach ($request->Areas as $key => $value) {
$area[$key] = $value;
}
// And then save in database with $area created using foreach.
Use this 'Areas.*'=> 'required', for validating the checkbox generally, moreover to insert the data under Areas, you can insert it via converting by json_encode($request->Areas);
I've read a lot of laravel tutorial to do this but none has worked for me.
This is my form
<div class="row increment control-group">
{{-- Request Item --}}
<div class="form-group col-md-4">
<input type="text" class="form-control" name="request_item[]" placeholder="Item" />
</div>
{{-- Request Description --}}
<div class="form-group col-md-4">
<input type="text" class="form-control" name="request_description[]" placeholder="Item Description" />
</div>
<div class="form-group col-md-4">
<button class="btn btn-success" id="btn-item" type="button">Add</button>
</div>
</div>
This is my controller
$request->validate([
'request_no' => 'required|max:255',
'request_date' => 'required|date', //unique:(tablename)
'request_item' => 'required|max:255',
'request_description' => 'max:255',
'request_by' => 'required|max:255',
'request_status' => 'required|max:255',
'request_scan' => 'mimes:pdf',
'created_by' => 'max:255',
'updated_by' => 'max:255',
]);
//For file uploading
$name="";
if($request->hasfile('filename')){
$file = $request->file('filename');
$name = time()."_".$file->getClientOriginalName();
$file->move(public_path(). '/images/', $name);
}
$itemArray = Input::get('request_item');
$count = count($itemArray);
for($i = 0; $i < $count; $i++){
$request= new \Trisco\IT_Request;
$request->request_no=$request->get('request_no');
$request->request_date=$request->get('request_date');
$request->request_item=$itemArray[$i];
$request->reqeust_description='';
$request->request_by=$request->get('request_by');
$request->request_status=$request->get('request_status');
$passport->request_scan = $name; //file
$request->added_by=$request->get('username');
$request->updated_by=$request->get('username');
$request->save();
}
The request_item & request_description are both string only.
Can anyone help me?
Thanks in advance.
how about you loop on your array?
$itemArray = Input::get('request_item');
foreach ($itemArray as $value) {
$request->request_item=$value;
//your code here
//request->save()
}
What is the error?
check also the spelling --
$request->reqeust_description='';
try this >>
$request->request_item= implode(',', $request['request_item']);
in cakephp
echo $this->Form->input('email', array('type' => 'email'));
will render
<div class="input email">
<label for="UserEmail">Email</label>
<input type="email" name="data[User][email]" value="" id="UserEmail" />
how to make this like that
<input type="email" name="data[User][email]" value="" id="UserEmail" class="input_class" style="some:style;" />
Just add a "class" and/or "style" argument to your options array.
echo $this->Form->input('email', array('type' => 'email', 'class' => 'input_class', 'style' => 'some:style' ));
See the the FormHelper documentation for a list of all options.
if you need only input without lable you can also try in this way
echo $this->Form->input('email', array('type' => 'email','div'=>false, 'class' => 'input_class', 'style' => 'some:style' ));
I have created one function to add users. The parameters are username,email,password and profile image.
<form name="User" method="post" action="http://192.168.1.100/filmtastic/api/users/adduser" ENCTYPE="multipart/form-data">
<table>
<tr><td><label>username:</label></td><td><input type="text" name="username"></td></tr>
<tr><td><label>password:</label></td><td><input type="text" name="password"></td></tr>
<tr><td><label>email:</label></td><td><input type="text" name="email"></td></tr>
<tr><td><label>Image:</label></td><td><input type="file" name="image"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit" /></td></tr></table>
</form>
Now in my UserController i have:
public function api_adduser() {
$this->layout = false;
$this->request->data['User']= $this->request->data;
if($this->request->data['User'] != array()) {
pr($this->request->data); die();
}
}
here i debug the data which is passed by that HTML form and optput is like
Array
(
[username] => jack roy
[password] => jack
[email] => jack#yahoo.com
[submit] => Submit
[User] => Array
(
[username] => jack roy
[password] => jack
[email] => jack#yahoo.com
[submit] => Submit
)
)
The problem is that it will not show me the image array. can you tell me how i will get that image array which suppose to look like
Array
(
[image] => Array
(
[name] => 06_01_2009_0692878001231258160_nanzig.jpg
[type] => image/jpeg
[tmp_name] => C:\wamp\tmp\php368F.tmp
[error] => 0
[size] => 81167
)
)
Maybe you simply forgot to set the form mime type to multipart/form-data. Here is my demo view, called add.ctp
<?php
echo $this->Form->create('User', array('enctype' => 'multipart/form-data'));
echo $this->Form->input('name');
echo $this->Form->password('password');
echo $this->Form->input('email');
echo $this->Form->file('image');
echo $this->Form->submit();
echo $this->Form->end();
Here is the UserController
class UserController extends AppController {
public $helpers = array('Html', 'Form');
public function add() {
pr($this->request->data);
}
}
And here is how the result looks like when you submit the form
Array
(
[User] => Array
(
[name] => admin
[password] => admin
[email] => asdf#qwerty.com
[image] => Array
(
[name] => cakephp-book.odt
[type] => application/vnd.oasis.opendocument.text
[tmp_name] => /tmp/phpKniokr
[error] => 0
[size] => 170247
)
)
)
Here is a dump of the generated HTML which you may want to use on your external page (may I ask why???). Note how many things the FormHelper adds... I wonder why you build an external view if your application is still accessing your server directly...
<form action="/test/cakedemo/user/add" enctype="multipart/form-data" id="UserAddForm"
method="post" accept-charset="utf-8">
<div style="display:none;">
<input type="hidden" name="_method" value="POST" />
</div>
<div class="input text">
<label for="UserName">Name</label><input name="data[User][name]"
id="UserName" />
</div><input name="data[User][password]" type="password" id="UserPassword" />
<div class="input text">
<label for="UserEmail">Email</label><input name="data[User][email]" type="text"
id="UserEmail" />
</div><input type="file" name="data[User][image]" id="UserImage" />
<div class="submit">
<input type="submit" value="Submit" />
</div>
</form>