如何通过SQL行数据Vue的文件(Laravel项目)

时间:2019-01-31 16:39:19

标签: sql laravel vue.js eloquent

我试图传递数据,并用它Vue的文件中,但我不能让它在这里工作是我的代码

<template>
    <div class="row profile-container">
        <div class="col-sm-4">
            <img src="/img/default.jpg" alt="" class="profile-img">
        </div>
        <div class="col-lg">
            <h5>{{$user->name}}</h5>
            <div class="card w-100">

                <ul class="list-group list-group-flush">
                    <li class="list-group-item">Cras justo odio</li>
                    <li class="list-group-item">Dapibus ac facilisis in</li>
                    <li class="list-group-item">Vestibulum at eros</li>
                </ul>
            </div>
        </div>
    </div>
</template>

这里的简档控制器与数据来分析其返回

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class ProfileController extends Controller
{
    public function index($id)
    {
        $user = User::where('unique_id', $id)->firstOrFail();

        return view('pages.profile')->with('user', $user);
    }
}

这里的app.js

require('./bootstrap');

window.Vue = require('vue');

Vue.component('welcome', require('./components/Welcome.vue').default);
Vue.component('profile', require('./components/Profile.vue').default);


const app = new Vue({
    el: '#app'
});

路线

Route::get('/', 'PagesController@index' );

Auth::routes();

Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout');
Route::get('/profile/{id}', 'ProfileController@index');

下面是刀刃文件PIP didly斗我不知道该怎么写了它lookslikemy后大多是代码,所以生病只需添加一些细节在这里,thatsprettycoolshaggyisgod我不知道还有什么在这里补充,请发送帮助

@extends('layouts.master')
@section('title')
  <title>Profile</title>
@endsection
@section('content')
  @if(Auth::user()->type==='user')
  <profile></profile>
@elseif (Auth::user()->type==='admin')
  <h1>Hi Admin</h1>
@endif
@endsection

1 个答案:

答案 0 :(得分:1)

您的脚本应如下所示:

<script>
    export default {
        name: 'profile',
        data() {
            return {
                user: []
            }
        },
        methods: {
            getUser() {
                let _this = this;
                axios.get('/user')
                    .then(function (response) {
                        _this.user = response.data;
                    }).catch(function (error) {
                        console.log(error);
                });
            },
        created() {
            this.getUser();
        }
    }
</script>

将此添加到您的路由文件:

Route::get('/brands', function($id) {
  $user = User::where('unique_id', $id)->findOrFail();
  return view('pages.profile')->with('user',$user);
});

让我知道是否有帮助,我认为您已经错过了很多代码,这不像将数据传递到Laravel视图那样容易,但是希望可以帮助您入门