Auto-incrementing UNSIGNED BIGINT (primary key) => $table->bigIncrements('id');
DATE => $table->date('created_at');
DATETIME => $table->dateTime('created_at');
VARCHAR => $table->string('name', 100);
TEXT => $table->text('description');
TIMESTAMP => $table->timestamp('added_on');
Allows (by default) NULL values to be inserted into the column
->nullable($value = true)
Specify a "default" value for the column
->default($value)
Add a comment
->comment('my comment')
Set INTEGER columns as auto-increment (primary key)
->autoIncrement()
2019年5月20日月曜日
2019年5月19日日曜日
laravel relation
class User extends Model
{
/**
* Get the phone record associated with the user.
*/
public function phone()
{
return $this->hasOne('App\Phone');
}
}
one to one
hasOne/belongsTo
one to many
hasMany/belongsTo
many to many
belongsToMany/belongsToMany
$shop = Shop::find($shop_id);
$shop->products()->attach($product_id);
$shop->products()->detach($product_id);
we may access the intermediate table using the pivot attribute on the models:
foreach ($user->roles as $role) {
echo $role->pivot->created_at;
}
Notice that each Role model we retrieve is automatically assigned a pivot attribute. This attribute contains a model representing the intermediate table, and may be used like any other Eloquent model.
Filtering Relationships Via Intermediate Table Columns
return $this->belongsToMany('App\Role')->wherePivot('approved', 1);
return $this->belongsToMany('App\Role')->wherePivotIn('priority', [1, 2]);
$comment = new App\Comment(['message' => 'A new comment.']);
$post = App\Post::find(1);
$post->comments()->save($comment);
$post->comments()->saveMany([
new App\Comment(['message' => 'A new comment.']),
new App\Comment(['message' => 'Another comment.']),
]);
$post = App\Post::find(1);
$post->comments[0]->message = 'Message';
$post->comments[0]->author->name = 'Author Name';
$post->push();
the difference between save and create is that save accepts a full Eloquent model instance while create accepts a plain PHP array:
$post = App\Post::find(1);
$comment = $post->comments()->create([
'message' => 'A new comment.',
]);
$post->comments()->createMany([
[
'message' => 'A new comment.',
],
[
'message' => 'Another new comment.',
],
]);
$account = App\Account::find(10);
$user->account()->associate($account);
$user->save();
$user->account()->dissociate();
$user->save();
$user->roles()->attach($roleId);
When attaching a relationship to a model, you may also pass an array of additional data to be inserted into the intermediate table:
$user->roles()->attach($roleId, ['expires' => $expires]);
$user->roles()->detach($roleId);
// Detach all roles from the user...
$user->roles()->detach();
When working with a many-to-many relationship, the save method accepts an array of additional intermediate table attributes as its second argument:
App\User::find(1)->roles()->save($role, ['expires' => $expires]);
updateExistingPivot method. This method accepts the pivot record foreign key and an array of attributes to update:
$user = App\User::find(1);
$user->roles()->updateExistingPivot($roleId, $attributes);
{
/**
* Get the phone record associated with the user.
*/
public function phone()
{
return $this->hasOne('App\Phone');
}
}
one to one
hasOne/belongsTo
one to many
hasMany/belongsTo
many to many
belongsToMany/belongsToMany
$shop = Shop::find($shop_id);
$shop->products()->attach($product_id);
$shop->products()->detach($product_id);
we may access the intermediate table using the pivot attribute on the models:
foreach ($user->roles as $role) {
echo $role->pivot->created_at;
}
Notice that each Role model we retrieve is automatically assigned a pivot attribute. This attribute contains a model representing the intermediate table, and may be used like any other Eloquent model.
Filtering Relationships Via Intermediate Table Columns
return $this->belongsToMany('App\Role')->wherePivot('approved', 1);
return $this->belongsToMany('App\Role')->wherePivotIn('priority', [1, 2]);
Inserting & Updating Related Models
setting the post_id attribute on the Comment$comment = new App\Comment(['message' => 'A new comment.']);
$post = App\Post::find(1);
$post->comments()->save($comment);
$post->comments()->saveMany([
new App\Comment(['message' => 'A new comment.']),
new App\Comment(['message' => 'Another comment.']),
]);
$post = App\Post::find(1);
$post->comments[0]->message = 'Message';
$post->comments[0]->author->name = 'Author Name';
$post->push();
the difference between save and create is that save accepts a full Eloquent model instance while create accepts a plain PHP array:
$post = App\Post::find(1);
$comment = $post->comments()->create([
'message' => 'A new comment.',
]);
$post->comments()->createMany([
[
'message' => 'A new comment.',
],
[
'message' => 'Another new comment.',
],
]);
Belongs To Relationships
When updating a belongsTo relationship, you may use the associate method. This method will set the foreign key on the child model:$account = App\Account::find(10);
$user->account()->associate($account);
$user->save();
$user->account()->dissociate();
$user->save();
Many To Many Relationships
$user = App\User::find(1);$user->roles()->attach($roleId);
When attaching a relationship to a model, you may also pass an array of additional data to be inserted into the intermediate table:
$user->roles()->attach($roleId, ['expires' => $expires]);
$user->roles()->detach($roleId);
// Detach all roles from the user...
$user->roles()->detach();
When working with a many-to-many relationship, the save method accepts an array of additional intermediate table attributes as its second argument:
App\User::find(1)->roles()->save($role, ['expires' => $expires]);
updateExistingPivot method. This method accepts the pivot record foreign key and an array of attributes to update:
$user = App\User::find(1);
$user->roles()->updateExistingPivot($roleId, $attributes);
2019年5月17日金曜日
lavavel blade
@extends
@section
@show
@yield
@parent
@component
@slot
@inculde, @includeIf, @includeWhen, @includeFirst
Hello, @{{ name }}.
In this example, the @ symbol will be removed by Blade; however, {{ name }} expression will remain untouched by the Blade engine, allowing it to instead be rendered by your JavaScript framework.
@verbatim
<div class="container">
Hello, {{ name }}.
</div>
@endverbatim
@if, @elseif, @else
@unless
@isset, @empty
@auth, @guest
@switch, @case, @break, @default
@for, @foreach, @forelse/@empty, @while
@continue, @break
@php
@csrf, @method
@error directive may be used to quickly check if validation error messages exist for a given attribute. Within an @error directive, you may echo the $message variable to display the error message:
<input id="title" type="text" class="@error('title') is-invalid @enderror">
@error('title')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
You may combine loops and includes into one line with Blade's @each directive:
@each('view.name', $jobs, 'job')
@each('view.name', $jobs, 'job', 'view.empty')
@inject directive may be used to retrieve a service from the Laravel service container.
@inject('metrics', 'App\Services\MetricsService')
<div>
Monthly Revenue: {{ $metrics->monthlyRevenue() }}.
</div>
@section
@show
@yield
@parent
@component
@slot
@inculde, @includeIf, @includeWhen, @includeFirst
Hello, @{{ name }}.
In this example, the @ symbol will be removed by Blade; however, {{ name }} expression will remain untouched by the Blade engine, allowing it to instead be rendered by your JavaScript framework.
@verbatim
<div class="container">
Hello, {{ name }}.
</div>
@endverbatim
@if, @elseif, @else
@unless
@isset, @empty
@auth, @guest
@switch, @case, @break, @default
@for, @foreach, @forelse/@empty, @while
@continue, @break
@php
@csrf, @method
@error directive may be used to quickly check if validation error messages exist for a given attribute. Within an @error directive, you may echo the $message variable to display the error message:
<input id="title" type="text" class="@error('title') is-invalid @enderror">
@error('title')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
You may combine loops and includes into one line with Blade's @each directive:
@each('view.name', $jobs, 'job')
@each('view.name', $jobs, 'job', 'view.empty')
@inject directive may be used to retrieve a service from the Laravel service container.
@inject('metrics', 'App\Services\MetricsService')
<div>
Monthly Revenue: {{ $metrics->monthlyRevenue() }}.
</div>
laravel Routing
The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the api middleware group.
register a route that responds to multiple HTTP verbs. You may do so using the match method. Or, you may even register a route that responds to all HTTP verbs using the any method:
Route::match(['get', 'post'], '/', function () {
//
});
Route::any('/', function () {
//
});
POST, PUT, or DELETE routes that are defined in the web routes file should include a CSRF token field.
Route::redirect('/here', '/there');
By default, Route::redirect returns a 302 status code. You may customize the status code using the optional third parameter:
Route::redirect('/here', '/there', 301);
You may use the Route::permanentRedirect method to return a 301 status code:
Route::permanentRedirect('/here', '/there');
Route::get('search/{search}', function ($search) {
return $search;
})->where('search', '.*');
Encoded forward slashes are only supported within the last route segment.
Route::namespace('Admin')->group(function () {
// Controllers Within The "App\Http\Controllers\Admin" Namespace
});
by default, the RouteServiceProvider includes your route files within a namespace group, allowing you to register controller routes without specifying the full App\Http\Controllers namespace prefix. So, you only need to specify the portion of the namespace that comes after the base App\Http\Controllers namespace.
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
In order to ensure your sub-domain routes are reachable, you should register sub-domain routes before registering root domain routes. This will prevent root domain routes from overwriting sub-domain routes which have the same URI path.
Route::prefix('admin')->group(function () {
Route::get('users', function () {
// Matches The "/admin/users" URL
});
});
Route::name('admin.')->group(function () {
Route::get('users', function () {
// Route assigned name "admin.users"...
})->name('users');
});
Route::fallback(function () {
//
});
The fallback route should always be the last route registered by your application.
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
register a route that responds to multiple HTTP verbs. You may do so using the match method. Or, you may even register a route that responds to all HTTP verbs using the any method:
Route::match(['get', 'post'], '/', function () {
//
});
Route::any('/', function () {
//
});
POST, PUT, or DELETE routes that are defined in the web routes file should include a CSRF token field.
Redirect Routes
If you are defining a route that redirects to another URI, you may use the Route::redirect method. This method provides a convenient shortcut so that you do not have to define a full route or controller for performing a simple redirect:Route::redirect('/here', '/there');
By default, Route::redirect returns a 302 status code. You may customize the status code using the optional third parameter:
Route::redirect('/here', '/there', 301);
You may use the Route::permanentRedirect method to return a 301 status code:
Route::permanentRedirect('/here', '/there');
View Routes
this method provides a simple shortcut so that you do not have to define a full route or controller.
Route::view('/welcome', 'welcome');
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
Route Parameters
Route::get('user/{id}', function ($id) {
return 'User '.$id;
});
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
//
});
not contain a - character. Instead of using the - character, use an underscore (_).
Optional Parameters
Make sure to give the route's corresponding variable a default value:
Route::get('user/{name?}', function ($name = null) {
return $name;
});
Route::get('user/{name?}', function ($name = 'John') {
return $name;
});
Regular Expression Constraints
Route::get('user/{name}', function ($name) {
//
})->where('name', '[A-Za-z]+');
Route::get('user/{id}', function ($id) {
//
})->where('id', '[0-9]+');
Route::get('user/{id}/{name}', function ($id, $name) {
//
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
Encoded Forward Slashes
The Laravel routing component allows all characters except /. You must explicitly allow / to be part of your placeholder using a where condition regular expression:Route::get('search/{search}', function ($search) {
return $search;
})->where('search', '.*');
Encoded forward slashes are only supported within the last route segment.
Named Routes
Route::get('user/profile', 'UserProfileController@show')->name('profile');
// Generating URLs...
$url = route('profile');
// Generating Redirects...
return redirect()->route('profile');
Route Groups
Route::middleware(['first', 'second'])->group(function () {
Route::get('/', function () {
// Uses first & second Middleware
});
Route::get('user/profile', function () {
// Uses first & second Middleware
});
});
Route::namespace('Admin')->group(function () {
// Controllers Within The "App\Http\Controllers\Admin" Namespace
});
by default, the RouteServiceProvider includes your route files within a namespace group, allowing you to register controller routes without specifying the full App\Http\Controllers namespace prefix. So, you only need to specify the portion of the namespace that comes after the base App\Http\Controllers namespace.
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
In order to ensure your sub-domain routes are reachable, you should register sub-domain routes before registering root domain routes. This will prevent root domain routes from overwriting sub-domain routes which have the same URI path.
Route::prefix('admin')->group(function () {
Route::get('users', function () {
// Matches The "/admin/users" URL
});
});
Route::name('admin.')->group(function () {
Route::get('users', function () {
// Route assigned name "admin.users"...
})->name('users');
});
Fallback Routes
define a route that will be executed when no other route matches the incoming request.Route::fallback(function () {
//
});
The fallback route should always be the last route registered by your application.
Accessing The Current Route
$route = Route::current();$name = Route::currentRouteName();
$action = Route::currentRouteAction();
laravel eloquent
Each database table has a corresponding "Model" which is used to interact with that table.
php artisan make:model Flight --migration
php artisan make:model Flight -m
protected $table = 'my_flights';
protected $primaryKey = 'flight_id';
public $incrementing = false;
protected $keyType = 'string';
By default, Eloquent expects created_at and updated_at columns to exist on your tables.
public $timestamps = false;
If you need to customize the format of your timestamps, set the $dateFormat property on your model. This property determines how date attributes are stored in the database
protected $dateFormat = 'U';
const CREATED_AT = 'creation_date';
const UPDATED_AT = 'last_update';
'delayed' => false,
];
App\Flight::all();
$flights = App\Flight::find([1, 2, 3]);
$flights = App\Flight::where('active', 1)
->orderBy('name', 'desc')
->take(10)
->get();
$flight = App\Flight::where('active', 1)->first();
$model = App\Flight::where('legs', '>', 100)->firstOrFail();
If the exception is not caught, a 404 HTTP response is automatically sent back to the user. It is not necessary to write explicit checks to return 404 responses when using these methods
$count = App\Flight::where('active', 1)->count();
$max = App\Flight::where('active', 1)->max('price');
php artisan make:model Flight --migration
php artisan make:model Flight -m
protected $table = 'my_flights';
protected $primaryKey = 'flight_id';
public $incrementing = false;
protected $keyType = 'string';
By default, Eloquent expects created_at and updated_at columns to exist on your tables.
public $timestamps = false;
If you need to customize the format of your timestamps, set the $dateFormat property on your model. This property determines how date attributes are stored in the database
protected $dateFormat = 'U';
const CREATED_AT = 'creation_date';
const UPDATED_AT = 'last_update';
Default Attribute Values
protected $attributes = ['delayed' => false,
];
App\Flight::all();
$flights = App\Flight::find([1, 2, 3]);
$flights = App\Flight::where('active', 1)
->orderBy('name', 'desc')
->take(10)
->get();
$flight = App\Flight::where('active', 1)->first();
Not Found Exceptions
$model = App\Flight::findOrFail(1);$model = App\Flight::where('legs', '>', 100)->firstOrFail();
If the exception is not caught, a 404 HTTP response is automatically sent back to the user. It is not necessary to write explicit checks to return 404 responses when using these methods
Retrieving Aggregates
You may also use the count, sum, max, and other aggregate methods provided by the query builder. These methods return the appropriate scalar value instead of a full model instance:$count = App\Flight::where('active', 1)->count();
$max = App\Flight::where('active', 1)->max('price');
Inserts
$flight = new Flight;
$flight->name = $request->name;
$flight->save();
App\Flight::where('active', 1)
->where('destination', 'San Diego')
->update(['delayed' => 1]);
$flight->name = $request->name;
$flight->save();
Updates
$flight = App\Flight::find(1);
$flight->name = 'New Flight Name';
$flight->save();
App\Flight::where('active', 1)
->where('destination', 'San Diego')
->update(['delayed' => 1]);
Mass Assignment
You may also use the create method to save a new model in a single line. before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.
protected $fillable = ['name'];
$flight = App\Flight::create(['name' => 'Flight 10']);
If you already have a model instance, you may use the fill method to populate it with an array of attributes:
$flight->fill(['name' => 'Flight 22']);
protected $guarded = ['price'];
protected $guarded = [];
you should use either $fillable or $guarded - not both.
Deleting Models
$flight = App\Flight::find(1);
$flight->delete();
App\Flight::destroy(1);
App\Flight::destroy(1, 2, 3);
App\Flight::destroy([1, 2, 3]);
App\Flight::destroy(collect([1, 2, 3]));
$deletedRows = App\Flight::where('active', 0)->delete();
laravel validation
use the validate method provided by the Illuminate\Http\Request object. If the validation rules pass, your code will keep executing normally; however, if validation fails, an exception will be thrown and the proper error response will automatically be sent back to the user. In the case of a traditional HTTP request, a redirect response will be generated, while a JSON response will be sent for AJAX requests.
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
retrieve an array of all the messages for a given field, use the get method:
foreach ($errors->get('email') as $message) {
//
}
foreach ($errors->all() as $message) {
//
}
if ($errors->has('email')) {
//
}
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
Stopping On First Validation Failure
Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute:
$request->validate([
'title' => 'bail|required|unique:posts|max:255',
'body' => 'required',
]);
If your HTTP request contains "nested" parameters, you may specify them in your validation rules using "dot" syntax:
$request->validate([
'title' => 'required|unique:posts|max:255',
'author.name' => 'required',
'author.description' => 'required',
]);
Displaying The Validation Errors
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
You may also use the @error Blade directive(form 5.8.13)to quickly check if validation error messages exist for a given attribute.
<input id="title" type="text" class="@error('title') is-invalid @enderror">
@error('title')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
echo $errors->first('email');
Working With Error Messages
$errors = $validator->errors();echo $errors->first('email');
retrieve an array of all the messages for a given field, use the get method:
foreach ($errors->get('email') as $message) {
//
}
foreach ($errors->all() as $message) {
//
}
if ($errors->has('email')) {
//
}
Manually Creating Validators
If you do not want to use the validate method on the request, you may create a validator instance manually using the Validator facade.
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
If you would like to create a validator instance manually but still take advantage of the automatic redirection offered by the requests's validate method, you may call the validate method on an existing validator instance. If validation fails, the user will automatically be redirected or, in the case of an AJAX request, a JSON response will be returned:
Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
])->validate();
If you would like to create a validator instance manually but still take advantage of the automatic redirection offered by the requests's validate method, you may call the validate method on an existing validator instance. If validation fails, the user will automatically be redirected or, in the case of an AJAX request, a JSON response will be returned:
Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
])->validate();
Custom Error Messages
$messages = [
'email.required' => 'We need to know your e-mail address!',
];
$validator = Validator::make($input, $rules, $messages);
Validating When Present
email field will only be validated if it is present in the $data array.
$v = Validator::make($data, [
'email' => 'sometimes|required|email',
]);
$v = Validator::make($data, [
'email' => 'required|email',
'games' => 'required|numeric',
]);
$v->sometimes('reason', 'required|max:500', function ($input) {
return $input->games >= 100;
});
The first argument passed to the sometimes method is the name of the field we are conditionally validating. The second argument is the rules we want to add. If the Closure passed as the third argument returns true, the rules will be added.
A Note On Optional Fields
By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the App\Http\Kernel class. Because of this, you will often need to mark your "optional" request fields as nullable if you do not want the validator to consider null values as invalid.laravel request
// http://domain.com/foo/bar, the path method will return foo/bar
$request->path();
// verify that the incoming request path matches a given pattern
if ($request->is('admin/*')) {
//
}
// Without Query String...
$url = $request->url();
// With Query String...
$url = $request->fullUrl();
// method
if ($request->isMethod('post')) {
//
}
By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware will automatically trim all incoming string fields on the request, as well as convert any empty string fields to null.
$input = $request->all(); // may same with $request->input();
// default value 'Sally'
$name = $request->input('name', 'Sally');
When working with forms that contain array inputs, use "dot" notation to access the arrays:
$name = $request->input('products.0.name');
$names = $request->input('products.*.name');
While the input method retrieves values from entire request payload (including the query string), the query method will only retrieve values from the query string:
$name = $request->query('name');
or $name = $request->name; // dynamic properties
When using dynamic properties, Laravel will first look for the parameter's value in the request payload. If it is not present, Laravel will search for the field in the route parameters.
if ($request->has('name')) {
//
}
If you would like to determine if a value is present on the request and is not empty, you may use the filled method:
if ($request->filled('name')) {
//
}
$request->path();
// verify that the incoming request path matches a given pattern
if ($request->is('admin/*')) {
//
}
// Without Query String...
$url = $request->url();
// With Query String...
$url = $request->fullUrl();
// method
if ($request->isMethod('post')) {
//
}
By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware will automatically trim all incoming string fields on the request, as well as convert any empty string fields to null.
$input = $request->all(); // may same with $request->input();
// default value 'Sally'
$name = $request->input('name', 'Sally');
When working with forms that contain array inputs, use "dot" notation to access the arrays:
$name = $request->input('products.0.name');
$names = $request->input('products.*.name');
While the input method retrieves values from entire request payload (including the query string), the query method will only retrieve values from the query string:
$name = $request->query('name');
or $name = $request->name; // dynamic properties
When using dynamic properties, Laravel will first look for the parameter's value in the request payload. If it is not present, Laravel will search for the field in the route parameters.
if ($request->has('name')) {
//
}
If you would like to determine if a value is present on the request and is not empty, you may use the filled method:
if ($request->filled('name')) {
//
}
Retrieving Old Input
$username = $request->old('username');
<input type="text" name="username" value="{{ old('username') }}">
登録:
投稿 (Atom)