laravel - how to set relation with hasManyThrough? - laravel

I have problem with relation of laravel in this case.
One event has many event dates.
One event date has many tickets.
One ticket has many slots. User will book slot and save in booking table.
How can i get list of event dates have list of tickets and total booking of every ticket ?
In Event model i has :
return $this->hasMany('App\EventTicket', 'event_date_id');
Please help me improve it to get right data ?

1) How can I get list of event dates to have list of tickets?
you can make hasManyThrough relation for that. In EventDates model make relation as tickes with hasManyThrough.
Now through event dates, you will get all the tickets for that day.
2) total booking of every ticket
after make hasManyThrough you can use withCount('tickes') with this, you'll get the booking total count.

Related

How display model with four relationships based on date in laravel?

I have these models:
Partner
Invoice
Bill
Transaction
CreditNote
Invoice, Bill, Transaction, and CreditNote belong to Partner.
and Partner has many invoices, bills, transactions, credit_notes.
I want to make SOA reports, which are based on date.
for example:
DATE TYPE ITEM
2021-1-1 partner invoiced us invoice_number..
2021-1-2 us invoiced partner bill_number..
2021-1-3 partner paid us transaction_debit
2021-1-4 us paid partner transaction_credit
2021-1-5 partner paid us credit_note
I use `rappasoft livewire data table to show this information.
I get partner stuff like this:
Partner::where('id', 1)->with('invoices', 'bills', 'transactions', 'credit_notes')->get()
I don't know how to display these information in a table a sort based on date
How to do this?
It sounds to me like your structure is wrong. All of the five things that you're putting in your table are what I'd consider "transactions" (not to be confused with your model "Transactions".
That being the case, then you want either want a Transactions model which is morphable (that's the more complicated solution) or which has a "type" property.
If morphable, each transaction would then be associated to the corresponding partner, and to the corresponding model - invoice, bill, payment, refund, etc. - on a "morphsTo" basis.
If you went down the easier route, each transaction would just have a type, linked to a TransactionType model, which would indicate the type of the transaction.
As things stand, your structure is overcomplicated. Either of the above approaches would simplify it.

List of all users who subscribed for Laravel Cashier in given date range

There are many functions in Laravel Cashier to get a lot of subscription information for a particular user. What if I need to get the list of all users who have subscribed in a given date range or who have cancelled in a given date range.
User::subscription()->where doesn't work
You can check subscriptions with the method subscribed('packageName'). Non tested I should be something like
User::where('subscription', User::subscribed('packageName')->between('created_at', [$start, $end])->get()

How to show bank balance through transactions in laravel

I have Bank table and transaction table. The relationship is one-to-many, so a bank may have many transactions. Transaction table has the bank foreign key(bank_id). and each transaction has a column 'type' which is either credit or debit.
I want to show each bank balance based on its transactions.
Here are my questions:
How to select a specific bank?
How to calculate the balance for a bank?
Should I calculate it in controller, model, or view? which one is the best?
Here is transactions table:
thanks in advance!
I'm not sure what you are trying to do. You'd select a bank like
$bank = Bank::find(2);
and you could do something like
$bank->transactions->sum('amount');
You need to create relationships in the model, like this in the Transactions model:
public function bank(){
return $this->belongsTo(Bank::class);
}
and this in the Bank Model
public function transactions (){
return $this->hasMany(Transactions::class);
}
Then you can access them like this:
$this->bank->transactions;
Laravel has excellent docs, you just need to read them, e.g. Relationships
I wanted to show the balance of each bank in my database. I had to sum all credits which belongs to a bank and then sum all debits for that bank and then find the balance, which is credits minus debits. So using relationship you mentioned. I wrote this code.
<td>{{$bank->transactions->where('type', 'credit')->sum('amount') - $bank->transactions->where('type', 'debit')->sum('amount')}}</td>

Design DB for chats between users

I have one problem with designing database structure for my app. I have 3 models: Events, groups and users.
Relations:
Events - Users - many to many.
Groups - Users - many to many.
There will be available one chat withing group and one inside event. Additonally in the near future I am going to implement chats between two or more users.
I'm a bit confused what is the best way to design it. I created Chat model and many to many relation between chat and users.
First idea: The members of group and event chats will be stored in respectively event_users and group_users tables. Only chats between users will be stored in chat_users.
Second idea: chat_users will be synchronized with event_users and group_users tables. The advantage of this way is that I will have simple separated logic to manage chats and there will be not many complicated queries to DB.
Thanks for any feedback or maybe other ideas.
I believe that Chat has a polymorphic association with Group and Event (and maybe User as well). Some solutions to the problem are provided here:
https://www.slideshare.net/billkarwin/practical-object-oriented-models-in-sql/29-Polymorphic_Assocations_Solutions_Exclusive_arcs
Could something like this work?
ChatUser
id
user_id
chat_id
Chat (exclusive arcs method)
id
event_id (nullable)
group_id (nullable)
description/etc
ChatMessage
id
chat_id
user_id_from
user_id_to (nullable)
date
message
Assumptions made:
event/group can contain multiple chats
user can belong to a group but not to group chat
Besides, Laravel comes with a native polymorphic relation handling solution. It could fit here as well.
User can participate to an event and event can have many users who participate to that event, in this specific case we need to use as Many to Many relation Laravel One to Many Relation
event_user
id
event_id
user_id
User can be member of many group and a group can avec many member, in this case we need a Many to Many relation Laravel Many to Many Relation
group_user
id
group_id
user_id
chat can be related to an event or a group, so we save the type of the chat witch can be either Event or Group and the corresponding ID of that event or group. so if we have a particular chat we can retreive all users related to that chat through the corresponding event or group it depending of the chatable_type in each case, to learn more see Laravel Many To Many Polymorphic Relations
chats
id
name
chatable_id
chatable_type
we need also one table to save all conversations for specific chat
messages
id
chat_id
message
created_at
and for users chat you can create a separate table witch can save the related information about that chat specifiquely
conversations
id
sender_id
receiver_id
message
all sender_id and receiver_id will be foreign key witch reference some id on users table

Laravel: Calling a common function for multiple Models from anywhere in laravel?

I am working on a Laravel 5.4 app. In that I have a booking in which we add Tours, Flights, Hotels which are separate section along with their separate Model / Controller / Views with operations of CURD. Tours, Flights, Hotels stores data in separate tables along with price of that item.
I have in total 4 tables Bookings, Tours, Flights, Hotels. Bookings have booking_id which related further to Tours, Flights, Hotels and these all can have multiple entries against a booking. I can have 2 hotels, or 4 tours against a booking_id.
Now, I have a billing section where I need to show all the entries with their no of items and price and a sub total of all items as total booking price.
I need a function which I can call from anywhere in the system and can show the booking total and if needed itemized list too.
Should I create a new Model/Controller for that purpose or a Trait maybe? Not able to think how should I do this in Laravel.
Please check and advise some solution/concept for this and I will try to implement it.
Directory:
/app/Http/Helpers
Add some a Helper class there with namespace: App\Http\Helpers.
Put all your methods in there.
You can now call them from anywhere as long as you import the namespace App\Http\Helpers.
If to don't want to typehint and make laravel resolve these dependencies, you can just have static methods, then you won't need to instantiate anything saving you some time.

Resources