I'm a new in October CMS. Now i have an issue with file attachment. In Model :
public $attachOne = [
'cover_image' => ['System\Models\File']
];
Now in controller :
$comic = Comic::with('cover_image)->find($id);
it return:
{
"id": 24,
"title": "Comic example detail",
"description": "Comic example detail",
"total_page": "14",
"cover_image": {
"id": 165,
"disk_name": "5d5a21ca68bf9280597931.png",
"file_name": "img06.png",
"file_size": 142958,
"content_type": "image/png",
"title": null,
"description": null,
"field": "cover_image",
"sort_order": 165,
"created_at": "2019-08-19 04:12:58",
"updated_at": "2019-08-19 04:15:23",
"path": "http://localhost:10080/storage/app/uploads/public/5d5/a21/ca6/5d5a21ca68bf9280597931.png",
"extension": "png"
}
}
I want cover_image return only some field, like below:
"cover_image": {
"id": 165,
"file_name": "img06.png",
"path": "http://localhost:10080/storage/app/uploads/public/5d5/a21/ca6/5d5a21ca68bf9280597931.png",
}
how can i do it?
use constraint eager loading
see in the doc https://octobercms.com/docs/database/relations#constraining-eager-loads
it is explained with where statement but you can use select statement and you can get only required attributes.
I am sure it will help you.
Related
Hi there I am working on a Laravel Project in which I have used polymorphic relation.
Here is the polymorphic Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class approval extends Model
{
use HasFactory;
public function approvable()
{
return $this->morphTo();
}
}
I put here the query:
return approval::with('approvable')->get();
When I run query the query I get result something like this:
[
{
"id": 27,
"status": "sent",
"description": "<p>Ok its perfect</p>",
"type": null,
"extra_description": null,
"sender_id": 17,
"receiver_id": 18,
"added_by": null,
"approvable_id": 27,
"approvable_type": "App\\Models\\deviation",
"created_at": "2023-01-04T10:25:21.000000Z",
"updated_at": "2023-01-04T10:25:21.000000Z",
"approvable": {
"id": 27,
"externalId": "deviation_261672825172",
"name": "Deviation 1021",
"type": "external",
"internal_type": null,
"seriousness": "level_2",
"schedule_date": "2023-01-06 09:39:32",
"sent_to_manager": null,
"status": "completed",
"completed_at": "2023-01-04 11:15:30",
"root_cause_status": null,
"root_cause_completed_at": null,
"actions_taken_status": null,
"actions_taken_completed_at": null,
"verification_status": null,
"verification_completed_at": null,
"description": "<p>In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available.</p>",
"customer_id": 5,
"manager_id": 17,
"added_by": 18,
"company_id": 2,
"created_at": "2023-01-04T09:39:32.000000Z",
"updated_at": "2023-01-04T11:15:30.000000Z",
"ccp_pc": null,
"schedule_type": "auto",
"close_up_notes": "dsfdsfdsf",
"parent_id": 26,
"repeat_incident": "yes",
"affected_products": null,
"complainer": null,
"sample_received": "yes",
"sending_status": "sent"
}
},
{
"id": 28,
"status": "approved",
"description": "<p>fggfdg</p>",
"type": null,
"extra_description": null,
"sender_id": 17,
"receiver_id": 17,
"added_by": null,
"approvable_id": 19,
"approvable_type": "App\\Models\\checklist_category",
"created_at": "2023-01-07T07:14:10.000000Z",
"updated_at": "2023-01-07T07:14:10.000000Z",
"approvable": {
"id": 19,
"name": "Checklist 1009",
"description": "Checklist 1009",
"type": "audit",
"company_id": 2,
"created_by": 17,
"created_at": "2023-01-07T06:37:59.000000Z",
"updated_at": "2023-01-07T07:14:10.000000Z",
"delete_status": "false",
"status": "approved",
"approved_by": 17,
"status_changed_at": "2023-01-07 07:14:10",
"equipment_id": null
}
}
]
It is basically linked with two models right now
checklist_category
deviation
Now, I do not want here all data from related models.
I want here that when the type is checklist_category, then I only want to select('id','name','description') from checklist_category model
and when the type is deviation, I want to select('id','name','seriousness') from deviation model.
So how would it be possible in eloquent eager loading?
Here is how i would do it :
->with([
'approvable' => function (MorphTo $morphTo)
{
// Use contrain to select some fields
$morphTo->constrain([
ChecklistCategory::class => function (Builder $query) {
$query->select('id');
},
Deviation::class => function (Builder $query) {
$query->select('other_field');
},
]);
},
])
You can use constrain to customize the query for each type of models, and your case add a select.
you have access to the query builder and you can do everything you need, so you can also add where/whereIn and all sorts of calls
If you also need to get relations for each type of models you can use morphWith :
->with([
'approvable' => function (MorphTo $morphTo)
{
// use morphWith to get a subrelation
// (it's the same as calling ->with() inside the ->constrain callback)
$morphTo->morphWith([
ChecklistCategory::class => [
'subrelation_of_checklist_category:id,name',
'another_one:id,created_at'
],
Deviation::class => [
'subrelation_of_deviation:id,deviation_id',
'another_one:id,created_at'
]
]);
},
])
I want to filter users based on their subscription_status which s stored in a pivot table.
I have Three tables users , subscription_packages , subscription_package_user
$user=User::with(['studentDetails','subscriptionsSatus.courses'])
->withPagination($offset,$perPage)
->get()
->sortBy('first_name')->values();
this code return the response is
[
{
"id": 44,
"first_name": "Abcd Test",
"last_name": "Test lastname",
"student_details": null,
"subscriptions_satus": [
{
"id": 1,
"name": "Gold",
"price": 3000,
"user_id": "2"
"pivot": {
"user_id": 44,
"subscription_package_id": 1,
"subscription_status": "on_free_trial",
"expires_on": null,
"id": 9
},
"courses": [
{
"id": 18,
"title": "English Grammar for Class 3",
"price": 400,
"strikethrough_price": null,
"status": "draft",
"user_id": 2,
"image": "http://127.0.0.1:8000/courses/1615702915.png",
"description": null,
"pivot": {
"subscription_package_id": 1,
"course_id": 18,
}
}
]
}
]
}]
i want to return only users who having subscription_status =$filter.
$filter='acive'or 'on_free_trail'
my model is
public function subscriptionsSatus()
{
return $this->belongsToMany(SubscriptionPackage::class)->withTimestamps()->withPivot('subscription_status','expires_on','id');
}
I havetried
$filter=$request->input('filter')??"active";
$user=User::with(['studentDetails','subscriptionsStatus.courses'])
->whereHas('subscriptionsStatus', function($query) use($filter){
$query->wherePivot('subscription_status','=',$filter);
})
->withPagination($offset,$perPage)
->get()
->sortBy('first_name')->values();
But Got error Column not found 'pivot'
You need to use wherePivot along with the orWhere like below:
public function subscriptionsStatus()
{
return $this->belongsToMany(SubscriptionPackage::class)
->withTimestamps()
->withPivot('subscription_status','expires_on','id')
->wherePivot(function($q){
return $q->where('subscription_status','=','active')
->orWhere('subscription_status','=','on_free_trail');
});
}
Update
Or in your controller:
$user=User::with(['studentDetails','subscriptionsStatus.courses'])
->whereHas('subscriptionsStatus', function($query) use($filter){
$query->withPivot('subscription_status')
->wherePivot('subscription_status','=',$filter);
})
->withPagination($offset,$perPage)
->get()
->sortBy('first_name')->values();
I have pivot table post_tag between posts and tags table.
I want to show the tags name for each post in resource API.
Resource file code snippets:
public function toArray($request)
{
return [
'Name'=> $this->title,
'Description'=> $this->desc,
'Image'=> $this->image_path,
'Posted By'=> $this->user->name,
'Category Name'=> $this->category->name,
'Tags' => $this->tags,
];
}
Relation added in Post model:
public function tags()
{
return $this->belongsToMany(Tag::class)->withTimestamps();
}
One of Results of an API:
"Name": "first post",
"Description": "desc of the post",
"Image": "https://blog.test/uploads/post/SjvDfC1Zk5UzGO6ViRbjUQTMocwCh0lzEYW1Gufp.jpeg",
"Posted By": "test",
"Category Name": "web dev",
"Tags": [
{
"id": 1,
"name": "HTML",
"created_at": "2020-01-08 19:19:55",
"updated_at": "2020-01-08 19:19:55",
"pivot": {
"post_id": 1,
"tag_id": 1,
"created_at": "2020-01-08 19:21:40",
"updated_at": "2020-01-08 19:21:40"
}
},
{
"id": 2,
"name": "CSS",
"created_at": "2020-01-08 19:19:55",
"updated_at": "2020-01-08 19:19:55",
"pivot": {
"post_id": 1,
"tag_id": 2,
"created_at": "2020-01-08 19:21:40",
"updated_at": "2020-01-08 19:21:40"
}
},
I need to show like that JUST names:
"Tags" : {
'HTML',
'CSS',
'JS',
}
You must be use like this
return [
'Name'=> $this->title,
'Description'=> $this->desc,
'Image'=> $this->image_path,
'Posted By'=> $this->user->name,
'Category Name'=> $this->category->name,
'Tags' => $this->tags->pluck('name')
];
I would like to be able to select a single document here is a sample of how a document looks
{
"_index": "myindex_products",
"_type": "product",
"_id": "8Wct9mEBlkDZwzEMRfbG",
"_version": 1,
"_score": 1,
"_source": {
"productId": 5749,
"name": "Product Name Here",
"productCode": "PRODCODE",
"productCategoryId": 73,
"length": 6,
"height": 0,
"productTypeId": 1,
"url": "product-name-here",
"productBrandId": 7,
"width": 0,
"dispatchTimeInDays": 10,
"leadTimeInDays": 6,
"stockAvailable": 0,
"weightKg": 0.001,
"reviewRating": 5,
"reviewRatingCount": 17,
"limitedStock": false,
"price": 16.3,
"productImage": "28796-14654.jpg",
"productCategory": {
"productCategoryId": 73,
"name": "Accessories - New",
"fullPath": "Accessories - New",
"code": "00057"
},
"productSpecification": [
{
"productSpecificationId": 127151,
"productId": 5749,
"specificationId": 232,
"name": "Brand",
"value": "Brand1"
}
,
{
"productSpecificationId": 127175,
"productId": 5749,
"specificationId": 10,
"name": "Guarantee",
"value": "10 years"
}
]
}
}
_id is being generated when I index so I don't know this at the point I want to update. I have the productId value and I would like to use this to select a document to then update/delete is there a way to return a single document if you know a particular exact value.
Thanks
While indexing, you can use something like PUT your_index/5749 (5749 being your product id) and ES will use its value for the _id field instead of auto-generating it.
Yammer Activity Stream is available at:
https://www.yammer.com/api/v1/streams/activities.json?access_token=
This successfully results in all the recent activities like:
{
"items": [
{
"id": "/users/www.yammer.com-341514-1508953644/rollups/45191477209921-45191477209921",
"unseen": true,
"icon": "/images/notifications/page_add.png",
"icon_name": null,
"category": "file-create",
"message": "[[user:1508783078]] uploaded [[uploaded_file:24511980]].",
"heading": "",
"created_at": "2014/10/02 07:07:42 +0000",
"objects": [],
"actions": [],
"subject": {
"type": "uploaded_file",
"id": 24511980
},
"meta": null,
"client_type": "unknown",
"client_url": "https://www.yammer.com",
"client_icon": "https://mug0.assets-yammer.com/mugshot/images/16x16/3rd_party.png",
"client_large_icon": "https://mug0.assets-yammer.com/mugshot/images/75x75/3rd_party.png",
"image": "https://mug0.assets-yammer.com/mugshot/images/48x48/no_photo.png",
"third_party": false
},
{
"id": "/users/www.yammer.com-341514-1508953644/rollups/45191475863746-45191475863746",
"unseen": true,
"icon": "/images/notifications/page_add.png",
"icon_name": "page",
"category": "file-download",
"message": "[[user:1508783078]] downloaded [[uploaded_file:24373320]] from the [[group:3455089]] group.",
"heading": "",
"created_at": "2014/10/02 07:07:00 +0000",
"objects": [
{
"id": 24373320,
"type": "uploaded_file"
}
],
"actions": [],
"subject": {
"type": "uploaded_file",
"id": 24373320
},
"meta": null,
"client_type": "unknown",
"client_url": "https://www.yammer.com",
"client_icon": "https://mug0.assets-yammer.com/mugshot/images/16x16/3rd_party.png",
"client_large_icon": "https://mug0.assets-yammer.com/mugshot/images/75x75/3rd_party.png",
"image": "https://mug0.assets-yammer.com/mugshot/images/48x48/no_photo.png",
"third_party": false
},
]}
QUESTION:
I am trying to get all the recent activities after a certain time-stamp or after a certain offset. Is there any query parameter for that?
this is
older_than="/users/www.yammer.com-341514-1508953644/rollups/45191475863746-45191475863746"
that would return all the activities older than the last activities you shew in your sample return value