如何从关系表中获取ID?

时间:2017-10-15 14:29:16

标签: laravel laravel-5 laravel-4 laravel-5.2 laravel-5.3

您好我有2个表数字和number_details有2个型号Number和Number_details。这是我的代码

数字模型

public function number() {
    return $this->hasOne('Number');
}

NumberDetails模型

public function store(Request $request){
    $numberDetails = new NumberDetails;
    $numberDetails->number_id =//how to get id ;
    $numberDetails->save();
}

NumberController

$('#login').click ( function() {
    $.ajax({
        type: "POST",
        url: 'login.php',
        data: $('#loginform').serialize(),
        dataType: 'json',
        success: function(data) {
            // Check the result from data
            if (data.result == 'success') {
                $('#message').html('success');
            } else {
                $('#message').html('fail');
            }
        },
        error: function(d) {
            $('#message').html('error');
        }
    });
    return false;
})

1 个答案:

答案 0 :(得分:0)

更新:当我尝试使用时:

 $numberDetails->number_id = Number::find(id);  Trying to get property of non-object