分类 Laravel 下的文章

Laravel的部署,还是尽量用docker吧,奈何哥不会啊。
所以目前就是一点点折腾了,有一些细节需要注意:

1、配置.env文件:

php artisan key:generate //这是生成Laravel的APP_KEY
php artisan jwt:secret // 在.env文件里添加一行JWT_SECRET=foobar

- 阅读剩余部分 -

首先,根据文档:

So, to get started, you should define which model attributes you want to make mass assignable. You may do this using the $fillable property on the model.

// model里定义哪些字段可以assignable
protected $fillable = ['name'];
// 使用以下2种方法,必须在`fillable`方法里定义哪些字段可以``
$flight = App\Flight::create(['name' => 'Flight 10']);
$flight = new App\Flight(['name' => 'Flight 10']);

但是呢,如果你不用fillable

- 阅读剩余部分 -

这是文档里说的:

Deleting An Existing Model By Key
In the example above, we are retrieving the model from the database before calling the delete method. However, if you know the primary key of the model, you may delete the model without retrieving it. To do so, call the destroy method:

  App\Flight::destroy(1);
  App\Flight::destroy([1, 2, 3]);
  App\Flight::destroy(1, 2, 3);

那么,这个destroy的返回值是什么呢?怎样判断是否删除成功了呢?

- 阅读剩余部分 -

根据rfc7231

A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.

简而言之,就是HTTP 1.1DELETE这个方法的request body的格式是没有定义的,因此会导致很多框架不会去实现它。

Laravel也一样,如果你想类似POST那样,在body中给一些参数,无论怎么玩,这个Request $request都是null

只有一个办法传参数,就是使用route parameter,类似这样:

DELETE /user/{user_id} 

折腾了十多分钟才研究明白这个事。

如果要让url上带参数,直接这样写:

$api->get('pic/{pic_id}', 'App\Controllers\PicController@getone')
            ->where('pic_id', '[a-zA-Z0-9]{8}');

这里,花括号里是参数,英文是route parameter,后面的where是用正则表达式来匹配参数,可以和其他url区别开。比如:

- 阅读剩余部分 -