I am using the simple form and array input like this (not dynamic)
<input type="text" name="pre_ref_position[]" id="pre_ref_position">
<input type="text" name="pre_ref_position[]" id="pre_ref_position">
Form post with ajax and return data like this;
[
0:{pre_ref_position:'example1'}
1:{pre_ref_position:'example2'}
]
So I want to save this data with eloquent but how can I handle this array and save each row in the database?
Assuming you receive your array in a variable called $refValin your controller
Then use the following code:
$refArr = [];
foreach($refVal as $key => $val)
{
$now = Carbon::now();
//ref_pos in the array below refers to the field name in your database.
$refArr[] = [ 'ref_pos' => $val['pre_ref_position'] ,'created_at' => $now, 'updated_at' => $now ];
}
//assuming model name is `Position`
Position::insert($refArr);
Related
Laravel: 8.51
Nova: 3.24
PHP Version: 7.4.20
I have problem with trying to solve this situation:
I have a action that uses a KeyValue to handle array of pairs: "product_id" and "amount"
field looks like:
KeyValue::make(__('Order details'), 'details')
->rules('json'),
also in model i added cast:
protected $casts = [
'details' => 'array',
];
I convert it to json and store in database like that
foreach ($fields->details as $key => $value)
{
$order_details[$key] = $value;
}
//(...)
DB::table('orders')->InsertGetId([
'details' => json_encode($order_details),
]);
finnaly my json field in database looks like:
{"1":"6","2":"7","3":"8"}
To this point everything works great.
Now I want to change what I see as product_id (in above example 1, 2, 3) to be from other resource like belongsTo('products')
I was considering how to be able to use BelongsTo or Select in key part of KeyValue field.
I tried few ways to display it somehow in stack field like:
Stack::make('Details', [
Text::make('details', function () {
return $this->details($key); // how to properly read $key from json in fields?
}),
Text::make('amount',function () {
return $this->details($value); // how to properly read $value from json in fields?
}),
]),
or
BelongsTo::make(__('Product'), 'user', Products::class)
->withMeta([
'belongsToId' => $this->details($key), // again i dont know how to properly read $key here from json.
]),
How can I save an array of data into individual rows in Laravel ?
Here's how my UI looks like
once i click the save button in the controller Store function gets the data
public function store(Request $request, $venue, CustomProduct $customProduct)
{
dd($request->Availability);
}
This is how the data looks like
I'm wondering how can i save this to my database ?
This is how my database table looks like
Please suggest me a way that i can save this to my table
Use this example:
$days = ["monday","thursday","wednesday","tuesday","friday","saturday","sunday"];
forach($request->Availability as $key => $value){
CustomProductAvalibility::create([
'day_id' => array_search($key), // array_search($key) returns day id
'starts_at' => $value["start"],
'ends_at' => $value["end"],
'custom_product_id' => '...' // your custom product id
]);
}
I'm trying to update multiple rows based on its unique field taskCode
and will update the field of taskWeight from tbl_projtask
I have this 2 variable
$request->dataWeight
This variable is coming from ajax request that contains something like this 95,75,65 a value that is separated by commas.
$request->dataWeightAttr
This variable is coming from ajax request that contains something like this TaskCode1, TaskCode2, TaskCode3
In my MainController.php
I have this code
$myString = $request->dataWeightAttr;
foreach($myString as $value){
DB::table('tbl_projtask')
->where('taskCode', $value)
->update([
'taskWeight'=> $request->dataWeight,
'by_id'=> auth()->user()->id,
'updated_by'=> auth()->user()->name,
'updated_at' => now()
]);
}
As you can see in my update code
I used request->dataWeightAttr to find which rows should be updated and $request->dataWeight the value for specific taskCode
Am I doing this right?
Convert your string to an Array
$dataWeight = explode(',',$request->dataWeight); // convert to array
$dataWeightAttr = explode(',',$request->dataWeightAttr); // convert to array
Assuming you have related values in both array.
foreach($dataWeightAttr as $key => $value){
DB::table('tbl_projtask')
->where('taskCode', $value)
->update([
'taskWeight'=> $dataWeight[$key],
'by_id'=> auth()->user()->id,
'updated_by'=> auth()->user()->name,
'updated_at' => now()
]);
}
You can use whereIn instead of foreach if you want to update same fields but different id.
DB::table('tbl_projtask')
->whereIn('taskCode', $request->dataWeightAttr)
->update([
'taskWeight'=> $request->dataWeight,
'by_id'=> auth()->user()->id,
'updated_by'=> auth()->user()->name,
'updated_at' => now()
]);
I need help on storing associative array into database columns in codeigniter.
I have a input field named "vehicle[]", actually this one this consist of 8 fields like Make, Model, color etc.
also the user can add one more more vehicles using "Add another" option which the input fields same as vehicle[]
here's my code for Model of codeigniter
public function vehicle($new){
$new = $this->input->post('vehicle');
$data = array(
'vehicle_number' => json_encode($new),
'make' => json_encode($new),
'color' => json_encode($new),
'color' => json_encode(model),
);
$this->db->insert('vehicles_tbl', $data);
this way the all he data stored in a one column of the database, how can i store each value in related column of the database.
My controller
$this->vehicle_model->vehicle($new);
You should code like this
//view
< input type="text" name="vehicle_number[]">
< input type="text" name="make[]">
< input type="text" name="color[]">
< input type="text" name="model[]">
// Model
public function vehicle(){
$new = $this->input->post('vehicle_number');
for ($i=0; $i <count($new) ; $i++) {
$data[] = array(
'vehicle_number' => $_POST['vehicle_number'][$i],
'make' => $_POST['make'][$i],
'color' => $_POST['color'][$i],
'model' =>$_POST['model'][$i],
);
}
return $this->db->insert_batch('vehicles_tbl', $data);
I am grabbing the values from the parameters in the URL domain.com?para=value in the controller using
Input:all()
Is there a way to add more values to the Input:all() in the controller?
I have tried $_POST['para'] = "value and $_GET['para'] = "value" but no luck.
I've gone through the docs but cannot find anything.
Thanks
More Info
Here is what is returned
{
"param_1" => "value",
"param_2" => "value",
"param_3" => "value",
}
I would like to add another param into the Input:all()
{
"param_1" => "value",
"param_2" => "value",
"param_3" => "value",
"NEW_PARAM" => "NEW VALUE",
}
In laravel 5, you can use
Request::merge(['New Key' => 'New Value']);
or by using request() helper
request()->merge(['New Key' => 'New Value']);
You should never need to add anything to Input. You should assign Input like so...
$arr = Input::all();
And then add to $arr like so...
$arr['whatever'] = 'whatever';
If you need to get that value in another part of the stack, try to pass it through yourself.
Cheers.
Best way to add data into the input::all() in laravel.
Solution 1
add Request package at the top of the page.
use Request;
Then add following code into your controller.
Request::merge(['new_key' => 'new_value']);
Solution 2
You can assign all the Input::all(); to a variable and then you can add new data to the variable. Like below.
$all_input = Input::all();
$all_input['new_key'] = 'new_value';
Add an input value on the fly inside a request instance
public function store(Request $request){
$request->request->add(['new_key' => 'new_value']);
}
Remove data from an input value on the fly inside a request instance
public function store(Request $request){
$request->request->remove('key');
}