check data that is not in array with 2 value matches - laravel

I have 2 arrays and I want to check those arrays with 2 value matches,
if those 2 value not matches I want to insert into database.
Code I have tried :
$data1 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa']};
$data2 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa'],['reg_no'=>12345,'name'=>'Roger']};
foreach($data1 as $d1)
foreach($data2 as $d2)
if($d1['reg_no'] != $d2['reg_no'] && $d1['name'] != $d2['name'])
//insert into database
// this not work because it will enter all data that not match
endif
endforeach
endforeach

you can use FirstOrNew Or FirstOrCreate in laravel.
like insert if not exist
$students = User::firstOrNew(['student_id' => $request->student_id, 'course_code' => $request->course_code]);
$students->foo = $request->foo;
$students->save();

$data1 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa']};
$data2 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa'],['reg_no'=>12345,'name'=>'Roger']};
$data = array_uintersect($data1, $data2, "strcasecmp");
if($data['reg_no'] != $data1['reg_no'] && $data2['reg_no']) {
// insert
}

Related

Codeigniter update_batch in Core PHP

everyone.
In the codeigniter there is update_batch function by using it we are able to bulk update multiple rows.
$this->db->update_batch('table_name', $update_data_array, 'where_condition_field_name');
I want similar functionality in core PHP in one function or in one file. Is there any workaround?
Is there any way to extract update_batch function from Codeigniter in one file/function?
I tried to extract this function but it is very lengthy process and there will be many files / functions should be extracted.
Please help me in this regard
Thanks in advance
You can also insert multiple rows into a database table with a single insert query in core php.
(1)One Way
<?php
$mysqli = new mysqli("localhost", "root", "", "newdb");
if ($mysqli == = false) {
die("ERROR: Could not connect. ".$mysqli->connect_error);
}
$sql = "INSERT INTO mytable (first_name, last_name, age)
VALUES('raj', 'sharma', '15'),
('kapil', 'verma', '42'),
('monty', 'singh', '29'),
('arjun', 'patel', '32') ";
if ($mysqli->query($sql) == = true)
{
echo "Records inserted successfully.";
}
else
{
echo "ERROR: Could not able to execute $sql. "
.$mysqli->error;
}
$mysqli->close();
? >
(2)Second Way
Let's assume $column1 and $column2 are arrays with same size posted by html form.
You can create your sql query like this:-
<?php
$query = 'INSERT INTO TABLE (`column1`, `column2`) VALUES ';
$query_parts = array();
for($x=0; $x<count($column1); $x++){
$query_parts[] = "('" . $column1[$x] . "', '" . $column2[$x] . "')";
}
echo $query .= implode(',', $query_parts);
?>
You can easily construct similar type of query using PHP.
Lets use array containing key value pair and implode statement to generate query.
Here’s the snippet.
<?php
$coupons = array(
1 => 'val1',
2 => 'va2',
3 => 'val3',
);
$data = array();
foreach ($coupons AS $key => $value) {
$data[] = "($key, '$value')";
}
$query = "INSERT INTO `tbl_update` (id, val) VALUES " . implode(', ', $data) . " ON DUPLICATE KEY UPDATE val = VALUES(val)";
$this->db->query($query);
?>
Alternatively, you can use CASE construct in UPDATE statement. Then the query would be something
like:
UPDATE tbl_coupons
SET code = (CASE id WHEN 1 THEN 'ABCDE'
WHEN 2 THEN 'GHIJK'
WHEN 3 THEN 'EFGHI'
END)
WHERE id IN(1, 2 ,3);

laravel eloquent union, join 2 columns in the same table

I have the following columns in the same table
Column 1
--------
1
2
3
Column 2
--------
4
5
6
I want it to be displayed like
Columns
--------
1
2
3
4
5
6
You can use unionAll
$first = DB::table('yourTable')
->select('column1 as res');
$result = DB::table('yourTable')
->select('column2 as res')
->unionAll($first)
->get();
You can make the Union of your data by taking out separately and merge it after. You can use union() as well as merge(), One thing to mention is that both data must have same skelton means structure.
use DB;
public function yourFunction(){
$data_1 = DB::table('table_name')->get();
$data_2 = DB::table('table_name')->get();
//your final data
$final_data = $data_1->union($data_2);
}
Assuming values are in the same row:
$data = DB::table('table_name')->select('column1', 'column2')->get();
$c = count($data);
if ($c) {
$col1 = [];
$col2 = [];
foreach ($data as $k => $d) {
$col1[] = $d->column1;
$col2[$c + $k] = $d->column2;
}
$result = array_merge($col1, $col2);
foreach ($result as $value) {
echo $value."\n";
}
} else {
echo "No data in table.";
}

How I can get 0 index value in codeigniter?

Array ( [0] => 18 [1] => 1 )
how i can get only 0 index value?. I am using this code in codeigniter. Can any one help?. Is this possible with for each loop so it access all indexes but show only zero index?
foreach($m as $m)
{
echo $m->['0'];
}
If you want only first index then you don't need to foreach loop
Just write:
echo $m[0];
And if you want all index of array then:
foreach ($m as $key => $value) {
echo $key;
}
Try this:
print_r($m[0]);
or
foreach($m as $m)
{
echo $m['0'];
}
Following is the code to get value of index 0
//$array variable declared containing multiple values
$array=array('0' =>"first value",'1' =>"second value",'2' =>"third value" );
echo "<pre>";
print_r($array); // helps in printing the value key and value
//iterating through each values in the array
foreach ($array as $key => $value) {
if($key==0) //checks if key is 0
{
echo $value; //prints the value in key 0
}
}

Check if a value is present in a comma separated varchar string

I have a record "id_zone" in database that store cites id like this $abilitato->id_zone = '1, 2, 3, 9, 50'
From laravel i need to check if a value passed from url is present in this field. The following work only if i have a single value
$abilitato = \App\Models\Servizi::where('id', $servizio)->('id_zone', $verifica->id_citta)->first();
if(!empty($abilitato) && empty($verifica->multicap)) {
$json = ['CAP' => '1' , 'DEB' => $cap ];
}else{
$json = ['CAP' => '0' , 'DEB' => $cap];
}
i need to check if
If you want to know if a value is in your array you can use this functión to know it.
here is a example:
// First convert your string to array
$myString = $abilitato->id_zone; // Here your string separate with comas
$array = explode(',', $myString); // Here convert it to array
$value = 7; // Here you put the value that you want to check if is in array
// Check if exist a value in array
if (in_array($value, $array)){
echo "Exist in array";
}else{
echo "No exist in array";
}
This is the documentation of these functions: explode , in_array
Regards!

Laravel, putting different column values in one variable

I have this table:
Friends
id | friendid
10 | 15
12 | 10
10 | 13
and i want to put all values (either from id or friendid columns) in one variable. is it posible?
i have this code:
$id = 10;
$id = DB::table('friends')->where('id', $x)->orWhere('friendid', $x)->lists('friendid');
but this line of code only returns values from 'friendid' column and what i want is to store values from 'friendid' and 'id' columns.
You can pass a second argument to lists, which will give set the keys of the resulting array:
$query = DB::table('friends')->where('id', $x)->orWhere('friendid', $x);
$results = $query->lists('friendid', 'id);
If you want it all as values of a single array, use this:
$query = DB::table('friends')->where('id', $x)->orWhere('friendid', $x);
$ids = [];
foreach ($query->select('id', 'friendid')->get() as $record) {
$ids[] = $record->id;
$ids[] = $record->friendid;
}

Resources