12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel コントローラをtinkerから実行する

Last updated at Posted at 2021-09-17

tinkerからコントローラを実行できると便利なので、その方法をご紹介します。

環境

  • PHP: 8.0.10
  • Laravel: 8.58.0

関連記事

モデル、コントローラを作る

$ php artisan make:model Book
$ php artisan make:controller BookController
  • app/Models/Book.php
  • app/Http/Controllers/BookController.php

index: 引数なしのメソッドを実行する

app/Http/Controllers/BookController.php
<?php declare(strict_types=1);

namespace App\Http\Controllers;

use App\Models\Book;
use Illuminate\Http\Request;

final class BookController extends Controller
{
    /**
     * @return array
     */
    public function index(): array
    {
        return [
            'data' => [
                'id' => 1,
                'name' => 'PHP Book',
            ]
        ];
    }
}

だいぶ適当な関数ですが、これをtinkerから実行してみます。

$ php artisan tinker

>>> $controller = app()->make(\App\Http\Controllers\BookController::class);
=> App\Http\Controllers\BookController {#3449}

>>> app()->call([$controller, 'index']);
=> [
     "data" => [
       "id" => 1,
       "name" => "PHP Book",
     ],
   ]

コントローラをtinkerから実行できました!

store: Requestオブジェクトのあるメソッドを実行する

app/Http/Controllers/BookController.php
<?php declare(strict_types=1);

namespace App\Http\Controllers;

use App\Models\Book;
use Illuminate\Http\Request;

final class BookController extends Controller
{
    /**
     * @param Request $request
     * @return array
     */
    public function store(Request $request): array
    {
        return [
            'data' => $request->all(),
        ];
    }
}

Request オブジェクトが必要な時の例です。

$ php artisan tinker

>>> $params = ['name' => 'Laravel Book'];
=> [
     "name" => "Laravel Book",
   ]

>>> $request = \Illuminate\Http\Request::create('/books', 'POST', $params);
=> Illuminate\Http\Request {#3452
     +attributes: Symfony\Component\HttpFoundation\ParameterBag {#3448},
     +request: Symfony\Component\HttpFoundation\InputBag {#3450},
     +query: Symfony\Component\HttpFoundation\InputBag {#3449},
     +server: Symfony\Component\HttpFoundation\ServerBag {#3441},
     +files: Symfony\Component\HttpFoundation\FileBag {#3440},
     +cookies: Symfony\Component\HttpFoundation\InputBag {#3446},
     +headers: Symfony\Component\HttpFoundation\HeaderBag {#3444},
   }

>>> $controller = app()->make(\App\Http\Controllers\BookController::class);
=> App\Http\Controllers\BookController {#3442}

>>> app()->call([$controller, 'store'], ['request' => $request]);
=> [
     "data" => [
       "name" => "Laravel Book",
     ],
   ]

show: Bookオブジェクトのあるメソッドを実行する

app/Http/Controllers/BookController.php
<?php declare(strict_types=1);

namespace App\Http\Controllers;

use App\Models\Book;
use Illuminate\Http\Request;

final class BookController extends Controller
{
   /**
     * @param Book $book
     * @return array
     */
    public function show(Book $book): array
    {
        return [
            'data' => [
                'id' => $book->id,
                'name' => $book->name,
            ],
        ];
    }
}

Book モデルのオブジェクトが必要な時の例です。

$ php artisan tinker

>>> $book = \App\Models\Book::factory()->make(['id' => 1, 'name' => 'PHP Book', 'created_at' => now(), 'updated_at' => now()]);

=> App\Models\Book {#4379
     id: 1,
     name: "PHP Book",
     created_at: "2021-09-01 00:00:00",
     updated_at: "2021-09-01 00:00:00",
   }

>>> $controller = app()->make(\App\Http\Controllers\BookController::class);
=> App\Http\Controllers\BookController {#4384}

>>> app()->call([$controller, 'show'], ['book' => $book]);
=> [
     "data" => [
       "id" => 1,
       "name" => "PHP Book",
     ],
   ]

今回はDB作ってないので、Book::factory() でBookインスタンスだけ作ってます。
DBがある場合は $book = \App\Models\Book::find(1); 等でBookインスタンス取得してください。

参考

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?