Laravel, putting different column values in one variable - laravel

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;
}

Related

check data that is not in array with 2 value matches

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
}

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.";
}

get all rows in single query with multiple id

I have a asset_request table with fields id and request_id.
I want to select multiple rows with specific ids.
$ids = $request->ids // 5,6
I want to select only rows with ids of 5 and 6 in request table
$ids = $request->ids;
$asset_request = asset_request::whereIn('id',array($ids))->first(); //gets only 6th row.
I need to get all rows matching the given ids.
To clarify after a chat discussion with the Op:
The Op was passing back a string request, therefore, the Op needed to change the following:
$id = $request->id;
$ids = str_split(str_replace(',', '', $id));
$asset_request = asset_request::whereIn('id', $ids)->get();
First you are calling the first method which will return only the first row matched.
You need to call get method to get all rows matched.
Secondly if you are sending ids as a comma separated string you need to convert it to array using explode.
$ids = $request->ids;
$asset_requst = asset_request::whereIn('id', explode(",", $ids))->get();
DB::table('asset_request')
->whereIn('id', (array) $request->ids)
->get();
or
TableModel::whereIn('id', (array) $request->ids)->get();

Active record of not in

$this->db->query('DELETE FROM default_model WHERE cat_id NOT IN (SELECT id FROM default_category)');
the active record format for this query
$query = $this->db->select('id')
->get('category')->result();
$this->db->where_not_in('cat_id',$query )
->delete('model');
I tried a lot but could not do it. How to pass $query
The query results are returned as standard objects. Therefore you need to fetch the ids from the returned objects in a non associative array before passing that array to the delete query.
Like this:
$result = $this->db->select('id')->get('category')->result();
$ids = [];
foreach($result as $result)
$ids[] = $result->id;
$this->db->where_not_in('cat_id', $ids)->delete('model');

Advanced whereNotNull statement in Laravel

Is it possible to do the following in Laravel 4? ...
DB::table('myTable')
->select(DB::raw($columnNames))
->whereNotNull(function($query) use($columns) {
foreach ($columns as $column) {
$query->whereNotNull($column);
}
})
->get();
If I have the following table:
table: myTable
id | name | age | weight
======================================
1 Jane NULL 150
2 NULL 12 80
3 Bob NULL NULL
4 John 22 120
5 Cody NULL NULL
If $columns is [age, weight] and $columnNames is 'age, weight', then applying the above whereNotNull statement, I would expect output like this:
age | weight
===================
NULL 150
12 80
22 120
How can I get this done?
UPDATE:
The condition is to return all rows where the selected columns are not ALL null. So a whereNotNull clause must be applied to each (selected) column in each row. If all columns are NULL, then whereNotNull will return false and that row shouldn't be part of the results. So only rows which have AT LEAST one non-NULL value should be returned.
If those are the only where's you don't even need a nested where. Important: orWhereNotNull instead of whereNotNull so only one column has to be not NULL.
$query = DB::table('myTable')->select(DB::raw($columnNames));
foreach($columns as $column){
$query->orWhereNotNull($column);
}
$result = $query->get();
Also (at least with your example) you don't need a separate variable $columnNames since select will accept an array of column names.
$query = DB::table('myTable')->select($columns);
If you happen to need more where conditions (especially ones with AND) you need a nested where:
$query = DB::table('myTable')->select(DB::raw($columnNames));
$query->where(function($q) use ($columns){
foreach($columns as $column){
$q->orWhereNotNull($column);
}
});
$result = $query->get();
A nested where will put ( ) around the where clauses. That means instead of:
WHERE age IS NOT NULL OR weight IS NOT NULL AND foo = 'bar'
You get:
WHERE (age IS NOT NULL OR weight IS NOT NULL) AND foo = 'bar'
Try using where() as the wrapping method. This would only show records that have both an age AND a weight.
DB::table('myTable')
->select(DB::raw($columnNames))
->where(function($query) use($columns) {
foreach ($columns as $column) {
$query->whereNotNull($column);
}
})
->get();
To show any records that have an age OR a weight, use orWhereNotNull() in the loop.
I see no reason why a loop would not work, because you are effectively doing this:
$query = $query->whereNotNull('age');
$query = $query->whereNotNull('weight');
$results = $query->get();

Resources