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
Related
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```
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
in this result:
LengthAwarePaginator {#251 ▼
#total: 2
#lastPage: 1
#items: Collection {#246 ▼
#items: array:2 [▼
0 => ProductCategories {#247 ▼
...
#attributes: array:6 [▼
"id" => 1
"category_name" => "test"
"lang" => "fa"
"images" => "{"images":{"original":"\/uploads\/post_images\/2017\/1512736029.jpeg","300":"\/uploads\/post_images\/2017\/300_1512736029.jpeg","600":"\/uploads\/post_images\/2017\/600_1512736029.jpeg","900":"\/uploads\/post_images\/2017\/900_1512736029.jpeg"},"thumbnail":"\/uploads\/post_images\/2017\/300_1512736029.jpeg"} ◀"
"created_at" => "2017-12-08 10:47:56"
"updated_at" => "2017-12-08 12:27:10"
]
#original: array:6 [▼
"id" => 1
"category_name" => "test test"
"lang" => "fa"
"images" => "{"images":{"original":"\/uploads\/post_images\/2017\/1512736029.jpeg","300":"\/uploads\/post_images\/2017\/300_1512736029.jpeg","600":"\/uploads\/post_images\/2017\/600_1512736029.jpeg","900":"\/uploads\/post_images\/2017\/900_1512736029.jpeg"},"thumbnail":"\/uploads\/post_images\/2017\/300_1512736029.jpeg"} ◀"
"created_at" => "2017-12-08 10:47:56"
"updated_at" => "2017-12-08 12:27:10"
]
...
}
1 => ProductCategories {#248 ▶}
]
}
which i get from this query:
$categories = ProductCategories::paginate(10);
dd($categories);
i'm trying to access to "images" column data such as thumbnail and 300 or etc, when i use foreach this data and show that on table i can't access to them, for example:
{{$categories->images->thumbnail}}
but i get Undefined property error and i can't show that
How about you just cast that field to an array ...
class ProductCategories ...
{
protected $casts = [
'images' => 'array',
];
...
}
#foreach ($categories as $category)
{{ $category->images['thumb'] }}
#endforeach
Laravel 5.5 Docs - Eloquent - Mutators - Array and Json Casting
The variable $categories is a Collection, so you need to get each individual object to get its data. For this you'll need to use a foreach.
#foreach($categories as $category)
{{ $category->images }}
#endforeach
And to have access to thumbnail you'll need to decode the json string that you have in images. For that, you can use json_decode($category->images) function. This will return an array with thumbnail as key.
If you want to make it right, you should keep image related data in a separate table and use a relationship between category and image:
public function images()
{
return $this->hasMany(Image::class);
}
But since you're keeping images data as JSON string, the first thing you want to do is to cast it to array. Add this to the model:
protected $casts = [
'images' => 'array',
];
Then in Blade view:
#foreach ($categories as $category)
{{ $category->images['thumbnail'] }}
#endforeach
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,
])
}
I am trying to pass a JSON to my view
With this code:
Route::get('json', function() {
$path = storage_path() . "/json/dish.json";
$json = json_decode(file_get_contents($path), true);
return View::make('pages.json')->withJson($json);
});
With {{dd($json)}}
I receive this:
array:1 [▼
"dish" => array:297 [▼
0 => array:2 [▶]
1 => array:2 [▶]
2 => array:2 [▶]
3 => array:5 [▶]
...
When I try to display the content of my $json with:
#foreach($json["dish"] as $key => $item)
{{$item}}
#endforeach
I get this error message:
htmlentities() expects parameter 1 to be string, array given (View: /Users/beyerdynamic/Documents/Developer/dev1/resources/views/pages/json.blade.php)
What am I doing wrong here?
By default Laravel tries to escape any variables before input. If you want to avoid it use {!! $item !!}. However, $item is array and it will not help you to show proper values. You will get 'Array' as output. If you want to display correct data use {{ $item['your_key'] }}.