I have a problem to get data using forach eloquent in laravel 6.
To get value which I want to use in foreach
$array_suspect_idBLE = Transaction::
select('id_ble')
->where('id_user',$suspect_id)
->where('updated_at','>=',$suspect_in)
->where('updated_at','<=',$suspect_out)
->pluck('id_ble');
The output
Illuminate\Support\Collection {#281
#items: array:5 [
0 => 18
1 => 6
2 => 12
3 => 16
4 => 10
]
}
and the foreach
foreach($array_suspect_idBLE as $arr){
$suspect_list = Transaction::
select('id_transaction','id_user','id_ble')
->where('id_user','<>',$suspect_id)
->where('updated_at','>=',$suspect_in)
->where('updated_at','<=',$suspect_out)
->where('id_ble', $arr->array_suspect_idBLE)
->get();
}
And the error is
message: Trying to get property 'array_suspect_idBLE' of non-object
The expected result are all id_transaction, id_user, id_ble that connect to array_suspect_idBLE 18,6,12,16,10
Any help would be appreciated.
Thank you
The problem is that the pluck() method returns an array of values. You are treating it as if it returns a collection of objects.
The solution would be to change it into this:
foreach($array_suspect_idBLE as $value) {
$suspect_list = Transaction::
select('id_transaction','id_user','id_ble')
->where('id_user','<>',$suspect_id)
->where('updated_at','>=',$suspect_in)
->where('updated_at','<=',$suspect_out)
->where('id_ble', $value)
->get();
}
Related
I have database like this
id
name
numbers
1
Kathy
5
2
Kathy
15
3
Kathy
25
4
William
5
5
William
10
and I'm trying to retrieve and save it like this
id
name
numbers
1
Kathy
5.15.25
2
William
5.10
So I made a code like this to implode and save the data
$arr_name = [];
$arr_numbers = [];
$arr_implode = [];
$data= MyDatabase::all();
foreach ($data as $data) {
if ($data->name == $data->name) {
$arr_numbers [] = $data->numbers;
} else {
$arr_numbers [] = $data->numbers;
}
$arr_implode[$data->name] = implode('.', $arr_numbers);
}
but what I get when I use dd($arr_implode); is like this, so I haven't tried saving the data in my database.
array:2 [▼
"Kathy" => "5.15.25"
"William" => "5.15.25.5.10"
]
What should I do to get the desired output? sorry, I'm not really good at arrays and stuff
You can use DB::raw to format them during the query
e.i.
use Illuminate\Support\Facades\DB;
return DB::table('table_name')->select([
DB::raw('ROW_NUMBER() OVER( ORDER BY name ) as id'),
'name',
DB::raw('group_concat(numbers SEPARATOR ".") as numbers')
])->groupBy('name')->get();
OR
Just query all the data and format the Collection
e.i.
use Illuminate\Support\Facades\DB;
$data = DB::table('table_name')->select(['id','name','numbers'])->get();
or if you have a model for that table
$data = Model::select(['id','name','numbers'])->get();
then format it the way you want
return $data
->groupBy('name')
->values()
->map( fn ( $item, $key) => [
'id' => $key+ 1,
'name' => data_get($item, '0.name'),
'numbers' => implode('.', $item->pluck('numbers')->all() )
]);
I pull in data from the database via ->get(); and want to remove a certain element based on a condition in a foreach loop so i would need something like
foreach($datas as $data) {
$data->forget(this);
}
or i count the index or whatever, but I need to find a way to remove it because the ->forget() does only work on collection instances (not on eloquent collections as it seems) , any idea?
here is the format of my log:
[{"id":14,...},{"id":15,...}...]
so i get objects in an array.
and here is the dd();
Collection {#274 ▼
#items: array:4 [▼
0 => Data {#275 ▶}
1 => Data {#276 ▶}
2 => Data {#277 ▶}
3 => Data {#278 ▶}
]
}
edit: i know its better to select only needed elements in the first place but thats not possible or rather not wished for because I am putting the results through a long carbon related check and wish to do something depending on different scenarios.
edit 2:
I can remove with forget(1..2 and 3) but not with 0, it tells me undefined offset?
Maybe map is what your are looking for if you want to remove a specific property in the item:
$datas = $datas->map(function ($data) {
// modify $data here, unset unset($data->property);
return $data;
});
See documentation here
Or filter to remove the whole item:
$datas = $datas->filter(function ($data) {
return ($data->property == 'bar') ? true : false;
});
See documentation here
I'm using Eloquent ORM in Laravel. There is 2 model, User and Trans. User hasMany Trans, and Trans belongsTo User. The problem is when I'm using query where, it doesn't work.
I tried using ->get() in the last code, it still doesn't work. I tried using ->all() in the last code it still doesn't work. I tried whereIn, it still doesn't work.
User Model
public function trans()
{
return $this->hasMany('App\Trans', 'user_id');
}
Trans Model
public function user ()
{
return $this->belongsTo('App\User','user_id');
}
Controller
$trans = Auth::user()->trans->where('date', $date);
I want the output is based on query where the date is based on user input, when I delete ->where , it works and the output like this.
Collection {#797 ▼
#items: array:13 [▼
0 => Trans {#783 ▶}
1 => Trans {#784 ▶}
2 => Trans {#785 ▶}
3 => Trans {#786 ▶}
]
}
try to change like that
Auth::user()->trans->where('date', $date);
to
Auth::user()->trans()->where('date', $date)->get();
note : if you want to get only property then u get property without
pointer but if you want to used another method then must used(add)
pointer(->).
I have models: Partido and Coalicion related in a many to many relationship.
I need to know when two or more Coalicion has the same Partido related.
Hope I have explained myself.
Edit 1:
Model:
class Coalicion extends Model
{
public function partidos()
{
return $this->belongsToMany(Partido::class);
}
}
Let's say users selected some elements from a select input and I grabbed them in an array and send them to the controller.
...
public function example(Request $request)
{
$coaliciones = $request->coaliciones;
foreach ($coaliciones as $c) {
$coalicion = Coalicion::find($c);
# Here we have a list of Coalicion model in a loop
# Let's say the first iteration I can see the relationship
dump($c->partidos);
}
}
This for example give me the following answer at the browser:
Collection {#1 ▼
#items: array:2 [▼
0 => Partido {#271 ▶} #This is Partido with id 1
1 => Partido {#268 ▶}
]
}
Collection {#2 ▼
#items: array:3 [▼
0 => Partido {#279 ▶}
1 => Partido {#280 ▶}
2 => Partido {#283 ▶} #This is Partido with id 1
]
}
I need to know when the item 0 of the first Collection and the item 2 of the second Collection are the same.
I kinda found a way but I don't know if it's the correct or best approach.
In Coalicion model I add the following function:
public function partidosId()
{
return $this->partidos->pluck('id');
}
With this I can get only the id's from the relations, then in the controller I created an empty array() and fill it with all my ids with a foreach loop, then, finishing up I evaluated if the id's in the now filled array are unique, if the unique values are less than the length of the array then some models have the same relation (I don't know which but It's a start), e.g.:
public function example(Request $request)
{
$coaliciones = $request->coaliciones;
$allItems = [];
foreach ($coaliciones as $c) {
$coalicion = Coalicion::find($c);
$allItems = array_merge($allItems, $coalicion->partidosId()->toArray());
}
if (count(array_unique($allItems))<count($allItems)) {
dd("Duplicates");
}else{
dd("Uniques");
}
}
If anyone find a better way or a way to know which are the ones with the duplicate relation please let me know
$item_ids = Cookie::get('name');
if(!is_array($item_ids)){
$items = Item::find($item_ids)->get();
}
$items = DB::table('items')
->whereIn('id', $item_ids )
->get();
dd($items);
$item_ids in dd($item_ids)
array:5 [▼
0 => "2"
1 => "2"
2 => "2"
3 => "4"
4 => "6"
]
As you can see in $item_ids 2 was repeated 3 times and I only get one. How to repeat it also 3 times. Is there any Query Builder could do this?
Query builder looks in the database for id = 2 with the whereIn method. There is only one item with id 2 in the database so it only returns one record.
However you can loop through your all your ids again and pull these from your database results. Do this for every id so you get multiple results for same id.
Something like this:
$item_ids = Cookie::get('name');
if(!is_array($item_ids)){
$items = Item::find($item_ids)->get();
}else{
$items = [];
$dbItems = Item::whereIn('id',$item_ids)->get();
foreach($item_ids as $id){
$items[] = $dbItems->first(function ($key, $value) use ($id) {
return $value->id == $id;
});
}
}
dd($items);