Отображение данных Laravel с отношениями в Vue.js - PullRequest
0 голосов
/ 01 декабря 2018

Я строю SPA с Vue.js, Vue Router, Axios и Laravel.У меня есть внешний ключ в моей таблице пользователей, чтобы определить «местоположение» пользователей.Я пытаюсь вытащить location_name из таблицы Locations с помощью внешнего ключа в таблице пользователя.Консоль показывает нулевое местоположение.

Отношение в пользовательской модели

<?php

public function location()
{
    return $this->belongsTo('App\location','location');
}

Отношение в модели местоположения

<?php

public function User()
{
    return $this->hasMany('App\User');
}

Контроллер

<?php

public function show($id){
    $User = User::with('location')->find($id);

    return response()->json($User);
}

Сценарий

 export default {
    data: function() {
        return {
            tech:[]
        }
    },
    mounted() {
        axios.get('/tech/' + this.$route.params.id).then(response=>this.tech=response.data);
    }
}

Просмотр

<input type="text" v-model="tech.location.location_name">

rout.js

   let routes = [
    {
        path:'/',
        component:require('./views/dashboard/dashboard')
    },

    {
        path:'/locations',
        component:require('./views/locations/locations')
    },

    {
        path:'/allLocations',
        component:require('./views/locations/allLocations')
    },
    {
        path:'/inActiveLocations',
        component:require('./views/locations/inActiveLocations')
    }
    ,
    {
        path:'/newLocation',
        component:require('./views/locations/newLocation')
    }
    ,
    {
        path:'/location/:id',
        component:require('./views/locations/location')
    }
    ,
    {
        path:'/editLocation/:id',
        component:require('./views/locations/editLocation')
    }
    ,
    {
        path:'/techs',
        component:require('./views/techs/techs')
    }
    ,
    {
        path:'/createTech',
        component:require('./views/techs/createTech')
    },
    {
        path:'/tech/:id',
        component:require('./views/techs/tech')
    }

web.php

 <?php

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

/* Locations*/
Route::post('/location','locationController@store');
Route::get('/locations','locationController@index');
Route::get('/inActiveLocations','locationController@inActiveLocations');
Route::get('/allLocations','locationController@allLocations');
Route::get('/location/{id}','locationController@show');
Route::get('/editLocation/{id}','locationController@edit');
Route::patch('/updateLocation/{id}','locationController@update');



/*Users*/
Route::get('/techs','techController@index');
Route::post('/tech','techController@store');
Route::get('/tech/{id}','techController@show');

1 Ответ

0 голосов
/ 01 декабря 2018

Вам нужно будет внести некоторые изменения в ваш код.Вы пытаетесь получить свойство, а не отношение.Чтобы получить местоположение, вам нужно будет внести следующие изменения.

public function show($id){
    $User = User::find($id);
    $response = $User;
    $response['location'] = $User->location;
    return response()->json($response);
}
...