Using custom name of `name` field of Zizaco/entrust Package - laravel-5

I am working in a project that has a large postgreSQL database. The previous project was developed in Java from scratch. We are now developing that in Laravel. The previous system had user management system similar to Zizaco/entrust. So, we used in our system as well. The previous table had module table instead of permission table used in entrust. We have already configured that by changing the table name in config/entrust.php. However, the previous system has permission_name instead of name field used in entrust. How do I config entrust to use the unique permission_name instead of name field.
I am looking for a solution, so that we don't have to change in the sources of entrust because then upgrading it would break the system. Can it be configured in the model?

The Entrust package is hardcoded to use the name attribute, so there is no configuration value or anything to change that. However, one thing you can attempt is to define an accessor and mutator for the name attribute.
In your App\Permission model, define the following functions:
class Permission extends Model {
// accessor
public function getNameAttribute($value) {
return $this->permission_name;
}
// mutator
public function setNameAttribute($value) {
$this->attributes['permission_name'] = $value;
}
}
Documentation for accessors and mutators: http://laravel.com/docs/5.0/eloquent#accessors-and-mutators

Related

Can this block of code of mine can be improved

[This is to populate data from two tables that one has two foreign keys from the same column as reference on the other table]
https://i.stack.imgur.com/D8fiv.png
[This is my schema for the table with the foreign key]
https://i.stack.imgur.com/eYDL0.png
This is written in laravel and it is working however i have an itchy feeling that this is wrong
As someone else has commented, you should put the code in your question. More context might also be necessary as it's not clear what you are trying to return from your view(). Are you returning $citizen, $family, or both? It would also be helpful to include what version of Laravel you are using. I'll assume you are using the latest Laravel 8.
Your code should work but you are making more work for yourself if you don't utilize Laravel's relationships. If you do, all the query stuff you have in your code can be reduced to just a few short lines of code such as this:
public function view(Citizen $citizen)
{
$citizen->load('family');
return $citizen;
}
Database Migration
You can shorten your migration by using foreignId(). See docs for details
https://laravel.com/docs/8.x/migrations#foreign-key-constraints
Before:
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('citizens');
After:
$table->foreignId('client_id')->constrained();
Route Model Binding
I'm assuming your view() method is in your controller and is called from a route. Since the one parameter required is the client's id, you can use Route Model Binding and skip fetching the client from the database.
https://laravel.com/docs/8.x/routing#route-model-binding
public function view(Citizen $citizen)
{
// your code
}
Relationships
You seem to have relationships set up but you aren't using them to load data. You should be able to load the related objects without any manual queries.
If you have a belongsTo(One to Many) relationship on the Citizen model to the Family model, you can load it like this:
$citizen->load('family');
or just use it like this
$relationship = $citizen->family->relationship;
I don't know what the relationships between your models are but you should read up on the different relationship types in the docs.
https://laravel.com/docs/8.x/eloquent-relationships

Laravel's uuid type

I was reading Laravel's documentation that I noticed Laravel 7 offers a uuid type in the page for database migrations
Unfortunately I couldn't find much information about using it in the documentation. How is such a column filled when I insert a new record? Does Laravel fill it on its own? Does Laravel guarantee it to be unique or should I worry about collisions? Can I use this field to create short links for the posts of a blog, for example?
Laravel should fill this on its own by setting the keyType(untested) on your model:
protected $keyType = 'string';
The key, whether you fill it manually or not, is unique as laravel depends on Ramsey.
To manually acquire a uuid you may use the provided helper:
use Illuminate\Support\Str;
return (string) Str::uuid();

TYPO3 cache behaviour with updated models

I have this weird behaviour from Typo3 6.2 LTS.
In my extension I have a Model with a FileReference Property. This property has a vaule != 0. This value does exist in sys_file_reference table.
Not the weird magic happens. If I try to access this file, I do only get a nullvalue instead of a FileReference- / FileObject.
We already cleared our cache (server and browser) but nothing. It's still null.
I appreciate every kind of help!
Greetz, Paddaels
I remember it was always hard to make a 1:1 relation from a domain model to a FileReference. I suggest you to use existing patterns and work with a ObjectStorage for that purpose.
You can copy the neccessary TCA from the existing tca of the tt_content table (field image for example). The Property annotation should look like:
/**
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Vendor\Extension\Domain\Model\FileReference>
* #lazy
* #cascade remove
*/
protected $propName;
Of course you have to create the FileReference Model in your own namespace. But you can extend the Extbase basemodel, so you dont have to write any methods.
To map your model to the sys_file_reference table you have to add some typoscript.
For that purpose create a ext_typoscript_setup.txt in your extensions folder and insert the following code (adjust namespace and modelname)
config.tx_extbase.persistence.classes {
Vendor\Extension\Domain\Model\FileReference.mapping {
tableName = sys_file_reference
}
}
After clearing the caches in the install tool (and making database migrations of course) it should work.
Explanations:
#lazy: Typo3 wont fetch all references at once, only if the property is accessed.
#cascade remove: Extbase will delete all sys_file_reference records related to your domain model once the model is deleted.

Laravel change stored value of polymorphic relationship

I am trying to make the stored value of a polymorphic relationship more readable by other applications. Currently the polymorphic model type is stored as the FQCN of the model. Using the example in the Laravel Docs, imageable_type could be "App\Product", or "App\Staff". However, this value can be a little more difficult to manage if any non-laravel applications which aren't based on this convention and are also accessing the same database. Also, if the model FQCN ever gets refactored, you have to modify your other applications to account for the change.
Is there a way to change the type to something more consistent and readable, and then have a mapping class that maps the keys to the model? (e.g. have "product" map to "App\Product")
Yes. This is a change that was recently implemented.
Add this to your service provider (in the boot method):
Illuminate\Database\Eloquent\Relations\Relation::morphMap([
'product' => App\Product::class
]);
If you simply pass an array of model names, it'll default to using the table names:
Illuminate\Database\Eloquent\Relations\Relation::morphMap([
App\Product::class,
App\Staff::class,
]);
if you are adding morphMap method to service provider, you might want to use
'product' => \App\Product::class
( "\" before App),otherwise your namespace can be wrong.

New Grails domain class is not creating a table in the database

I just created a new grails domain class on a project I just started working on. I know the datasources are setup correctly since we already have a bunch of domain classes that are updating to the database just fine.
The error I get:
Caused by BatchUpdateException: ORA-00942: table or view does not exist
I tried running DBMUpdate but that also did not create the table.
Is there something I'm missing with creating domain classes? Do I need to change something in the changelog ? Any advise would be helpful!
The easiest thing would be to add dbCreate = "update" to your DataSource.groovy. A better thing would be to use the database-migrations plugin for your app.
Regarding manually creating tables, the convention grails following by default is to underscore camelcase. For example, given the following domains:
class User {
String firstName
String lastName
static hasMany = [addresses: Address]
}
class Address {
static belongsTo = [user: User]
}
You would end up with the following tables:
user
---------
id
version
first_name
last_name
---------
address
---------
id
version
user_id
---------
Try running...
grails export-schema
This will uses Hibernate's SchemaExport tool to generate DDL or export the schema for the entire database. From that file, you can grab the lines for the missing table.
Check out the Grails Quick Reference for more details on the command.

Resources