Skip to content

Instantly share code, notes, and snippets.

@astrsk-hori
Last active August 29, 2015 14:19
Show Gist options
  • Save astrsk-hori/0f81662a94205a2345b8 to your computer and use it in GitHub Desktop.
Save astrsk-hori/0f81662a94205a2345b8 to your computer and use it in GitHub Desktop.
laravel5を試してみる(メモ) ref: http://qiita.com/astrsk_hori/items/c3748c588164ab0ebd1b
composer create-project laravel/laravel la5test --prefer-dist
config/database.php
resources/views
resources/views/home.blade.php
resources/views/tasks/index.blade.php
php artisan tinker
$ php artisan route:list
+--------+--------------------------------+-------------------------------------------------------+--------------+------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+--------------------------------+-------------------------------------------------------+--------------+------------------------------------------------------------+------------+
| | GET|HEAD | / | | App\Http\Controllers\WelcomeController@index | guest |
| | GET|HEAD | home | | App\Http\Controllers\HomeController@index | auth |
| | GET|HEAD | task | task.index | App\Http\Controllers\TaskController@index | |
| | GET|HEAD | task/create | task.create | App\Http\Controllers\TaskController@create | |
| | POST | task | task.store | App\Http\Controllers\TaskController@store | |
| | GET|HEAD | task/{task} | task.show | App\Http\Controllers\TaskController@show | |
| | GET|HEAD | task/{task}/edit | task.edit | App\Http\Controllers\TaskController@edit | |
| | PUT | task/{task} | task.update | App\Http\Controllers\TaskController@update | |
| | PATCH | task/{task} | | App\Http\Controllers\TaskController@update | |
| | DELETE | task/{task} | task.destroy | App\Http\Controllers\TaskController@destroy | |
| | GET|HEAD | auth/register/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController@getRegister | guest |
| | POST | auth/register/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController@postRegister | guest |
| | GET|HEAD | auth/login/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController@getLogin | guest |
| | POST | auth/login/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController@postLogin | guest |
| | GET|HEAD | auth/logout/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\AuthController@getLogout | |
| | GET|HEAD|POST|PUT|PATCH|DELETE | auth/{_missing} | | App\Http\Controllers\Auth\AuthController@missingMethod | guest |
| | GET|HEAD | password/email/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\PasswordController@getEmail | guest |
| | POST | password/email/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\PasswordController@postEmail | guest |
| | GET|HEAD | password/reset/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\PasswordController@getReset | guest |
| | POST | password/reset/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\Auth\PasswordController@postReset | guest |
| | GET|HEAD|POST|PUT|PATCH|DELETE | password/{_missing} | | App\Http\Controllers\Auth\PasswordController@missingMethod | guest |
+--------+--------------------------------+-------------------------------------------------------+--------------+------------------------------------------------------------+------------+
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Task;
use Illuminate\Http\Request;
class TaskController extends Controller {
protected $tasks;
public function __construct(Task $tasks)
{
$this->tasks = $tasks;
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$tasks = $this->tasks->all();
return view('tasks.index', ['tasks' => $tasks]);
}
http://localhost/task
php artisan tinker
>>> $task = App::make('\App\Task');
=> <App\Task #000000003956511c000000011e1b911e> {}
>>> $task->title = 'test title';
=> "test title"
>>> $task->body = '本文です';
=> "本文です"
>>> $task->save();
=> true
>>> $task->all();
=> <Illuminate\Database\Eloquent\Collection #0000000039565101000000011e1b911e> [
<App\Task #00000000395651f1000000011e1b911e> {
id: 1,
title: "test title",
body: "本文です",
created_at: "2015-04-16 06:30:57",
updated_at: "2015-04-16 06:30:57"
}
]
>>>
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Task;
use Illuminate\Http\Request;
class TaskController extends Controller {
protected $tasks;
public function __construct(Task $tasks)
{
$this->tasks = $tasks;
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$tasks = $this->tasks->all();
return view('tasks.index', ['tasks' => $tasks]);
}
php artisan migrate --env=testing
php artisan key:generate --env=testing
use App\Task;
create database la5test default character set utf8;
@extends('app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Task List</div>
<div class="panel-body">
<ul class="list-group">
@foreach ($tasks as $task)
<li class="list-group-item">{{$task->title}}</li>
@endforeach
</ul>
</div>
</div>
</div>
</div>
</div>
@endsection
php artisan migrate --env=testing
php artisan key:generate --env=testing
DB_HOST=localhost
DB_DATABASE=la5test
DB_USERNAME=hoge
DB_PASSWORD=hogehoge
php artisan make:model Task
php artisan make:model models/Task
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTasksTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('tasks');
}
}
php artisan migrate
mysql> show tables;
+-------------------+
| Tables_in_la5test |
+-------------------+
| migrations |
| password_resets |
| tasks |
| users |
+-------------------+
php artisan make:controller TaskController
Route::resource('task', 'TaskController');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment