This question already has answers here:
Laravel 5.4 field doesn't have a default value
(10 answers)
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
When I am importing data from excel after getting the file and on the step of saving data to db it showing field doesn't have value from excel file. Where as all the fields are filled with data.
i am using laravel excel
LeadImport Code
return new lead([
'name' => $row[0],
'leadid' => $row[1],
'number' => $row[2],
'city' => $row[3],
'state' => $row[4],
'address' => $row[5],
'pincode' => $row[6],
'type' => $row[7],
]);
Controller Code
Excel::import(new LeadsImport, $request->file('file'));
Error
SQLSTATE[HY000]: General error: 1364 Field 'leadid' doesn't have a default value (SQL: insert into `leads` (`updated_at`, `created_at`) values (2022-01-14 19:24:21, 2022-01-14 19:24:21))
The leadid don't have any assigned value and it doesn't have any default value assigned. What is $row[1] returning? I assume row[1]'s value is null, that's why you actually get this error. dd() it and tell us what it returns.
in your excel file you must have cells:
name, leadid, number, city, state, address, pincode, type
and in LeadImport just:
return new lead([
'name' => $row['name'],
'leadid' => $row['leadid'],
'number' => $row['number'],
'city' => $row['city'],
'state' => $row['state'],
'address' => $row['address'],
'pincode' => $row['pincode'],
'type' => $row['type'],
]);
Related
I have a name field on my Client model which must be unique. For the store method I have the following rules:
array (
'token' => 'string|max:250',
'directory' => 'max:250',
'name' => 'required|string|max:250|unique:clients',
)
For the update method, I have amended this ruleset to ignore the current ID to avoid a duplicate being identified:
$rules['name'] = $rules['name'] . ',id,' . $id;
This produces the following ruleset (for record ID 105):
array (
'token' => 'string|max:250',
'directory' => 'max:250',
'name' => 'required|string|max:250|unique:clients,id,105',
)
When the update method is executed, the The name has already been taken. response is returned during the validation.
I realise this is a well-discussed topic, but believe I am conforming to the correct approach. Can someone point out the error in my logic, please?
Note: This is running on Laravel 5.8.
array(
'token' => 'string|max:250',
'directory' => 'max:250',
'name' => [
'required', 'string', 'max:250',
\Illuminate\Validation\Rule::unique(Client::class, 'email')->ignore($this->id)
]
)
i've some problem with my code.
$id = DB::table('sn_project_details')->insertGetId([
'emp_name' => $request->emp_name,
'emp_id' => $request->emp_id,
'department' => $request->department,
'submit_date' => $request->submit_date,
'total_amount' => $request->total_amount,
'project_tittle' => $request->project_tittle,
'project_desc' => $request->project_desc,
'scope' => $request->scope,
'file' => $request->file
]);
//Update Table
\DB::table('sn_project_details')
->where('project_id', $id)
->update(['doc_ref' => "ID_",$request->scope,"_",$id]);
return redirect('/user')
I want to update column doc_ref with example value ID_Scope_220,
ID_ its fixed value. Scope from textbox scope. 220 from #emp_id.
but when i execute this code, update query not working properly.
can someone help? thx
use dot instead of comma
->update(['doc_ref' => "ID_".$request->scope."_".$emp_id]);
I'm trying to get just value of id column on a table but it returns
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '[{"id":1}]' for column 'id_jenis' at row 1 (SQL: insert into `pesanan` (`name`, `telpon`, `alamat`, `id_jenis`, `jenis`, `do`, `updated_at`, `created_at`) values (Pradita Candrani, 0813, Jalan Sunan Ampel Kasin, [{"id":1}], Cuci Basah Standar, None, 2019-11-27 12:18:35, 2019-11-27 12:18:35))
Here it is my code on Controller
public function pesan(Request $request){
$harga = Harga::select('id')->where('nama',$request->jenis)->get();
Pesanan::create([
'name' => $request->nama,
'telpon' => $request->telpon,
'alamat' => $request->alamat,
'id_jenis' => $harga,
'jenis' => $request->jenis,
'do'=>$request->do
]);
return redirect('/pesanan');
}
how can I fix this? Please help
You're getting object now and passing it to id_jenis directly. use first() instead of get(). and pass the $harga->id in id_jenis.
$harga = Harga::select('id')->where('nama',$request->jenis)->first();
Pesanan::create([
'name' => $request->nama,
'telpon' => $request->telpon,
'alamat' => $request->alamat,
'id_jenis' => $harga->id,
'jenis' => $request->jenis,
'do'=>$request->do
]);
If you want to store multiple ids in id_jenis then use pluck.
$harga = Harga::where('nama',$request->jenis)->pluck('id')->toArray();
Here you'll get multiple ids in array. so use json_encode to store JSON in db as below.
Pesanan::create([
'name' => $request->nama,
'telpon' => $request->telpon,
'alamat' => $request->alamat,
'id_jenis' => json_encode($harga),
'jenis' => $request->jenis,
'do'=>$request->do
]);
$harga = Harga::select('id')->where('nama',$request->jenis)->get();
After this line, write
\Log::info(['Harga', $harga]);
and check the latest file and error in /storage/logs/laravel.log
Welcome to the wonderful world of debugging
I have a Model which contains many foreign keys. One of those foreign keys must be a unique value.
My validation rules are the following ones:
$data['rules'] = [
'address' => 'required|string',
'buyer_id' => 'required|exists:buyers,id',
'buyer_name' => 'required|string',
'date' => 'required|date',
'email' => 'required|email',
'identification_photo' => 'required|string',
'invoice' => 'string|required',
'middleman_id' => 'nullable|exists:middlemen,id',
'price' => 'required|numeric|between:1,99999999999999999999999999.9999',
'property_id' => 'required|exists:properties,id|unique:reservations,property_id',
'purchase_receipt' => 'required|string',
'rfc' => array(
'required',
'regex:/^([A-Z,Ñ,&]{3,4}([0-9]{2})(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])[A-Z|\d]{3})$/'
),
'tier_id' => 'nullable|exists:tiers,id',
'user_id' => 'required|exists:users,id',
];
The one that I have trouble is property_id. This must be unique in the current table which is reservations.
In order to ignore that validation when I make an update, I'm adding this line of code before calling the Validator:
$book['rules']['property_id'] .= ",{$item->property_id}";
And when I do a Log::info of all my rules, I got the following line: 'property_id' => 'required|exists:properties,id|unique:reservations,property_id,4',
But I keep receiving the error. Am I doing something wrong?
The error is on this line:
$book['rules']['property_id'] .= ",{$item->property_id}";
Instead of passing the id of the foreign key you want to ignore, you have to give the current model ID in order to ignore that validation for a specific item.
$book['rules']['property_id'] .= ",{$item->id}";
With this you tell that for your Model with id = x, ignore that specific validation. To understand it in a better way, you are telling that for this validation, ignore the validation of the property only for the record with id equals to the $item->id.
I think the line of code you are looking for is
'property_id' => 'required|exists:properties,id|unique:reservations,property_id,'.$request->id',
This ignores the current row you are updating while also validating your property_id
I am using laravel backpack for my one project. I am stuck in one place where in the list of data the description column showing limited words in the column. I want to show full description in list column itself.
$this->crud->addColumn([
'label' => "Message",
'type' => 'text',
'name' => 'message'
]);
You should be able to do that using 'limit' => 20000. Here's this field's definition from the docs:
[
'name' => 'name', // The db column name
'label' => "Tag Name", // Table column heading
// 'prefix' => "Name: ",
// 'suffix' => "(user)",
// 'limit' => 120, // character limit; default is 80;
],
Hope it helps. Cheers!