【问题标题】:Save/retrieve data by foreign key (API)通过外键 (API) 保存/检索数据
【发布时间】:2017-04-18 08:59:36
【问题描述】:

我正在尝试为 iOS 应用设置我的 API。这是我第一次使用 Laravel 作为 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 应用程序用户保存的数据(合同)。例如,用户信息和他们对我的表感兴趣的汽车。有人可以帮我在控制器中查询什么(不是视图)。或者为此类项目提供有用的链接。

【问题讨论】:

    标签: ios mysql laravel orm eloquent


    【解决方案1】:

    UserCars 之间的关系应该是 many-to-many。请阅读文档以正确应用此关系。

    如果你的关系在那个地方,那么你可以这样做:

    $user = JWTAuth::parseToken()->authenticate();
    
    $cars = $user->cars; // returns collection of cars associated with the user.
    

    例如 - 在您的 User 模型中定义以下关系:

    public function cars()
    {
        return $this->belongsToMany('App\Car');
    }
    

    更新

    要保存用户和关联汽车,您可以这样做:

    $user = JWTAuth::parseToken()->authenticate();
    
    $user->phone = $request->phone;
    $user->city = $request->city;
    
    $user->save();
    
    $car_ids = $request->car_ids; // it should return an array
    
    $user->cars()->sync($car_ids);
    

    还有更多存储数据的方法。请阅读文档here 了解更多信息。

    【讨论】:

    • 所以这将返回与用户关联的汽车。现在在返回之前我该如何链接它们?如何通过 http 调用将汽车 ID 链接到用户 ID? @iCode
    • 检查更新的答案。用于链接数据读取here。而且我没有收到您的问题“如何通过 http 调用将汽车 ID 链接到用户 ID?”
    • 谢谢你的榜样!我的意思是,在 iOS 应用程序中,我会将用户数据(姓名、电话...)发送到 Laravel 中的 store 函数。汽车数据也会发生同样的情况。但正如我所说,我想显示用户信息以及他们感兴趣的汽车。怎么做?在应用程序中,我有一个表格,用户将填写他们的信息和他们感兴趣的汽车(下拉菜单)@iCode
    • 如果在请求中发送了cars_id,这个$car_ids = $request->car_ids; // it should return an array 将返回一个数组,对吗?我想要做的是我的 iOS 应用程序中有两个表格,第一个是让用户填写他们的信息,然后填写他们想要租用的汽车信息。两个单独的表,但我想将它们收集在我的联系表中,正如我在问题中显示的那样。用户信息将转到用户表,汽车信息将转到汽车表,但通过 ID 关联它们是我的问题。你能帮我吗?谢谢
    • 是的,您理解正确。对于您的第二个问题,请尝试对用户信息和他的汽车兴趣使用相同的表格。或者第二个选项是创建一个返回新创建用户的表单,然后您可以使用该用户的 id 在第二个表单中发送。在您的控制器中找到用户并关联兴趣。
    猜你喜欢
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 2019-12-29
    • 2014-04-13
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多