第一篇文章!php 和 Laravel 新手,我想我可以通过创建一个测试项目来学习。我一直在关注 Laracasts 寻求指导,但遇到了问题。
我希望实现的目标:
我已经为 Guzzle 和存储数据设置了控制器。我已经创建了一个按预期工作的数据库。但我不确定到底需要做什么,所以我在这里为如何实际运行控制器并将数据存储在数据库中而苦苦挣扎。
如果我做错了什么,如果有人可以检查我的代码,并在这方面提供一些有关路线的指导,我将不胜感激。
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
class DataController extends Controller
{
public function index()
{
$client = new Client(['base_uri' => 'https://api.ratings.food.gov.uk/ratings']);
$response = $client->request('GET', [
'headers' => [
'x-api-version' => '2',
'Accept' => 'application/json'
]
]);
$mydata = json_decode($response->getBody()->getContents(), true);
$object = new Object();
$object->ratingId = $mydata->ratingId;
$object->ratingName = $mydata->ratingName;
$object->ratingKey = $mydata->ratingKey;
$object->ratingKeyName = $mydata->ratingKeyName;
$object->schemeTypeId = $mydata->schemeTypeId;
$object->save();
Requests::insert($object);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
移民
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRatingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ratings', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->integer('ratingId');
$table->string('ratingName');
$table->string('ratingKey');
$table->string('ratingKeyName');
$table->integer('schemeTypeId');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('ratings');
}
}
Run Code Online (Sandbox Code Playgroud)
API JSON 响应示例
{
"ratings": [
{
"ratingId": 12,
"ratingName": "5",
"ratingKey": "fhrs_5_en-gb",
"ratingKeyName": "5",
"schemeTypeId": 1,
"links": [
{
"rel": "self",
"href": "http://api.ratings.food.gov.uk/ratings/12"
}
]
},
{
"ratingId": 11,
"ratingName": "4",
"ratingKey": "fhrs_4_en-gb",
"ratingKeyName": "4",
"schemeTypeId": 1,
"links": [
{
"rel": "self",
"href": "http://api.ratings.food.gov.uk/ratings/11"
}
]
},
{
"ratingId": 10,
"ratingName": "3",
"ratingKey": "fhrs_3_en-gb",
"ratingKeyName": "3",
"schemeTypeId": 1,
"links": [
{
"rel": "self",
"href": "http://api.ratings.food.gov.uk/ratings/10"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
您应该利用 Eloquent ORM 的强大功能。我在您的代码中发现了一些问题。您需要删除在那里编写的不必要的行才能创建对象。考虑到您已经创建了与 API 响应返回的名称相同的列,并且考虑到您的模型名称是Rating并且应该是,以下是我的建议:
您的控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use App\Rating;
class DataController extends Controller
{
public function index()
{
$client = new Client(['base_uri' => 'https://api.ratings.food.gov.uk/ratings']);
$response = $client->request('GET', [
'headers' => [
'x-api-version' => '2',
'Accept' => 'application/json'
]
]);
$mydata = json_decode($response->getBody()->getContents(), true);
/* You don't need to create an object as you are already parsing the response as an array, so remove below lines */
// $object = new Object();
// $object->ratingId = $mydata->ratingId;
// $object->ratingName = $mydata->ratingName;
// $object->ratingKey = $mydata->ratingKey;
// $object->ratingKeyName = $mydata->ratingKeyName;
// $object->schemeTypeId = $mydata->schemeTypeId;
// $object->save();
Rating::create($mydata);
}
}
Run Code Online (Sandbox Code Playgroud)
$fillable并通过向模型添加受保护的静态属性来使列可在您的评级模型中填写Rating:
protected static $fillable = ['ratingId', 'ratingName', 'ratingKeyName', 'schemeTypeId'];
Run Code Online (Sandbox Code Playgroud)
如果上述解决方案不是您喜欢的解决方案,那么您需要将其视为$mydata数组,即执行$mydata['ratingId']get ratingId、 not$mydata->ratingId或true从中删除参数json_decode(),以将响应解析为对象,而不是数组。