I am trying to build a (slightly complex) subscription service using Laravel.
The subscriptions would be of the type: Breakfast Subscription for 20 breakfasts to be used within 30 days of enrolment.
For example, If user enrols for the breakfast subscription on 1st of April, he can avail for any 20 days of his choice, until April 30.
I've made the following tables and corresponding Models:
User Model and table
Users:
- id
- name
- email
etc
Subscriptions table and Model
Subscriptions:
- id
- name
- price
- validity
- meals_available
subscription_user pivot (?) table with softDeletes
- subscription_id
- user_id
- start_date
- end_date
- created_at
- updated_at
- deleted_at
I've updated the corresponding models with belongsToMany relations ships
User:
class User extends Authenticatable
{
...
/*
* User can have many subscriptions
*/
public function subscriptions()
{
return $this->belongsToMany('App\Subscription')->withTimestamps();
}
}
Subscriptions Model
class Subscription extends Model
{
...
/*
* Subscription can have many Users
*/
public function users()
{
return $this->belongsToMany('App\User')->withTimestamps();
}
}
Issues that I need help with 1. Is the database/model structure correct way to solve the problem?
While I can use attach() and detach() methods, I am not able to fill in start_date and end_date values in the subscription_table. How to do this?
I want to use soft deletes on the pivot table. How I can use it?
Thanks in Advance.