首页 > 解决方案 > 如何使用 React js 获取嵌套的 json 对象

问题描述

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

我可以轻松获取用户的 id,但我无法检查用户是否有可关注的 id

在此处输入图像描述

我需要引用枢轴对象,并且该对象仅在我这样做时显示

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

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

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

配置文件.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'
        //     });
        // }
    }

用户控制器.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');
    }




}

标签: javascriptphplaravelreactjs

解决方案


推荐阅读