ownmaster

Experience

330

0 Best Reply Awards

  • Member Since 7 Months Ago
  • 0 Lessons Completed
  • 0 Favorites

19th February, 2018

ownmaster left a reply on Save Data In Multiple Tables At Once • 7 months ago

@Snapey Thanks. Here is the sample code for future thread readers:

DB::transaction(function () use($properties, $permissions) {
    $role = Role::create($properties);
    $role->permissions()->attach($permissions);
});

16th February, 2018

ownmaster left a reply on Symphony Process Fails From Artisan Development Server On Windows 10 • 7 months ago

@bobbybouwmann Yes, I do. And always run php without providing full path. I tested it with PHP 7.1.11 and 7.2.2. I also checked the problem on a clean Laravel 5.6 project and it exists there too.

ownmaster started a new conversation Symphony Process Fails From Artisan Development Server On Windows 10 • 7 months ago

Laravel 5.6.

Symfony (new Process('php -v'))->mustRun()->getOutput() ('php -v' can be replaced by any other command - result is same) works correctly from php artisan tinker or php artisan serve on Debian but on Windows 10 works only from tinker, and fails from local website (php artisan serve):

Route::get('/', function () {
    return (new Symfony\Component\Process\Process('php -v'))->mustRun()->getOutput();
});

Error:

The command "php -v" failed. Exit Code: 1(General error) 'php' is not recognized as an internal or external command, operable program or batch file

I also tried running php artisan serve from elevated command prompt and got same result. Is there a way to fix it?

ownmaster left a reply on Save Data In Multiple Tables At Once • 7 months ago

@Snapey Do you mean DB:transaction()? Can I use Eloquent methods inside the transaction? Because Laravel examples use only DB:: methods.

@Varsang Well, this structure is quite common and is used by, for example, Entrust.

15th February, 2018

ownmaster started a new conversation Save Data In Multiple Tables At Once • 7 months ago

Image this scenario. There are roles and permissions, and relation is many to many. That means, there is role table, permission' table and relation tablerole_permission`. Relations of Eloquent classes are as follows:

class Role extends Model
{
  public function permissions()
  {
    return $this->belongsToMany('App\Permission');
  }
}
class Permission extends Model
{
  public function roles()
  {
    return $this->belongsToMany('App\Role');
  }
}

Now, there is a fixed set of permissions, based on which I want to create different roles. It means that I need to insert 1 record into role table and multiple records into the role_permission table. I do it like this (assuming I have a list of permissions IDs):

$role = Role::create($properties); // insert
$role->permissions()->attach($permissions); // insert

The questions is: can I save all this information at once, like in one transaction? The expected code could look like this:

$role = new Role($properties);
$permissionObjects = Permission()::findMany($permissions);
$role->permissions()->associate($permissionObjects);
$role->save(); // insert

1st February, 2018

ownmaster started a new conversation Strange Index In Relation Table • 7 months ago

I can't understand index creation logic when I create a relation table in MySQL using Laravel.

Below is the migrations code for 3 tables: a, b and a_b which is a relation table:

Schema::create('a', function (Blueprint $table) {
            $table->increments('id');
        });
...
Schema::create('b', function (Blueprint $table) {
            $table->increments('id');
        });
...
Schema::create('a_b', function (Blueprint $table) {
      $table->integer('a_id')->unsigned();
      $table->integer('b_id')->unsigned();

      $table->foreign('a_id')->references('id')->on('a')
        ->onUpdate('cascade')->onDelete('cascade');
      $table->foreign('b_id')->references('id')->on('b')
        ->onUpdate('cascade')->onDelete('cascade');

      $table->primary(['a_id', 'b_id'], 'pk_a_b');
    });

Tables creation order: a, b and finally a_b.

--pretend option shows these instructions:

create table `a_b` (`a_id` int unsigned not null, `b_id` int unsigned not null) default character set utf8mb4 collate utf8mb4_unicode_ci
alter table `a_b` add constraint `a_b_a_id_foreign` foreign key (`a_id`) references `a` (`id`) on delete cascade on update cascade
alter table `a_b` add constraint `a_b_b_id_foreign` foreign key (`b_id`) references `b` (`id`) on delete cascade on update cascade
alter table `a_b` add primary key `pk_a_b`(`a_id`, `b_id`)

The resulting relation table produced by Laravel:

CREATE TABLE `a_b` (
  `a_id` int(10) unsigned NOT NULL,
  `b_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`a_id`,`b_id`),
  KEY `a_b_b_id_foreign` (`b_id`),
  CONSTRAINT `a_b_a_id_foreign` FOREIGN KEY (`a_id`) REFERENCES `a` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `a_b_b_id_foreign` FOREIGN KEY (`b_id`) REFERENCES `b` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

As you can see, additional index was created on column b_id:

KEY `a_b_b_id_foreign` (`b_id`)

The questions are:

  1. Why was it created? I didn't ask for it in migration file.
  2. Why there was only 1 index created
  3. Why it was created on b_id column, not on a_id?

29th January, 2018

ownmaster left a reply on Is It Possible To Combine Route::resource() With Middleware Options For Each Action (particularly - Permissions)? • 7 months ago

@arukomp I've put three dots in my code sample :) - I have all 7 actions and I have several such objects (each with 6-7 standard actions). But, I see that there is no syntax which will allow accomplish my goals, so there is nothing to do.

26th January, 2018

ownmaster left a reply on Is It Possible To Combine Route::resource() With Middleware Options For Each Action (particularly - Permissions)? • 7 months ago

Well, as I mentioned in initial post - I would like to keep all this information in one file (in my case - routes/web.php).

ownmaster started a new conversation Is It Possible To Combine Route::resource() With Middleware Options For Each Action (particularly - Permissions)? • 7 months ago

Route::resource() by default produces 7 routes for corresponding actions. For example, if I use Entrust and want configure permissions for each action method, then I can define each action route manually

Route::get('photos', ['as'=>'photos.index', 'uses'=>'[email protected]', 'middleware' => ['permission:photos-list']]);
Route::get('photos/create', ['as'=>'photos.create', 'uses'=>'[email protected]', 'middleware' => ['permission:photos-create']]);
...
Route::delete('photos/{photo}', ['as'=>'photos.destroy', 'uses'=>'[email protected]', 'middleware' => ['permission:photos-delete']]);

or, I can use Route::resource() and then configure permissions in PhotosController constructor as described in this thread.

Now, the question is: is it possible to define these permissions for each action directly in the Route::resource() call so that it will look nice and compact (i.e. without using middleware() and group() methods)?

25th January, 2018

ownmaster left a reply on Assign Separate Middleware To Each Method Of A Resource In Laravel • 7 months ago

@willvincent

public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('permission:manage-posts', ['only' => ['create']]);
        $this->middleware('permission:edit-posts',   ['only' => ['edit']]);
        $this->middleware('permission:view-posts',   ['only' => ['show', 'index']]);
    }

Can similar compact syntax be used directly in the routes/web.php?

Or, in other words, is it possible to rewrite example below in a more simple way (without moving part of the code into controller's constructor)?

Route::get('photos', ['as'=>'photos.index', 'uses'=>'[email protected]', 'middleware' => ['permission:list']]);
Route::get('photos/create', ['as'=>'photos.create', 'uses'=>'[email protected]', 'middleware' => ['permission:create']]);
Route::post('photos/', ['as'=>'photos.store', 'uses'=>'[email protected]', 'middleware' => ['permission:create']]);
Route::get('photos/{id}', ['as'=>'photos.show', 'uses'=>'[email protected]', 'middleware' => ['permission:list']]);
Route::get('photos/{id}/edit', ['as'=>'photos.edit', 'uses'=>'[email protected]', 'middleware' => ['permission:edit']]);
Route::patch('photos/{id}', ['as'=>'photos.update', 'uses'=>'[email protected]', 'middleware' => ['permission:edit']]);
Route::delete('photos/{id}', ['as'=>'photos.destroy', 'uses'=>'[email protected]', 'middleware' => ['permission:delete']]);

ownmaster left a reply on Ensure Only One Single Record Is Returned By Eloquent Query • 7 months ago

No, the idea here is different.

Example #1. There is 1 employee with 'salary' = 1000 in the database. Employee::where('salary', 1000)->firstOrFail() will return this employee.

Example #2. There are 2 employees with 'salary' = 1000 in the database. Employee::where('salary', 1000)->firstOrFail() will return one of them.

Example #3. There are no employees with 'salary' = 1000 in the database. In this case `Employee::where('salary', 1000)->firstOrFail() will raise an exception.

The idea of method I'm talking about (singleOrFail()) is that it will raise an exception not only in example #3 (because there are no matching users), but also in example #2 (because there are many matching users).

ownmaster started a new conversation Ensure Only One Single Record Is Returned By Eloquent Query • 7 months ago

I checked Laravel collection methods and can't understand - is there a method for Eloquent query to ensure that exactly 1 record is returned - not 0 and not more than 1? Something like singleOrFail(). As an example, in Entity Framework there is First() which works like Laravel's firstOrFail() and there is also Single().

Or, is it not implemented (yet)?

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.