ownmaster

ownmaster

Member Since 10 Months Ago

Experience Points 330
Experience
Level
Lessons Completed 0
Lessons
Completed
Best Reply Awards 0
Best Answer
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • Chatty Cathy Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

19 Feb
9 months ago

ownmaster left a reply on Save Data In Multiple Tables At Once

@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);
});
16 Feb
9 months ago

ownmaster left a reply on Symphony Process Fails From Artisan Development Server On Windows 10

@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

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

@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.

15 Feb
10 months ago

ownmaster started a new conversation Save Data In Multiple Tables At Once

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
01 Feb
10 months ago

ownmaster started a new conversation Strange Index In Relation Table

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?
29 Jan
10 months ago

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

@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.

26 Jan
10 months ago

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

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)?

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)?

25 Jan
10 months ago

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

@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

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

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)?