duanjingsen7904 2018-07-08 08:08
浏览 163

如何使用react js获取嵌套的json对象

I'm using laravel, and im trying to check to see if a user is following a user, if so the text box will change from follow to following.

I can get the id of the user easily but i can't check to see if a user has followable id

enter image description here

I need to reference the pivot object, and this object only shows when i do

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

but i need to use $myuser variable by itself.

This is what i have so far.

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 (model)

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

User.php Model

?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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器