Paginate on grouped queries - laravel

I'm writing a system to follow cash flow. The users register transactions they made:
(transactions index screenshot)
but I need to get these transactions separated by date. I tried using an Request to index, but it doesn't looked the best pratice (it worked, however). Now I'm trying to do another aproach: retrieve all transactions and paginate them by date. Is it possible?
ps: maybe grouping by months helps? like:
$transactions = Transaction::all()->groupBy(function($transaction) {
return Carbon::parse($transaction->date)->format('m-Y');
});
this returns:
{
"03-2019": [
{
"id": 1,
"title": "Teste 1",
"date": "2019-03",
"user_id": 1,
"description": "Primeiro teste, com entrada de 1500 reais.",
"value": "1500.00",
"flow": 1,
"created_at": "2019-03-24 05:39:45",
"updated_at": "2019-03-24 05:39:45"
},
{
"id": 2,
"title": "Teste 2",
"date": "2019-03",
"user_id": 1,
"description": "Segundo teste, com saída de 500 reais.",
"value": "500.00",
"flow": 0,
"created_at": "2019-03-24 05:39:45",
"updated_at": "2019-03-24 05:39:45"
}
]
}
ps²: yeah, front-end is in brazilian portuguese and english isn't my native language.. :)

Try this out
<?php
use Illuminate\Pagination\LengthAwarePaginator;
$transactions = Transaction::all()->groupBy(function($transaction) {
return Carbon::parse($transaction->date)->format('m-Y');
});
//new LengthAwarePaginator($data,$countOfData,$perPage,$currentPage);
$transactions=new LengthAwarePaginator($transactions->forPage($currentPage,$perPage),$transactions->count(),$currentPage);
Hope it helps...

Related

Laravel, get relation row instead of id

I want to get author row instead of author_id. I could this with add collection and change one by one but has Laravel any function for this? Well, I want make this one line
Can i use something like this
Book::where('id',$bid)->with('author')->first('author_id AS author'); //Changes only coulmn name :(
model
public function author()
{
return $this->hasOne(Author::class,'id','author_id');
}
query
Book::where('id',$bid)->with('author')->first()
Output
{
"id": 1,
"name": "Book 1",
"author_id": 3,
"category_id": 2,
"level_id": 1,
"book_language_id": 1,
"book_length": 0,
"img": "book1.png",
"summary": "Summary 1",
"rate_avg": "2.75",
"created_at": "2022-03-04T18:46:32.000000Z",
"updated_at": "2022-03-04T18:52:28.000000Z",
"author": {
"id": 3,
"name": "Author 3",
"created_at": "2022-03-04T18:46:32.000000Z",
"updated_at": "2022-03-04T18:46:32.000000Z"
}
}
Want
{
"id": 1,
"name": "Book 1",
"author": {
"id": 3,
"name": "Author 3",
"created_at": "2022-03-04T18:46:32.000000Z",
"updated_at": "2022-03-04T18:46:32.000000Z"
},
"category_id": 2,
"level_id": 1,
"book_language_id": 1,
"book_length": 0,
"img": "book1.png",
"summary": "Summary 1",
"rate_avg": "2.75",
"created_at": "2022-03-04T18:46:32.000000Z",
"updated_at": "2022-03-04T18:52:28.000000Z",
}
in your query
Book::where('id',$bid)->with('author')->first()
you are getting the book that has that id and you are eager loading the author relation, so in order to get the author you have to access the author field:
Book::where('id',$bid)->with('author')->first()->author
As I said I can this with collection like this:
$b=Book::where('id',$bid)->with('author')->first();
$book=collect([
'id'=>$b->id,
'name'=>$b->name,
'author'=>$b->author,
'category_id'=>$b->category_id,
'level_id'=>$b->level_id,
'book_language_id'=>$b->book_language_id,
'book_length'=>$b->book_length,
'summary'=>$b->summary,
'rate_avg'=>$b->rate_avg,
]);
But this method seems unnecessary
In your example the only difference between the output and what you want is "author_id": 3, as been deleted.
So if you don't want a column or rename a column, you need to use ->select and take all the field you want. And you can also rename with something like that
-> select(DB::raw('author_id as auhtor'), 'books.*')

Cannot retreive virtual card number in test mode via stripe API using Go examples

Trying to follow the example here: https://stripe.com/docs/issuing/cards/virtual
When I add params.AddExpand("number"), no number is returned, yet via the dashboard I was able to see the card numbers. Here's sample code and redacted info for the Req and Resp.
func (ac *appContext) CardRetrieve(id string) *stripe.IssuingCard {
stripe.Key = ac.Config.Stripe.SecretKey
params := stripe.IssuingCardParams{}
params.AddExpand("number")
params.AddExpand("cvc")
ic_num, _ := card.Get(id, &params)
return ic_num
}
Returns:
{
"id": "ic_redacted",
"object": "issuing.card",
"brand": "Visa",
"cancellation_reason": null,
"cardholder": {
"id": "ich_redacted",
"object": "issuing.cardholder",
"billing": {
"address": {
"city": "A Beach",
"country": "US",
"line1": "404 Main St.",
"line2": "Suite #302",
"postal_code": "19001",
"state": "DE"
}
},
"company": null,
"created": 1613338532,
"email": "redacted#notreal.com",
"individual": {
"dob": {
"day": 20,
"month": 10,
"year": 1990
},
"first_name": "User",
"last_name": "Testing",
"verification": {
"document": {
"back": null,
"front": null
}
}
},
"livemode": false,
"metadata": {
},
"name": "User Testing",
"phone_number": "+15165551212",
"requirements": {
"disabled_reason": "under_review",
"past_due": [
]
},
"spending_controls": {
"allowed_categories": [
],
"blocked_categories": [
],
"spending_limits": [
{
"amount": 1,
"categories": [
],
"interval": "daily"
}
],
"spending_limits_currency": "usd"
},
"status": "active",
"type": "individual"
},
"created": 1613338532,
"currency": "usd",
"exp_month": 1,
"exp_year": 2024,
"last4": "0088",
"livemode": false,
"metadata": {
},
"replaced_by": null,
"replacement_for": null,
"replacement_reason": null,
"shipping": null,
"spending_controls": {
"allowed_categories": null,
"blocked_categories": null,
"spending_limits": [
{
"amount": 1,
"categories": [
],
"interval": "daily"
}
],
"spending_limits_currency": "usd"
},
"status": "inactive",
"type": "virtual"
}
What confuses me is the documentation found here:
https://stripe.com/docs/issuing/cards/virtual
It says: You can retrieve both the full unredacted card number and CVC from the API. For security reasons, these fields are only available for virtual cards and will be omitted unless you explicitly request them with the expand property. Additionally, they are only available through the Retrieve a card endpoint. That links to the issue card retrieval end point, but the params defined in the virtual cards example references the CardParams{} struct.
No of the examples show what imported module their aliasing for card to exec card.Get, but it stands to reason given the flow of the documentation that this should be IssuingCardParams{} and that the card alias is referencing: "github.com/stripe/stripe-go/issuing/card"
I also find it strange that we're defining params in the example but not passing it into the card.Get()
Edit:
I went digging through the module and it seems like to get the card details you have to call: details, _ := card.Details(id, params) but I get a 404 when trying to call that. The object returned is actually the right object and I see number and cvc, albeit nil.
I get the following error:
2021/02/15 00:33:06 Request error from Stripe (status 404): {"status":404,"message":"Unrecognized request URL (GET: /v1/issuing/cards/ic_redacted/details). Please see https://stripe.com/docs
So it seems you need to include a /v72 in the import:
"github.com/stripe/stripe-go/v72"
The documentation should be updated to show this and the virtual card example for go should also be updated.

How to merge two collections with a specific condition in laravel?

I have Two collections competences and coCompetences those collections look like :
competence :
...
{
"id": 6,
"category_id": 17,
"user_id": 1,
"objective_level": 4,
"current_level": 4,
"target_date": "2021-11-28",
"obtained_date": "2022-10-14",
"comment": "",
"created_at": "2020-01-08 10:06:28",
"updated_at": "2020-01-08 10:06:28",
"name": null,
"competenceName": "Hierarchy Building & BOM (Bill of Material)",
"category": {
"id": 17,
"competence": "Hierarchy Building & BOM (Bill of Material)",
"created_at": "2020-01-08 09:53:55",
"updated_at": "2020-01-08 09:53:55",
"wheel_id": 10
}
},
{
...
coCompetences :
{
"category": {
"id": 12,
"competence": "Criticality Analysis",
"created_at": "2020-01-08 09:53:55",
"updated_at": "2020-01-08 09:53:55",
"wheel_id": 10
},
"user_id": 1,
"competenceName": "Criticality Analysis",
"category_id": 12,
"objective_level": 0,
"current_level": 0,
"target_date": "2020-01-14",
"obtained_date": "2020-01-14",
"comment": ""
},
I would like to push only coCompetences elements into competences where coCompetences.category_id are not exist in competences.category_id
in another way : add all coCompetences elements to competences except those where coCompetences.category_id already exists in coCompetences.
I don't want to write code instead of you, algorithm will be enough, I hope.
simplest way
Take array Ids from first array, for example by pluck()
Filter second and return items that don't exist in first
push filtered data to first

Laravel vue cannot access to relation data

I have customer data which has hasOne relationship with address table.
Returned data format is like below.
{
"customer_id": 1,
"last_name": "Cruickshank",
"first_name": "Pearl",
"phone": "+4 921-008-8344",
"email": "kristofer.kautzer#example.org",
"order_id": null,
"address_id": 66,
"created_at": "2017-01-18 06:24:40",
"updated_at": "2017-01-18 06:24:40",
"deleted_at": null,
"address": {
"address_id": 1,
"address_code": "RS-03549-9811",
"address1": "14978 Effertz Turnpike Apt. 086",
"address2": null,
"city": "Susanaburgh",
"province": "Illinois",
"country": "PG",
"postal_code": "03549-9811",
"created_at": "2017-01-18 06:24:40",
"updated_at": "2017-01-18 06:24:40"
}
I got data using return $this->model->with(implode(',', $relation))->get();
However, I cannot access any of data in address object.
Vue Code
I throw city is undefined. However, I can access to address object data using chrome developer tool
<tr v-for="(customer, index) in customers">
<td>{{index + 1}}</td>
<td>{{getFullName(customer.first_name, customer.last_name)}}</td>
<td>{{customer.phone}}</td>
<td>{{customer.address.city}}</td>
</tr>
Am I doing something wrong? I tried customer['address']['city'] as well.
I can access city when I get data as you see below.
getCustomerList () {
axios.get('/api/customers')
.then((res)=>{
if(res.status === 200){
//I can access city here but not in vue for loop ...
console.log(res.data.customers[0].address.city)
this.customers = res.data.customers
}
})
},

Laravel - pivot data not available in email

I have the relations in the models as follows:
class HotelBooking extends Model
{
public function rooms(){
return $this->belongsToMany('App\Room', 'hotelbooking_room', 'hotelbooking_id')->withPivot(['quantity', 'addon_id']);
}
}
When I call $booking->rooms()->get();, I should get the following data:
"rooms": [
{
"id": 28,
"hotel_id": 89,
"type": "Double",
"quantity": 4,
"adults": 2,
"children": 1,
"description": null,
"created_at": "2016-05-15 12:24:39",
"updated_at": "2016-05-15 15:29:58",
"pivot": {
"hotelbooking_id": 30,
"room_id": 28,
"quantity": 1,
"addon_id": 386
}
}
]
Which is normally correct. What's weird is that when I send $rooms to the email data, the pivot attribute is not sent. It works well along the website but in case of sending it in the email it does not. Any idea why?!

Resources