1
我正在尝试为iOS应用程序设置我的API。这是我第一次使用Laravel的API,因此这里是我在我的表:通过外键保存/检索数据(API)
汽车
Schema::create('cars', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->nullable();
$table->string('age')->nullable();
$table->string('model')->nullable();
$table->string('color')->nullable();
用户
$table->increments('id');
$table->string('name')->nullable();
$table->string('street')->nullable();
$table->string('niehgborhood')->nullable();
$table->string('city')->nullable();
合同
$table->increments('id');
$table->integer('user_id')->unsigned;
$table->foreign('user_id')->references('id')->on('users');
$table->integer('car_id')->unsigned;
$table->foreign('car_id')->references('id')->on('cars');
模式
protected $table = 'users';
protected $guarded = ['id'];
protected $fillable = ['phone', 'email',
'street','city','niehgborhood'
,'national_id'];
public function cars()
{
return $this->hasMany('App\User');
}
用户
protected $guarded = ['id'];
protected $fillable = ['name', 'age',
'color, model'
];
public function users()
{
return $this->belongsToMany('App\Cars');
}
在我的控制,我熟悉保存请求数据
$user = JWTAuth::parseToken()->authenticate();
$user->phone = $request->phone;
$user->city = $request->city;
$user->save();
我这个项目的目标是要显示的数据(合同)由我的仪表板中的iOS应用用户保存。例如,用户信息和他们对我的表感兴趣的汽车。有人可以帮我做什么在控制器查询(而不是视图)。或者为这样的项目提供有用的链接。
所以这将返回与用户关联的汽车。现在在回答之前,我怎样才能链接它们?如何通过http调用将汽车ID链接到用户ID? @iCode – leo0019
检查更新的答案。用于链接数据读取[这里](https://laravel.com/docs/5.3/eloquent-relationships#updating-many-to-many-relationships)。我没有得到你的问题“我怎么能通过http调用将汽车id链接到用户id?” –
谢谢你的例子!我的意思是,在iOS应用中,我会将用户数据(姓名,电话......)发送到Laravel的“商店”功能。汽车数据也会发生同样的情况。但正如我所说,我想要显示用户信息和他们感兴趣的汽车。怎么做?在应用程序中,我有一个表格,用户将填写他们的信息和他们感兴趣的汽车(下拉列表)@iCode – leo0019