Loop through the array containing collection of another array - php

I am trying to get fetch the value of date and courseName, but instead I'm getting these error exception.
loop:
array:2 [▼
0 => Collection {#284 ▼
#items: array:3 [▼
0 => {#277 ▼
+"date": "2018"
}
1 => {#279 ▶}
4 => {#282 ▶}``
]
}
1 => Collection {#283 ▼
#items: array:2 [▼
0 => {#280 ▼
+"id": 1
+"courseName": "newc1"
}
1 => {#271 ▶}
]
}
]
//tried these
#foreach ($result as $key=> $value)
#foreach ($value as $val)
{{$val->date}}
#endforeach
#endforeach
//controller
$data = array();
$date = db::table('students')->select('date')->get()->unique();
$course = db::table('courses')->select('id', 'courseName')->get();
array_push($data, $date, $course);
Needed to loop through these. Instead got
ErrorException (E_ERROR)
Undefined property: stdClass::$date

You have to control the db result if it's okey on date select also for laravel use distinct() and get must be at the end before it
//This
$date = db::table('students')->select('date')->get()->unique();
//To This
$date = db::table('students')->select('date')->distinct()->get();
if(!$date)
return $this->response;
//or
return false;
//Update
$course = DB::table('students')
->select('id','courseName', 'courses')
->groupBy('courses')//What ever you want to uniqe
->get();
For Return View Example you have to change for yourself
return $this->responseWithView("index //PAGE","success", ['courses' => $course,'anything' => $youwant]);

I'm not quite sure what you are trying to accomplish by having these collections together in one array, but here is one way to solve it:
I noticed that you are inserting collections with different attributes into an array.
$date = db::table('students')->select('date')->get()->unique();
So each item will have a date property
In the next query you select different columns:
$course = db::table('courses')->select('id', 'courseName')->get();
Here each item will have an id and courseName property
As you can see you are holding different items in the same array.
So you need to check if the date attribute exists.
#foreach ($result as $key=> $value)
#foreach ($value as $val)
#if(isset($val->date))
{{$val->date}}
#endif
#endforeach
#endforeach

Related

How to use nested loop for different arrays in laravel blade template?

I am trying to display the distance for each of my drivers on my database in my view.
When I do die or dump for $pickupDistanceAndTime2 it returns the arrays below, so I need each of the drivers to have its own distance.
0 => array:2 [▶]
1 => array:2 [▶]
2 => array:2 [▶]
return view('driverspane', [
'drivers' => $driver,
'rideDistancenTime' => $pickupDistanceAndTime2,
]);
In my view, I am looping but not getting the way I want it to appear.
#foreach ($drivers as $item)
#foreach ($rideDistancenTime as $data)
{{ $data[0] }}
#endforeach
#endforeach
But the above code keeps repeating distance and time for each of the driver, but I want each of the driver to have its own distance and time only, please see the result I am getting below attached.
The array for $driver returns
#items: array:3 [▼
0 => App\Driver {#317 ▶}
1 => App\Driver {#300 ▶}
2 => App\Driver {#281 ▶}
]
From what you did above, you are just repeating the same values for the first index of the array. You should not do a nested foreach statement.
Rather you could do it this way(using a for statement, provided that the arrays all have same number of keys:
#for ($i=0; $i<count($drivers); $i++)
{{$rideDistancenTime[$i][1]}}
#endfor
Now it should loop properly.
You can use below code to achieve your goal:
$rideDistancenTime;
#foreach ($drivers as $key => $item)
{{ $rideDistancenTime[$key] }}
#endforeach```

Get value by column name value

Ny array looks something like this:
Illuminate\Database\Eloquent\Collection {#2052 ▼
#items: array:3 [▼
0 => App\Models\NewsMeta {#2089 ▶}
1 => App\Models\NewsMeta {#2090 ▶}
2 => App\Models\NewsMeta {#2091 ▶}
]
}
If I open up the array 2:
#original: array:7 [▼
"id" => 17
"post_id" => 240231
"news_tag_id" => 5
"meta_name" => "_thumbnail_id"
"meta_value" => "240232"
"created_at" => "2020-08-06 22:34:06"
"updated_at" => "2020-08-06 22:34:06"
]
Now, I looking to get value "240232" given that I've 240231.
How do I search inside array of the object?
Something like: where post_id is 240231 get ts meta_value.
FYI: It's not eloquent or DB query.
Something like this should work:
$postId = 240231;
$metaValue = null;
foreach ($collection as $model) {
if ($model->post_id == $postId) {
$metaValue = $model->meta_value;
break;
}
}
echo $metaValue;
You could also use the collection's seach method:
$postId = 240231;
$metaValue = $collection->search(static function($model, $key) use($postId) {
if ($model->post_id == $postId) {
return $model->meta_value;
}
});
echo $metaValue;
You can use the collection firstWhere() method
$collection->firstWhere('post_id', '240231')['meta_value'] ?? null;
With the data you provided this should return 240232. In a dataset where there is no post_id = 240231 it would return null.

Creating new Associative collection and adding new items in laravel

Here's my sample code.
$users = 3 users //suppose
$leaders = new Collection();
foreach($users as $user)
{
$name = $user->name;
$username = $user->username;
$leaders->put('name', $name);
$leaders->put('username', $username);
}
dd($leaders);
This gives me the result for only one user (The 3rd user)
Collection {#274 ▼
#items: array:2 [▼
"name" => "user3"
"username" => "username3"
]
}
Because put() is replacing the values with the same keys.
I tried with this too:
$leaders->push($name);
$leaders->push($username);
But I am getting:
Collection {#274 ▼
#items: array:6 [▼
0 => "user1"
1 => "username1"
2 => "user2"
3 => "username2"
4 => "user3"
5 => "username3"
]
}
How to create different item arrays for different users?
Update #1: Got the answer.
Now trying to display the values on the view like this:
#foreach($leaders as $leader)
{{ $leader->username }}
#endforeach
Error: Trying to get property of non-object.
Is this not supposed to work this way for custom collections?
Update #2: Nevermind. Found it.
It is supposed to work this way: {{ $leader['username'] }}
Just try push method
foreach($users as $user)
{
$leaders->push([
'name' => $user->name,
'username' => $user->username,
])
}

search in values in array and get another value

I have an array like this:
//the result of `dd()` function in laravel
Collection {#401 ▼
#items: array:2 [▼
0 => {#400 ▼
+"tutorial_package_count": 2
+"tutorial_package_id": 1
+"tutorial_id": 1
}
1 => {#402 ▼
+"tutorial_package_count": 1
+"tutorial_package_id": 2
+"tutorial_id": 2
}
]
}
suppose I have a variable as $tutorial_id = 1,
now I want to get value of tutorial_package_count. at this here I want 2.
I dont want to use a loop.
In this case you can use filter method available for collections:
$filtered = $collection->filter(function ($value, $key) use($tutorial_id) {
return $value['tutorial_id'] == $tutorial_id;
});
$firstMatch = $filtered->first();

Laravel blade array foreach loop

I'm trying to acces to the parameters of an array, and not been able to.
I get an array through this eloquent statement:
$plazas = DB::table('clase_schedule')->select(['schedule_id', DB::raw('SUM(capMax)')])->groupBy('schedule_id')->get();
What returns me this array:
array:2 [▼
0 => {#465 ▼
+"schedule_id": "2"
+"SUM(capMax)": "221"
}
1 => {#464 ▼
+"schedule_id": "3"
+"SUM(capMax)": "12"
}
]
I've tryed serveral things to acces to the schedule_id and SUM(capMax) values, but nothin.
#foreach($plazas as $id => $id)
{{$id[0]}}<br/>
#endforeach
With that i get the return value of
0
1
Use alias to fetch query
$plazas = DB::table('clase_schedule')->select(['schedule_id', DB::raw('SUM(capMax) as capmax')])->groupBy('schedule_id')->get();
Blade
#foreach($plazas as $plaza)
{{ $plaza['capmax'] }}<br/>
#endforeach

Categories