Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用vuejs和axios在关系数据库Laravel模型中获取数据_Javascript_Php_Laravel_Vue.js_Axios - Fatal编程技术网

Javascript 如何使用vuejs和axios在关系数据库Laravel模型中获取数据

Javascript 如何使用vuejs和axios在关系数据库Laravel模型中获取数据,javascript,php,laravel,vue.js,axios,Javascript,Php,Laravel,Vue.js,Axios,我试图从表user中获取具有外键为user\u id的表Personal Information中的数据,但无法获取。请问有人知道怎么做吗 为用户和个人关系建模 class Personal extends Model { protected $fillable = ['fillable data']; protected $table = 'informations'; public function user() { return $this->belongsT

我试图从表
user
中获取具有外键为
user\u id
的表
Personal Information
中的数据,但无法获取。请问有人知道怎么做吗

为用户和个人关系建模

class Personal extends Model
 {
 protected $fillable = ['fillable data'];    
 protected $table = 'informations';

public function user()
{
    return $this->belongsTO(User::class);
}
}

我的个人控制器

class PersonalController extends Controller
{

private $personal;

public function __construct(Personal $personal)
{
    $this->personal = $personal;
}

public function index()
{   
    $user = auth()->user();
    if(!$user==null)
    {
        return view('welcome');
    }else
    {
        return view('home' , compact('user'));
    }  
}

public function get()
{
    $user = auth()->user()->personal;
    return response()->json($user);
}
用户关系模型

public function personal()
{
    return $this->hasOne(Personal::class);

}
我的视图模板

export default {
    data(){
        return{
            form: new Form({
                id: '', 
                name: '', 
                surname: '', 
                identity: '', 
                birthday: '', 
                country: '',
                province: '',  
                county: '', 
                telemovel: '', 
                address: '', 
                facebook: '', 
                tweet:'', 
                linkedin: '',
            }) 
        }
    },
    methods:{    
        loadUsers(){
            axios.get("api/personal").then(({ data }) => (this.form.fill(data)));
        },
        createUser(){
            this.form.post('api/personal')
        },
    },
    created(){
        this.loadUsers();
    },
    mounted() {
        console.log('Component mounted.')
    }
}

可能是因为您有语法错误。在您的关系个人模型中,belongTo是这样的,还可以指定外键,如下所示

public function user() {
    return $this->belongsTo('App\Models\User','user_id'); }

用户id是外键。

谢谢您的回答,但真正的问题是我应该首先安装Laravel Passport。在那之后一切都很顺利