how to implement entrust in laravel 5.4 - laravel-5.4

Hi I am new with the use of laravel.I can't understand how to create multiple role user. I want to create an admin and general user . I want to use entrust (https://github.com/Zizaco/entrust) if possible. I am using Laravel 5.4 and install entrust. If possible an small example with explain of entrust will be helpful.

Step 1. After creating a project in laravel, open the composer.json and update the require object with entrust like this
"require": {
"php": ">=7.1.3",
"fideloper/proxy": "~4.0",
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0",
"tymon/jwt-auth": "1.0.0-rc.1",
"zizaco/entrust": "dev-master"
},
Then Run
composer update
Step 2. Open config/app.php and find providers array and add the following line.
Zizaco\Entrust\EntrustServiceProvider::class,
Below providers array find aliases and add the following line
'Entrust' => Zizaco\Entrust\EntrustFacade::class,
Then Run this command
php artisan vendor:publish
After this you will see a new file in config directory named entrust.php
Step 3. Open app/Http/Kernel.php and add the middleware
<?php
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* #var array
*/
protected $routeMiddleware = [
....
'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,
]; ?>
Step 4. Run This
php artisan entrust:migration
Above command will create the 4 tables roles, permissions, role_user and permission_role
Now You are ready to go

Related

database error after adding new column

I had created a login and register form. But I forgot to add a column. So, I wanted to add a new column named "contactNumber". For adding the column, I wrote a command named php artisan migrate:refresh in cmd.exe and also write a code at create_users_table in migration folder. But, the new column's value doesn't go to mysql database. What am I doing wrong?
You need to add your new column to your model, so in your User class you need to do this:
protected $fillable = [
'name', 'email', 'password', 'contactNumber'
];
Hope this help
You're trying to insert a new record in the database but the contactNumber column doesn't have a default value. In the migration file you can add nullable to that column, eg.
$table->string('contactNumber')->nullable();
or directly in the database from a GUI.
For this type of scenario i personally create new migration like this
php artisan migrate:make add_column_to_table --table="tableName"
Now to run the new migration run this command.
php artisan migrate
This will add new column in your table.Also have a look into this thread.
https://laracasts.com/discuss/channels/laravel/add-new-column-to-existing-table

Laravel seed database from existing database

i have now new structure of my database, but i need to import the old data in the new format. For that reason i want to use the Laravel seeder, but i need somehow to connect to the old database and make select queries and to tell the seeder how to put the data in the new database.
Is that possible ?
Try:
Examples:
php artisan iseed my_table
php artisan iseed my_table,another_table
Visit: https://github.com/orangehill/iseed
Configure your laravel app to use two mysql connections (How to use multiple database in Laravel), one for the new database, the other for the old one.
I'll fake it like old and new.
In your seeds read from the old database and write into the new.
$old_user = DB::connection('old')->table('users')->get();
foreach ($old_users as $user) {
DB::connection('new')->table('users')->insert([
'name' => $user->name,
'email' => $user->email,
'password' => $user->password,
'old_id' -> $user->id
// ...
]);
}
Make sure to add messages while seeding like $this->command->info('Users table seeded'); or even a progress bar (you can access command line methods) to know at which point of the import you are.
Download package from
Git repo : https://github.com/orangehill/iseed
then update below file src/Orangehill/Iseed/IseedCommand.php
Add below code at line number 75
// update package script
if($this->argument('tables') === null){
$tables = Schema::getConnection()->getDoctrineSchemaManager()->listTableNames();
}
and update getArguments method in same file with below code
array('tables', InputArgument::OPTIONAL, 'comma separated string of table names'),
and then run php artisan iseed so it will get all the tables from your existing db and start creating seeders for all tables

I am not able to connect to mysql using laravel

I am able to migrate the migration tables to the database i configured in database.php. But when i am trying to fire following command in routes.php
$details=DB::query('SELECT * FROM test_user');
print_r($details);
. I am geeting error on my browser.
There is no such a method that you have used as given below:
DB::query('SELECT * FROM test_user');
Instead you can do it using:
// Get all records from "test_user" table
$users = DB::table('test_user')->get();
Read the Laravel documentation first.

Having trouble running cakephp app on remote server

If you get:
Error: SQLSTATE[42000]: Syntax error or access violation: 1104 The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
After uploading cake php app and database from xampp localhost to a remote server.
Having tried importing the cake database into a new db on local machine and works fine. So I couldn't see it being the information imported.
Had no idea how to fix this. Its a simple and common problem with an easy fix as below.
After much hair pulling I managed to find the problem/fix with the help of my good friend ten1 on cakephp IRC chat.
When this is a cakephp specific issue which it was in my case you need to do the un-thinkable and edit the core.
The file you need to edit is AclNode.php Located here: /lib/Cake/Model/AclNode.php
You need to add a line before line 113
112 }
$db->query('SET SQL_BIG_SELECTS=1'); //Add this line
113 $result = $db->read($this, $queryData, -1);
114 $path = array_values($path);
This is generally only a problem on servers with shared hosting.
Rather than editing core file you could add a beforeFind method to your app/models/app_model.php file, if wanted it to affect all over or to your particular model file, like the following:
function beforeFind() {
$this->query('SET SQL_BIG_SELECTS=1');
}
For Cakephp 3 following works:
'Datasources' => [
'default' => [
'init' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET SESSION SQL_BIG_SELECTS=1',
), // Add this to the existing array

Override WP_SITEURL and WP_HOME for WordPress Multisite

For doing local development on a WordPress site (http://www.example.com), I was previously overriding the WP_SITEURL and WP_HOME values in wp-config.php like so:
define('WP_SITEURL', 'http://local-example/');
define('WP_HOME', 'http://local-example/');
This would allow me to copy the database and site files to a local server, and make modifications as necessary, testing on the local install.
It was then necessary to convert the install to a WordPress Multisite so that users, authentication, plugins, etc. could be shared between the main site and a secondary site, hosted on a subdomain (http://second.example.com).
The method above to override the values in the wp_options table no longer works, but I am unsure the proper way to set a value for the entries in wp_blogs as well as the wp_2_options table for the primary and subdomain.
Updating my HOSTS file is somewhat of a workaround, but it not ideal (I am not able to compare to the live site, etc). Running a script to change the database values is another option I have tried, but is slightly more cumbersome, so my questions is whether or not there is an option in MultiSite to override these values in a settings file, such as wp-config.php, and if so what it would look like.
Update: full updated plugin code with additional description can be found here: http://justinsilver.com/technology/wordpress/wordpress-plugins/wordpress-plugin-wp-server-migration/
I was able to come up with a solution with the help of #user916011. I needed to be able to copy the wp_options table(s) to my development environment as they contain configurations that are needed. To overcome the issue of not being able to set the WP_SITEURL and WP_HOME values in MultiSite, I wrote a custom filter to replace the _config_wp_siteurl() and _config_wp_home() functions that are available for non-multisite installs that is included in a plugin that is available network-wide and is configured in wp-config.php. I am then able to copy all of the database tables except wp_site and wp_blogs to a local database.
I highly recommend the URL Token Replacement Techniques for WordPress 3.0 article by Chris Murphy to help handle URLs in your content.
This example assumes a subdomain multisite install, with a domain of example.com and two subdomains, www.example.com and second.example.com. The local development URLs will be www.example.local and second.example.local respectively.
Database Changes:
Update the domain value in wp_site:
UPDATE wp_site SET domain = 'example.local' WHERE domain = 'example.com';
Update the domain value(s) in wp_blogs:
UPDATE wp_blogs SET domain = 'www.example.local' WHERE domain = 'www.example.com';
UPDATE wp_blogs SET domain = 'second.example.local' WHERE domain = 'second.example.com';
Plugin Code:
The following plugin should be installed network-wide.
<?php
/*
Plugin Name: MultiSite WP_HOME and WP_SITEURL
Plugin URI: http://doublesharp.com/
Description: Allows wp_options values to be overwritten in wp-config.php for MultiSite
Author: Justin Silver
Version: 1.0
Author URI: http://doublesharp.com
License: GPL2
*/
function _ms_config_wp_siteurl( $url = '' ) {
if (is_multisite()):
global $blog_id, $current_site;
$cur_blog_id = defined('BLOG_ID_CURRENT_SITE')? BLOG_ID_CURRENT_SITE : 1;
$key = ($blog_id!=$cur_blog_id)? $blog_id.'_' : '';
$constant = 'WP_'.$key.'SITEURL';
if ( defined( $constant ) )
return untrailingslashit( constant($constant) );
endif;
return $url;
}
add_filter( 'option_siteurl', '_ms_config_wp_siteurl' );
function _ms_config_wp_home( $url = '' ) {
if (is_multisite()):
global $blog_id;
$cur_blog_id = defined('BLOG_ID_CURRENT_SITE')? BLOG_ID_CURRENT_SITE : 1;
$key = ($blog_id!=$cur_blog_id)? $blog_id.'_' : '';
$constant = 'WP_'.$key.'HOME';
if ( defined( $constant ) )
return untrailingslashit( constant($constant) );
endif;
return $url;
}
add_filter( 'option_home', '_ms_config_wp_home' );
?>
Configure wp-config.php:
Add new constants to wp-config.php. The primary site should use the standard WP_HOME and WP_SITEURL and the tertiary URLs should use WP_{$blog_id}_HOME and WP_{$blog_id}_SITEURL
define('WP_HOME', 'http://www.example.local');
define('WP_SITEURL', 'http://www.example.local');
define('WP_2_HOME', 'http://secondary.example.local');
define('WP_2_SITEURL', 'http://secondary.example.local');
You could use the update_option in functions.php
update_option("siteurl","http://example.com");
update_option("home","http://example.com");
There's a similar question being asked here: Team Development of a Wordpress Site which I provided a possible solution for. In your case, you may not want to go to that extent (though it would be very flexible); however, you could always look at the portion of the answer that mentions a domain-replacement technique.
I've outlined that solution here: URL Token Replacement Techniques...
I had the same issue and wanted a solution as similar to defining WP_HOME & WP_SITEURL in wp-config.php as possible.
I can't use a plugin, because I am syncing with GIT and don't want to have that plugin in my repo and I would have to add the plugin everytime... I suppose it could be activated network wide through the wp_sitemeta table... but it wasn't ideal for me.
I came up with this solution.
Be sure to not sync wp_blogs, wp_site, wp_sitemeta. And then add this code to your local wp-config.php somewhere below $table_prefix:
/* Update local database */
mysql_connect( DB_HOST, DB_USER, DB_PASSWORD ) or die( mysql_error() );
mysql_select_db( DB_NAME );
$result = mysql_query("SELECT * FROM `". $table_prefix ."blogs`") or die( mysql_error() );
while( $row = mysql_fetch_array( $result ) )
{
$id = (1 == $row['blog_id']) ? '' : $row['blog_id'] .'_';
mysql_query( "UPDATE `". $table_prefix . $id ."options` SET `option_value`='http://". $row['domain'] ."/' WHERE (`option_name`='siteurl' OR `option_name`='home')" ) or die( mysql_error() );
}
This will make sure your sites are synced up to your local wp_blogs table.
The only drawback is that when you add a new site, you do manually need to copy it into your wp_blogs table and update its local url.
This question is too old to answer, but recently I faced a similar situation where I have to migrate my wp site from staging to production and it was painful as it is having 5 subsite.
After scratching my head for all day, I found a simple and useful solution.
Of course one have to make changes in wp-config.php.
Other then that you have to follow below steps.
Generate database dump using following command.
mysqldump -u [user name] –p [password] [options] [database_name] [tablename] > [FileName]
After that open that file in any editor.
Find and replace your old domain with new domain.
Now restore the database with dump you created.
mysql -u [user name] -p [password] [database name] < [file anme]