Laravel - whenLoaded() and count() on Resource - php

I have a model for a collection which contains assets.
I wish to return the asset count with the collection resource though as a result this causes the whenLoaded function to be called which as a result loads all of the assets within the collection. I do not wish for this to occur.
I'm not sure how to go about creating a work around which will continue to allow me to use the resource, count the assets, but not call the whenLoaded function.
return [
'id' => $this->id,
'name' => $this->name,
'description' => $this->description,
'enabled' => $this->enabled,
'sticky' => $this->sticky,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'asset_count' => $this->assets->where('enabled', 1)->count(),
'assets' => Asset::collection($this->whenLoaded('assets')),
];
Is there a way to still use the resource, return the asset count, but not have the whenLoaded called as a result?

Try this
you can simply create a new relationship like liveassets
function liveassets(){
return $this->hasMany('assets')->where('enabled','=', 1);
}
return [
'id' => $this->id,
'name' => $this->name,
'description' => $this->description,
'enabled' => $this->enabled,
'sticky' => $this->sticky,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'asset_count' => $this->liveassets->count(),
'assets' => $this->liveassets,
];

Related

How to create mongodb collection on runtime while executing Yii2 code?

I need to create mongodb collection while executing code, by checking whether the collection is exists or not.
I tried below things
Yii::$app->db->createCommand()-> createCollection("collection_name");
but collection not created.
Please help.
Issue resolved..
Yii::$app->db->createCommand()->createCollection("collection_name")->execute();
and index will added for collection as,
$collectionName = Yii::$app->db->getCollection("collection_name");
$collectionName->createIndexes([
'key' => ['id' => 'int'],
'name' => 'id_index'
],
[
'key' => ['id' => 'int', 'category' => 'text'],
'name' => 'id_category_index',
]);

Typed property Spatie\Feed\FeedItem::$author must not be accessed before initialization

I am using laravel spatie/laravel-feed for generate feed for my blog site
here is the method for get all item
public static function getFeedItems()
{
return Post::all();
}
Here is the method for generate data
public function toFeedItem(): FeedItem
{
return FeedItem::create([
'id' => $this->id,
'title' => $this->title,
'summary' => $this->meta_description,
'updated' => $this->updated_at,
'link' => url('/', $this->slug),
'authorName' => $this->title,
]);
}
I have added this in my web.php
Route::feeds();
i have added the link in my view like this
#include('feed::links')
when i am visiting this link then it's showing this error
https://github.com/spatie/laravel-feed here have like this
return FeedItem::create([
'id' => $this->id,
'title' => $this->title,
'summary' => $this->summary,
'updated' => $this->updated_at,
'link' => $this->link,
'authorName' => $this->authorName,
]);
In array we can't use like this
authorName' => $this->authorName,
soltuion to use
'author' => $this->authorName,

Laravel: Factory Fake Car Data

I created a new factory in Laravel called "CarFactory.php" and I want to use https://github.com/pelmered/fake-car.
This is the fake data that I will insert into my database using Tinker:
return [
'name' => $this->faker->vehicleBrand(),
'founded' => $this->faker->biasedNumberBetween(1998,2017, 'sqrt'),
'description' => $this->faker->paragraph()
];
In the Laravel usage code, I'm not sure where to put this (e.g. Car Model, CarFactory):
$faker->addProvider(new \Faker\Provider\Fakecar($faker));
$v = $faker->vehicleArray();
return [
'vehicle_type' => 'car',
'vin' => $faker->vin,
'registration_no' => $faker->vehicleRegistration,
'type' => $faker->vehicleType,
'fuel' => $faker->vehicleFuelType,
'brand' => $v['brand'],
'model' => $v['model'],
'year' => $faker->biasedNumberBetween(1998,2017, 'sqrt'),
];
Help is needed to know how to set this up.

Cakephp3 able to save data but cannot save associations

Hi so I'm trying to save data through Cakephp3 framework. I have two tables Measures and MeasuresUsers. Measures has many MeasuresUsers
$data = [
'name' => $this->request->data['name'],
'description' => $this->request->data['description'],
'deleted' => $this->request->data['deleted'],
'chart_type' => $this->request->data['chart_type'],
'public' => $this->request->data['public'],
'user_id' => $this->request->data['user_id']
];
$measure = $this->Measures->newEntity($data,['associated' => ['MeasuresUsers']]);
$measure['measures_users'] = [
'user_id' => $user['id'],
'chart_type' => $this->request->data['chart_type'],
'deleted' => $this->request->data['deleted'],
'public' => $this->request->data['public']
];
$this->Measures->save($measure)
I'm able to save data into Measures table but not into the associative table (MeasuresUsers). I'm not sure what I'm missing here, any help or comments is appreciated!
#HarryM. is close, but you want to put your data into the $data array before creating the entity (the associated tag is kinda pointless if you're not providing that data!), and since it's a hasMany relation, I think you need to have the data in an array of arrays:
$data = [
'name' => $this->request->data['name'],
'description' => $this->request->data['description'],
'deleted' => $this->request->data['deleted'],
'chart_type' => $this->request->data['chart_type'],
'public' => $this->request->data['public'],
'user_id' => $this->request->data['user_id'],
'measures_users' => [
[
'user_id' => $user['id'], // Should this be $this->request->data['user_id'] instead?
'chart_type' => $this->request->data['chart_type'],
'deleted' => $this->request->data['deleted'],
'public' => $this->request->data['public'],
],
],
];
$measure = $this->Measures->newEntity($data, ['associated' => ['MeasuresUsers']]);
$this->Measures->save($measure);
Coming from CakePHP 2.x, I see your associate model is MeasuresUsers.
When you're setting the associated data in
$measure['measures_users'] = [
'user_id' => $user['id'],
'chart_type' => $this->request->data['chart_type'],
'deleted' => $this->request->data['deleted'],
'public' => $this->request->data['public']
];
Trying changing $measure['measures_users'] to $measure['MeasuresUsers']:
$measure['MeasuresUsers'] = [
'user_id' => $user['id'],
'chart_type' => $this->request->data['chart_type'],
'deleted' => $this->request->data['deleted'],
'public' => $this->request->data['public']
];
CakePHP's model conventions usually assume camel casing of what you have in the database e.g. DB Table it movie_tickets and Cake would expect is as MovieTicket unless otherwise declared in the Model itself.

Unable to get name against the id in yii2 gridview

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

Categories