如何使用React JS获取嵌套的JSON对象

时间:2018-07-08 08:08:38

标签: javascript php laravel reactjs

我正在使用laravel,我试图检查用户是否关注用户,如果是,则文本框将从关注变为关注。

我可以轻松获得用户的ID,但是我无法检查用户是否具有可追踪的ID

enter image description here

我需要引用数据透视对象,并且该对象仅在我执行操作时显示

<div id="profile" data='{{ $myuser->followers}}'></div>

但是我需要自己使用$myuser变量。

这是我到目前为止所拥有的。

Profile.blade.php

  <div id="profile" data='{{ $myuser}}'></div>

Profile.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';


export default class Example extends Component {
    constructor(props){
        super(props);
        let id = JSON.parse(this.props.data);
        // console.log('data from component', JSON.parse(this.props.data));
        this.state = {
            btnText: 'Follow',
            className: 'follow-button',
            user:{
                // i can get a user id, but i cant get the followable id.
                id:id.id, 
                followers:id.pivot.followable_id
            }
        };
    }

    myfollow(user) {
        axios('/user/follow/'+ this.state.user.id , { method: "POST" })
          .then(response => {
            console.log(response);
          });
    };

    componentDidMount(){

        console.log('data from component', this.state.user.followers);

        // if (this.state.user.followers === 3){    
        //  this.setState({
        //         btnText:'Following', 
        //         className:'following-button'
        //     });
        // }
    }

UserController.php

public function getProfile($user)
{  
    $users = User::with(['posts.likes' => function($query) {
                        $query->whereNull('deleted_at');
                        $query->where('user_id', auth()->user()->id);
                    }, 'follow','follow.follower'])
                  ->where('name','=', $user)->get();

    $user = $users->map(function(User $myuser){


        $myuser['followedByMe'] = $myuser->follow->count() == 0 ? false : true;

        return $myuser;

    });



    if(!$user){
        return redirect('404');
    }

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

MyFollow.php (模型)

public function followedByMe()
{
    foreach($this->follower as $followers) {
        if ($followers->user_id == auth()->id()){
            return true;
        }
    }
    return false;
}

User.php模型

?php

namespace App;
use App\User;
use App\Post;
use App\GalleryImage;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\MyFollow;
use Overtrue\LaravelFollow\Traits\CanFollow;
use Overtrue\LaravelFollow\Traits\CanBeFollowed;

class User extends Authenticatable
{
    use Notifiable,CanFollow, CanBeFollowed;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];


    public function posts()
    {
        return $this->hasMany(Post::class);

    }


    public function images()
    {
        return $this->hasMany(GalleryImage::class, 'user_id');

    }


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

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


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




}

0 个答案:

没有答案