Hello StackOverflow community, I am working with laravel and i created this collection
$headquarters = collect([
[
'headquarter' => 'Leon',
'offers' => [
[
'name' => 'Name1',
'slug' => 'Name1'
], [
'name' => 'Name2',
'slug' => 'Name2'
]
]
],[
'headquarter' => 'Granada',
'offers' => [
[
'name' => 'Name3',
'slug' => 'Name3'
],[
'name' => 'Name4',
'slug' => 'Name4'
],[
'name' => 'Name5',
'slug' => 'Name5'
]
]
]
]);
I want to filter this collection by headquarter and offer slug in order to get a Single offer
Right now i am trying using filter
$offer = $this->headquarters()
->filter(function($hq) use ($headquarter, $slug) {
return $hq['headquarter'] == $headquarter && $hq['offers']['slug'] == $slug;
});
But with no success.
Thanks for any advice
You can get all offers in specific headquarter with this code
$this->headquarters()->where('headquarter', 'Leon')[0]['offers'][0];
Then for each all offers
foreach ($this->headquarters()->where('headquarter', 'Leon')[0]['offers'] as $offer) {
print_r($offer);
}
or try this code
$offer = collect($this->headquarters()->where('headquarter', $headquarter)
->first()['offers'])
->where('slug', $slug)->first();
You $hq['offers'] is an array, you should access as $hq['offers'][0]['slug'].
$offer = $this->headquarters()
->filter(function($hq) use ($headquarter, $slug) {
return $hq['headquarter'] == $headquarter && in_array($slug, array_column($hq['offers'], 'slug'));
});
Related
I have the code below but it showing an error "Cannot use empty array elements in arrays".
It seems that the issue is in this line }), collect(json_decode($post['comments'], true))->map(function ($comment) {
Code:
'data' => [
'comments' =>
collect(json_decode($configs['comments'], true))->map(function ($comment) {
return [
'title' => $comment['attributes']['title'],
'message' => $comment['attributes']['message'],
];
}), collect(json_decode($posts['comments'], true))->map(function ($comment) {
return [
'title' => $comment['attributes']['title'],
'message' => $comment['attributes']['message'],
];
}),
]
If we simplify your code it seems like this;
'data' => [
'comments' =>
collect(),
collect()
]
It is not a valid syntax. You can try like this;
$comments = collect(json_decode($configs['comments'], true))->map(function ($comment) {
return [
'title' => $comment['attributes']['title'],
'message' => $comment['attributes']['message'],
];
});
$postComments = collect(json_decode($posts['comments'], true))->map(function ($comment) {
return [
'title' => $comment['attributes']['title'],
'message' => $comment['attributes']['message'],
];
});
'data' => [
'comments' => $comments->merge($postComments)->toArray()
];
I'm trying to setup different user types and their respective permissions in my AppServiceProvider.php in my project, and I get the error
explode() expects parameter 2 to be string, object given
Nowhere in my code do I have an explode() at least that I can see. Before adding the Inertia::share(function(){}) there was no such error.
This is my code:
public function register()
{
Inertia::version(function () {
return md5_file(public_path('mix-manifest.json'));
});
Inertia::share(function () {
$auth = null;
if (Auth::user()) {
$perms = [];
$user = Auth::user();
if ($user->isSuperAdmin() || $user->isAdmin()) {
$perms = [
[
'url' => '/',
'icon' => 'fa fa-home',
'name' => 'Dashboard'
],
[
//rest of permissions
],
];
}
if ($user->isUser()) {
$perms = [
[
'url' => '/',
'icon' => 'fa fa-home',
'name' => 'Dashboard'
],
[
//rest of permissions
],
];
}
$auth = [
'id' => Auth::user()->id,
'name' => Auth::user()->name,
'card' => Auth::user()->card,
'scard' => Auth::user()->scard,
'user_type_id' => Auth::user()->user_type_id,
'email' => Auth::user()->email,
'perms' => $perms
];
}
return [
'app' => [
'name' => Config::get('app.name'),
],
'auth' => [
'user' => $auth,
],
'flash' => [
'success' => Session::get('success'),
],
'errors' => Session::get('errors') ? Session::get('errors')->getBag('default')->getMessages() : (object)[],
]
});
What am I doing wrong? Where i'm getting the error it doesn't specify where the error is, just what it is, it signals the last line of the code I presented as where the error is, but all that's there is the closing parenthesis and brackets.
Knowing nothing of Inertia, it seems you are misusing the Inertia::share function. In their docs, I see 3 examples. The first two have parameter 1 being a string (eg. 'auth.user' or 'app.name'), and the last has parameter 1 being an associative array, so each element still has a unique string key.
In your code, you are passing a closure as the first parameter. I believe that you can fix it by simply adding a name as the first parameter:
Inertia::share('auth.user', function () {
$auth = null;
if (Auth::user()) {
$perms = [];
$user = Auth::user();
if ($user->isSuperAdmin() || $user->isAdmin()) {
$perms = [
[
'url' => '/',
'icon' => 'fa fa-home',
'name' => 'Dashboard'
],
[
//rest of permissions
],
];
}
if ($user->isUser()) {
$perms = [
[
'url' => '/',
'icon' => 'fa fa-home',
'name' => 'Dashboard'
],
[
//rest of permissions
],
];
}
$auth = [
'id' => Auth::user()->id,
'name' => Auth::user()->name,
'card' => Auth::user()->card,
'scard' => Auth::user()->scard,
'user_type_id' => Auth::user()->user_type_id,
'email' => Auth::user()->email,
'perms' => $perms
];
}
return [
'app' => [
'name' => Config::get('app.name'),
],
'auth' => [
'user' => $auth,
],
'flash' => [
'success' => Session::get('success'),
],
'errors' => Session::get('errors') ? Session::get('errors')->getBag('default')->getMessages() : (object)[],
];
});
Hello :) I'm just learning Wordpress and I can not find an answer to a question about how to write code.
Here is my code:
if ( !function_exists( 'mythemename_social_sites_list' ) ) :
function mythemename_social_sites_list() {
return [
[ 'name' => 'Twitter', 'icon' => 'fab fa-twitter' ],
[ 'name' => 'Facebook', 'icon' => 'fab fa-facebook-f' ],
[ 'name' => 'Google Plus', 'icon' => 'fab fa-google-plus-g' ],
];
}
endif;
In general, the function returns many more elements of the array, but here I gave only 3 to be more visible. What's the problem?
Is there any sense to write it this way? In a sense, a function that returns an array but does nothing with it? From what I understand, functions should be used when they do something. For example, this one would take an array, then do something with it and return the changed one.
Would not it be better to just write this:
global $mythemename_social_sites_list;
$mythemename_social_sites_list = [
[ 'name' => 'Twitter', 'icon' => 'fab fa-twitter' ],
[ 'name' => 'Facebook', 'icon' => 'fab fa-facebook-f' ],
[ 'name' => 'Google Plus', 'icon' => 'fab fa-google-plus-g' ],
];
Global, because I use this array in several places.
Thanks in advance for the brightening :)
I'm doing a manual query. You can translate the names of the columns easily, but I can not do it with the results of the columns in a slightly more efficient way. The results are printed in excel with the help of yii2tech\spreadsheet\Spreadsheet.
Preparing query
$columns = [
[
'attribute' => 'description',
'label' => Yii::t('data', 'Description')
],
[
'attribute' => 'type',
'label' => Yii::t('data', 'Type')
]
];
$query
->addSelect(['description' => 'data.description'])
->addSelect(['type' => 'data.type'])
->from('data')
$rows = $query->all();
So far I make the query. The following is my way of translating the results of the type column. Because they can be just some values.
Translating results
foreach ($rows as $key => $row) {
$rows[$key]['type'] = Yii::t('data', $row['type']);
}
This data is exported to xls format:
Exporting results
$exporter = new Spreadsheet([
'dataProvider' => new ArrayDataProvider([
'allModels' => $rows,
]),
'columns' => $columns,
]);
You may define translation inside of $columns declaration - it will save you manual iteration trough results array to replace type with translated string:
$columns = [
[
'attribute' => 'description',
'label' => Yii::t('data', 'Description'),
],
[
'attribute' => 'type',
'label' => Yii::t('data', 'Type'),
'value' => function ($data) {
return Yii::t('data', $data['type']);
}
],
];
If sheet is big and types are often repeated, you may try to cache translated string - Yii::t() may be quite expensive:
$columns = [
[
'attribute' => 'description',
'label' => Yii::t('data', 'Description'),
],
[
'attribute' => 'type',
'label' => Yii::t('data', 'Type'),
'value' => function ($data) {
static $translations = [];
if (!isset($translations[$data['type']])) {
$translations[$data['type']] = Yii::t('data', $data['type']);
}
return $translations[$data['type']];
},
],
];
This will call Yii::t() only once per unique type. But if list of types is small and hardcoded, you may simplify this even more - create getTranslatedTypes() static method, which returns translated list of all types:
public static function getTranslatedTypes() {
return [
'some type' => Yii::t('data', 'some type'),
// ...
];
}
And use it as a source of translations:
$translations = Type::getTranslatedTypes();
$columns = [
[
'attribute' => 'description',
'label' => Yii::t('data', 'Description'),
],
[
'attribute' => 'type',
'label' => Yii::t('data', 'Type'),
'value' => function ($data) use ($translations) {
return $translations[$data['type']] ?? $data['type'];
},
],
];
I have a index view in which I have a field name sub-division. The field displays the id. But I want to display it's name instead of ID.
What I have done?
I have placed a function in my Model
public function getSubdivision()
{
return $this->hasOne(SurveyHescoSubdivision::className(), ['id' => 'sub_division']);
}
In my search model I have done filtering like
$query->andFilterWhere([
'id' => $this->id,
'meter_id' => $this->meter_id,
'created_by' => $this->created_by,
'updated_by' => $this->updated_by,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'store_id' => $this->store_id,
'sub_division'=> $this->sub_division,
'status'=>'Inventory Stored',
]);
And in my index view
[
'label' => 'Sub-Division',
'value' => function ($d) {
return $d->subdivision->name;
},
// 'filter' => Html::activeDropDownList($searchModel, 'sub_division', \common\models\SurveyHescoSubdivision::toArrayList(), ['prompt' => "Sub-Div", 'class' => 'form-control']),
],
The table has the name column against the id.
When I goto my index page I am getting the below exception
Trying to get property of non-object
I have done this thing in other views but I don't know why this is showing
Any help would be highly appreciated.
Your subdivision id is empty, Use :
[
'label' => 'Sub-Division',
'value' => 'subdivision.name',
],
OR
[
'label' => 'Sub-Division',
'value' => function ($d) {
return !empty($d->sub_division) ? $d->subdivision->name : null;
},
],