I'm wondering if I can do that in coorilation to an activities feed which is outlined in these two videos.
https://laracasts.com/lessons/build-an-activity-feed-in-laravel
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm still struggling with the concept of model ownership. What I mean by that is coming up with a way to show who created a particular row in the database through my application. Lets say I have 3 administrators and one of them creates a new user which is only a basic user permissions for the application and I want to show which admin registered that user. I have what I think is a correct migration for something like this but if someone could help me understand if there's a better way to go about this I would certain consider it.
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTables extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_accounts', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password', 60);
$table->string('avatar')->unique()->nullable();
$table->integer('status_id')->unsigned()->default(1);
$table->integer('role_id')->unsigned();
$table->integer('creator_id')->unsigned();
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
Schema::create('user_accounts_profiles', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->string('biography')->nullable();
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('state')->nullable();
$table->integer('postcode')->nullable();
$table->string('country')->nullable();
$table->string('phone')->nullable();
$table->timestamp('birthday')->nullable();
$table->string('facebook_username')->unique()->nullable();
$table->string('twitter_username')->unique()->nullable();
$table->string('google_plus_username')->unique()->nullable();
$table->string('behance_username')->unique()->nullable();
$table->string('pinterest_username')->unique()->nullable();
$table->string('linkedin_username')->unique()->nullable();
$table->string('github_username')->unique()->nullable();
$table->string('youtube_username')->unique()->nullable();
$table->string('instagram_username')->unique()->nullable();
$table->string('external_link')->unique()->nullable();
$table->integer('creator_id')->unsigned();
$table->timestamps();
$table->softDeletes();
});
Schema::create('user_accounts_statuses', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('label')->unique();
$table->integer('creator_id')->unsigned();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('user_accounts');
Schema::drop('user_accounts_profiles');
Schema::drop('user_accounts_statuses');
}
}
<?php
use App\UserAccountStatus;
use Illuminate\Database\Seeder;
class UserAccountStatusesTableSeeder extends Seeder
{
public function run()
{
factory(UserAccountStatus::class)->create(['name' => 'Unconfirmed', 'label' => 'unconfirmed']);
factory(UserAccountStatus::class)->create(['name' => 'Active', 'label' => 'active']);
factory(UserAccountStatus::class)->create(['name' => 'Inactive', 'label' => 'inactive']);
factory(UserAccountStatus::class)->create(['name' => 'Suspended', 'label' => 'suspended']);
factory(UserAccountStatus::class)->create(['name' => 'Banned', 'label' => 'banned']);
}
}
<?php
use App\UserAccount;
use App\Role;
use App\UserAccountStatus;
use Illuminate\Database\Seeder;
class UserAccountsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$myUser = factory(UserAccount::class)
->create([
'first_name' => 'John',
'last_name' => 'Doe',
'username' => 'jdoe',
'email' => 'john@example.com',
'password' => 'testpass123',
'avatar' => 'johndoe.jpg',
'status_id' => 1,
'role_id' => 5,
'creator_id' => 1
]);
$myUser->profile()->create([ 'user_id' => 1 ]);
factory(UserAccount::class, 10)->create()->each(function($u) {
$u->profile()->save(factory('App\UserAccountProfile')->make());
});
}
}
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
private $tables = [
'user_accounts',
'user_accounts_profiles',
'user_accounts_statuses',
];
private $seeders = [
'UserAccountsTableSeeder',
'UserAccountStatusesTableSeeder'
];
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->cleanDatabase();
Model::unguard();
foreach($this->seeders as $seeder)
{
$this->call($seeder);
}
Model::reguard();
}
private function cleanDatabase()
{
DB::statement('SET FOREIGN_KEY_CHECKS=0');
foreach ($this->tables as $tableName)
{
$this->command->info('Deleting existing ' . $tableName . ' data ...');
DB::table($tableName)->truncate();
}
DB::statement('SET FOREIGN_KEY_CHECKS=1');
}
}
Please or to participate in this conversation.