Form::openで直接メソッドを指定する。
{{ Form::open(['action' => ['UserController@change', $user->id]]) }} {{ Form::close() }}
これにはroute.phpへの記述も必要。
Route::post('user/change', 'UserController@change');
Pythonのsubprocessを使って標準出力を入手したい。
import subprocess cmdline = 'echo test' p = subprocess.Popen(cmdline, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) while True: line = p.stdout.readline() if not line: break print(line.strip())
ところが、この場合「test」ではなく「b'test'」と表示される。
b'test'
デコードすれば結果だけを表示できる。
print(line.strip().decode('ascii'))
artisan用のコマンドは名前が変わって「コンソール」になった。なのでmake:consoleでコマンドを作る。
php artisan make:console FutariUpdate --command=futari:update
app\Console\Kernel.phpにコマンドを登録する。
use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { protected $commands = [ 'Shoek\Console\Commands\Inspire', 'Shoek\Console\Commands\FutariUpdate', ];
LaravelのSentryで以下のエラーが出た。
The [login] attribute is required
原因はlogin_attributeの設定を間違えていたことだった。
'login_attribute' => 'email',
LaravelのSeederを使ってSentry用のユーザを一気に追加しようとしたら、以下のエラーメッセージが出た。
[Cartalyst\Sentry\Users\LoginRequiredException] A login is required for a user, none given.
そのときのSeederはこちら。
<?php use League\Csv\Reader; class UserSeeder extends Seeder { public function run() { $reader = Reader::createFromPath('./import/user.csv'); $csvs = $reader->setDelimiter("\t")->fetchAssoc($reader->fetchOne()); array_shift($csvs); foreach ($csvs as $csv) { Sentry::createUser($csv); } } } <|| CSVファイルの中身はこちら。 >|| username email password MyUserName1 MyEmailAddress1 MyPassword1 MyUserName2 MyEmailAddress2 MyPassword2
原因はSentry::createUserにactivatedの情報が無かったから。そこでCSVファイルの1行目にactivatedを足して、その値を1にした。
username email password activated MyUserName1 MyEmailAddress1 MyPassword1 1 MyUserName2 MyEmailAddress2 MyPassword2 1
migrateにどうしても失敗する場合がある。以下みたいなメッセージ。
php artisan migrate Do you really wish to run this command? yes {"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException", "message":"Class 'CreateUsersTable' not found", "file":"vendor\/laravel\/framework\/src\/Illuminate\/Database\/Migrations\/Migrator.php", "line":301}}
上記の場合だと「CreateUsersTable」が「vendor\composer」フォルダ内に登録されてないのが原因。
composer dump-autoload
で直る。
自作コマンドを作成してみた。
php artisan command:make testFutari --command=test:futari
上記コマンドで生成されたソースにfire()とgetArguments()とgetOptions()の中身を追記した。
<?php use Illuminate\Console\Command; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; class testFutari extends Command { /** * The console command name. * * @var string */ protected $name = 'test:futari'; /** * The console command description. * * @var string */ protected $description = 'Command description.'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function fire() { print 'example = ' . $this->argument('example') . "\n"; print 'optional = ' . $this->option('optional') . "\n"; print 'none = ' . $this->option('none') . "\n"; } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return array( array('example', InputArgument::REQUIRED, 'An example argument.'), ); } /** * Get the console command options. * * @return array */ protected function getOptions() { return array( array('optional', '-o', InputOption::VALUE_OPTIONAL, 'An example option.', null), array('none', null, InputOption::VALUE_NONE, 'An example option.', null), ); } }
そしてコマンドを実行したらこんな結果になった。
$ php artisan test:futari aaa -o bbb --none example = aaa optional = bbb none = 1
ちなみにソース上でコマンドを実行する時は以下のように書く。
Artisan::call('test:futari', [ 'example' => 'aaa', '-o' => 'bbb', '--none' => true ]);
Laravelでデータを削除するときのForm::openの書き方
{{ Form::open([ 'route' => ['comments.destroy',$comment->id], 'method' => 'delete', 'class'=>'form-horizontal' ]) }} <p><button type="submit" class="btn-u btn-u-blue">DELETE</button></p> {{ Form::close() }}
LaravelのEloquentでwhereInを使う。例えば4つのIDから最小の値段を取得する場合。
$nedan = Title::whereIn('id',[10,11,12,13])->min('nedan');
Laravelのソース内でArtisanコマンドを使う。
Artisan::call('mybench:mycommand');
引数付きで使う例。
Artisan::call('mybench:mycommand', ['year' => 2014]);
引数付きで使う場合、自作コマンド側の引数受け取り部分はこうなる。
/** * Get the console command arguments. * * @return array */ protected function getArguments() { return array( ['year', InputArgument::OPTIONAL, '年'] ); }