Custom fields design for multiclient application - laravel

I have a question releated to application design. I have a ecommerce application which is used by 9 clients. Every client has the same copy of application with different frontend template. App is designed under laravel and it's updating on every customer server to keep up-to-date. So every app have the same backend("engine"), same database design etc.
Problem is that two of the clients wants a custom fields to for CRUD pages. With current update mechanism every client will get those fields which is not what i want.
I've been thinking about adding separate table to database to keep there a configuration of all fields - like a map of table columns. So when app is used controller will call configuration table to get list of fields and foreach them in view.
+---+---------------+-------------------+------------+
|id |controller | field_name |field_type |
+---+---------------+-------------------+------------+
| 1 | products | price_retail | integer |
| 2 | manufacturers | name | varchar |
| 3 | manufacturers | logo | varchar |
| 4 | manufacturers | custom_for_client | integer |
+---+---------------+-------------------+------------+
Is it a valid - good idea?

Without knowing exactly what you plan to do with these "fields", I can only make suggestions.
Have you considered the use of a json column to store data for fields that aren't applicable to all users?
Database Migration
...
$table->json('meta')->nullable();
...
Model
...
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
...
'meta' => 'array',
...
];
...
You can then access the "fields" within this json object like so:
$model->meta['column'];
or iterate over then like so:
collect($model->meta)->each(function ($column) {});
https://laravel.com/docs/7.x/eloquent-mutators#array-and-json-casting

Related

Laravel 5 - deleting child model data

I have a Model called Campaign which takes the following structure
+----+--------------+-----------------+----------+---------------+--------------+--------------------+----------+-------+--------+---------------------+---------------------+
| id | campaignName | userId | clientId | clientContact | contactEmail | campaignObjectives | acNumber | notes | active | created_at | updated_at |
+----+--------------+-----------------+----------+---------------+--------------+--------------------+----------+-------+--------+---------------------+---------------------+
| 1 | test | 7 | 10 | Mr Fakes | 12345 | sdfsdfsd | 12345 | | 0 | 2016-02-29 11:51:59 | 2016-02-29 13:51:28 |
+----+--------------+-----------------+----------+---------------+--------------+--------------------+----------+-------+--------+---------------------+---------------------+
I then have a CampaignTypes Model with the following structure
+----+--------------+-----------------+------------+---------------------+---------------------+
| id | campaignType | creativeArrival | campaignId | created_at | updated_at |
+----+--------------+-----------------+------------+---------------------+---------------------+
| 14 | Dynamic | 2016-02-26 | 1 | 2016-02-23 16:00:01 | 2016-02-23 16:00:01 |
+----+--------------+-----------------+------------+---------------------+---------------------+
The relationships in these Models is pretty straight forward. A Campaign can have many CampaignTypes and a CamapignType belongs to a Campaign.
In the Campaign schema I have an active column. This is what I use to delete a Campaign. So the destroy method looks like the following
public function destroy(Campaign $campaign)
{
$campaign->update([
'active' => false
]);
Session::flash('flash_message', 'Campaign deleted');
Session::flash('flash_type', 'alert-success');
return Redirect::route('campaigns.index')->with('message', 'Campaign deleted.');
}
Now although it does not cause too many problems, I do not currently set any CampaignTypes row as being deleted if its parent Campaign has been deleted.
What would be the best way to delete the child data without actually deleting it?
Thanks
What you are doing to your Campaign model is called a soft delete and Laravel actually has a nice way to handle that (check out the link). However, it is totally valid to use your own conventions for soft deleting, as you currently are doing when you change the active column to 0. Either way you choose, there is no native Eloquent method to do this automatically, so you'll need a bit of code to modify the parent model.
If you continue to use custom soft deleting (as you are now), it would be easiest to make a custom delete method on the Campaign model. This method will update the record (soft delete it) and also delete any children. You have not specified whether you also want the children models soft deleted or hard deleted, but either one is simple (if you want to soft-delete them, just loop through all and update the relevant column).
Campaign model:
public function deleteAll() {
$campaign = self::find($this->id);
$campaign->update([
'active' => false
]);
//delete children, either hard or soft (use foreach loop on soft)
$campaign->types()->delete();
}
Then you just call that custom method in your controller.
public function destroy(Campaign $campaign)
{
$campaign->deleteAll();
}
If you decide to implement the Laravel convention for soft deleting (basically adding a deleted_at attribute to the model and use a trait) then the model's deleting and deleted events will get triggered, and you can listen for those and respond to them in the model's boot method.
Campaign Model:
protected static function boot() {
parent::boot();
static::deleting(function(campaign) {
//delete children, either hard or soft (use foreach loop on soft)
$campaign->types()->delete();
});
}
And then that gets triggered every time you call delete() on your model, like this:
public function destroy(Campaign $campaign)
{
$campaign->delete();
}

can I use laravel 4 eloquent model or do I need to use db:query

Hi I am trying my first attempt to use ORM with Laravel. I have a big table from Drupal that I want to grab some records of and I need to join those with another table in Drupal to get the records that I care about manipulating.
Like so...
Node
----------------------------------------------------------
| Nid | type | misc other stuff | N
==========================================================
| 1 | Programs | Test Service | 1 |
----------------------------------------------------------
| 2 | Programs | Example Service | 1 |
----------------------------------------------------------
| 3 | Something else | Another Service | 1 |
----------------------------------------------------------
Fields
----------------------------------------------------------
| id | title | NID | tag |
==========================================================
| 1 | Blog Title 1 | 1 | THER |
----------------------------------------------------------
| 2 | Blog Title 2 | 2 | TES |
----------------------------------------------------------
| 3 | Blog Title 3 | 3 | ANOTHER |
----------------------------------------------------------
I want to get all the Nodes where type='Programs' and inner join those with all fields where NIDs are the same. Do I do that with an Eloquent ORM in app/model/node.php? Or a query builder statement $model=DB:table? what is the code for this? Or do I just do it in PHP?
You could do this with the ORM, but would have to override everything that makes it convenient and elegant.
Because you say you're trying to "manipulate" data in the fields table, it sounds like you're trying to update Drupal tables using something other than the Drupal field system. I would generally not recommend doing this—the Drupal field system is big, complicated, and special. There's a whole CMS to go with it.
You should move the data out of the old Drupal database and into your new database using seeds (http://laravel.com/docs/4.2/migrations#database-seeding).
Define a "drupal" database connection in your app/config/database.php, in addition to whatever you're using as a "default" connection for a new application. You can seed Eloquent models from an alternative connection in this manner:
<?php
// $nodes is an array of node table records inner joined to fields
$nodes = DB::connection('drupal')
->table('node')
->join('fields', 'node.nid', '=', 'fields.nid')
->get();
Pull the data out and put it in proper tables using Laravel migrations into normalized, ActiveRecord-style tables (http://laravel.com/docs/4.2/migrations#creating-migrations).
I prefer query builder, it's more flexible
DB::table('Node')
->join('Fields', 'Fields.NID', '=', 'Node.Nid')
->where('type', 'Programs')
->get();
Create two models in app/model (node.php and field.php) like this:
class Node extends \Eloquent {
protected $fillable = [];
protected $table = 'Node';
public function fields()
{
return $this->hasMany('Field');
}
}
class Field extends \Eloquent {
protected $fillable = [];
public function node()
{
return $this->belongsTo('Node');
}
}
Than you could do something like this:
$nodes = Node::with('fields')->where('type', 'Programs')->get();
You will get all your nodes with their relation where type is Programs.

Laravel 4: one to many by on multiple columns?

I'm making a table that essentially maps rows in a table to rows in another table where the structures are as follows:
|--- Words --| |- Synonyms -|
|------------| |------------|
| id | | id |
| en | | word_id |
| ko | | synonym_id |
| created_at | | created_at |
| updated_at | | updated_at |
|------------| |------------|
Now then, I know I can essentially have the words model have many Synonyms through a function like:
public function synonyms()
{
return $this->hasMany('Synonym');
}
No problem, but this method always gets it by the the word_id, and I would like to get it from word_id OR synonym_id that way I don't have to make multiple entries in the DB.
Is there anyway I can do this?
Check laravel docs Eloquent relationships. It would only get word_id because that's the only foreign key I believe.
Also why do you have synonym_id in your Synonyms table?
I believe you are looking for polymorphic relationship.
http://laravel.com/docs/eloquent#polymorphic-relations
I think your best bet is to create a many-to-many relationship with words on itself using the synonyms table as your pivot table.
Add this to your Word model.
public function synonyms()
{
return $this->belongsToMany('Word', 'synonyms', 'user_id', 'synonym_id');
}
Using it:
$word = Word::where('en', '=', 'someword')->first();
foreach($word->synonyms as $synonym) {
// This method would probably return the same word as a synonym of itself so we can skip that iteration.
if($synonym->en == $word->en) {
continue;
}
// Echo the synonym.
echo $synonym->en;
}
I'm a bit confused on you wanting to be able to find synonyms by the word_id or synonym_id but I think if you are using the many-to-many, it won't matter because if you know the synonym, it's still technically just a word, and you'd do the exact same thing.

Propel ORM - One-to-many relationship with parent name

Is it possible to create a parent name field into one-to-many relationship in Propel ORM.
This type of relationship uses in CRM systems.
Just imagine that we have a Task List. So, we created a Task #1 and related it to a Project.
Task #2 is related to Account (e.g. create a contract).
Task #3 is related to Bug Tracker (e.g. fix a bug).
So, we have the following relationships:
task_name | parent_name | parent_id
--------------------------------------------------
Start a project | Project | <project_id>
Create a contract | Account | <account_id>
Fix a bug | Bug Tracker | <bug_id>
Is it possible to implement in Propel. If no, could you recommend me another ORM with this feature.
The main purpose is to get a list of records with all relationship values.
For my example, it should look like (in JSON):
{
"Task_0":{"Id":1,"Name":"Start a project","ParentId":1,"ParentName":"Project","Project":{"Id":1,"Name":"Project-1","Tasks":{"Task_0":"*RECURSION*"}}},
"Task_1":{"Id":1,"Name":"Create a contract","ParentId":1,"ParentName":"Account","Account":{"Id":1,"Name":"Account-1","Tasks":{"Task_0":"*RECURSION*"}}},
"Task_2":{"Id":1,"Name":"Fix a bug","ParentId":1,"ParentName":"Bug","Bug":{"Id":1,"Name":"Bug-1","Tasks":{"Task_0":"*RECURSION*"}}}
}
Does anyone help me?
The output you have shown looks as if the toArray function has been used on the Propel objects and then the json_encode function. This should work if you define foreign keys mutually in Propel's schema.xml.
Since project tasks, account tasks and bug tracker tasks all have something in common, they all are tasks :), I would organize them as sub classes of a more general task entity.
You will end up with a collection of tables like this:
Table "task"
id | name
------------------------
1 | Start a project
2 | Create a contract
3 | Fix a bug
4 | Start another project
5 | Fix another bug
---------------------------------------
Table "bugtrack_task"
id | id_task
---------------
1 | 3
2 | 5
---------------------------------------
Table "project_task"
id | id_task
---------------
1 | 1
2 | 4
---------------------------------------
Table "account_task"
id | id_task
---------------
1 | 2
In the end, you would define a view in the schema.xml. This could look something like this:
<table name="view_task" phpName="ViewTask" skipSql="true" readOnly="true" description="All my tasks together for display">...</table>
Note that the skipSql attribute has been set to true. This will skip this view table when generating the SQL code. Propel will generate the classes for you but won't touch your database. You can now manually define the view yourself putting into it whatever you desire.
Of course you'd have to put some effort into creating this view but it pays off as you will be able to use the Propel classes like so for instance:
$tasks = ViewTask::create()->find();
$result = array();
foreach($tasks as $task) {
$result[] = $task->toArray();
}
return json_encode($result);
This isn't a complete answer but I hope you see the idea! Good luck :-)

Laravel 4: A better way to represent this db structure/relationship within laravel

I have the following db table set up
+--------------+ +--------------+ +-----------------------+
| users | | clients | | user_clients |
+--------------+ +--------------+ +----------------------+
| id | | id | | usersid |
| name | | name | | clientid |
| authid | | email | +----------------------+
| (plus) | | (plus) |
+-------------+ +-------------+
I have set up the a relationship table [b]user_clients[/b] with foreign keys to the relevant db, so userid is link to users->id and clientid is linked to clients->id.
Dependant on the Users Authid is how many clients are linked:
Authid 1: User can only have one client associated to them.
Authid 2: User can only have one to many clients associated to them
Authid 3: User has access to ALL clients.
So as i am new to this relationship side of laravel currently i would do a lot of querying to get some details eg:
I would done something like:
$userClient =UsersClients::select('clientid')->where('userid','=',$userid)->get();
Then I would probably loop through the result to then get each client details and output to the page.
foreach($userClient as $i ->$cleint){
echo '<div>' .$cleint->name . '</div>';
........
}
Would this be an old way and could it be handled better??
----------------EDIT---------------
i have managed to sort it as the following:
User Model:
public function clients() {
return $this->hasMany('UsersClients','userid');
}
User Controller
$selectedUserClients = User::find(24)->clients;
I get the same out come as my previous result as in client id's 1 & 2, but now how to get the client details from the actual client db basically is there an easier way that the following:
foreach ($selectedUserClients as $key => $client) {
$clientInfo = Client::select('id','clientname')->where('id','=',$client->clientid)->get();
echo $clientInfo[0]->clientname;
}
The users_clients table needs it's own ID column in order for many-to-many relationships to work.
On your User Model, try
public function clients()
{
return $this->belongsToMany('Client','user_clients','userid','clientid');
}
Now you can find the clients assigned to each individual user with
User::find(24)->clients.
You could also do the inverse on your Client model...
public function users()
{
return $this->belongsToMany('User','user_clients','clientid','userid');
}
This would allow you to find all the users belonging to each client
Client::find(42)->users;
I would also like to mention that it's best practice to use snake case for your id's such as user_id or client_id.
Your table names should be plural. users and clients.
Your pivot table should be snake_case, in alphabetical order, and singular. client_user.
This would make working with Eloquent much easier because it's less you have to worry about when setting up the relationships and it might be easier for someone else to help you work on your project.
Instead of return $this->belongsToMany('Client','user_clients','userid','clientid'); all you'd have to do is return $this->belongsToMany('Client'); which should keep your app much cleaner and easier to read.

Resources