Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

php - Laravel 8 - can't get data from database

I want to get data from database that name is follower_id but when I try to get data I get error. And when I try with foreach I'm returning null I'm using database with relationships and I can get data if I type {{$follows}} . On the other hand, if I type {{$follows->follower_id}} I get this error:

Property [follower_id] does not exist on this collection instance.

So how can I solve this problem? I'm beginner in Laravel and database relationships by the way.

My controller is:

public function getProfile($username){

        $follow = Follow::all();
        $user = User::where('username', $username)->first();
        if(isset($user)){
        return view('design2.profile', ['user'=>$user,'follow'=>$follow]);        
        }else{
            return redirect()->route('home');
        }
    }

My blade is:

<h6><span class="text-secondary"><strong>{{$follow->follower_id}} follower</strong></span></h6>

My follow model:

public $table = "follow";

protected $fillable = [
    'follower_id',
    'user_id',
];


public function user(){
    return $this->belongsTo('AppModelsUser');
}

My user model:

protected $fillable = [
        'name',
        'email',
        'username',
        'password',
        'info',
        'twitter_name',
        'instagram_name',
        'photo'
    ];

   public function follows(){
        return $this->hasMany('AppModelsFollow');
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you need is to get all the followers of a user. Let me do the coding for you so that you have a somewhat more standard code than you have now.

In you User Model

<?php

namespace AppModels;

use IlluminateFoundationAuthUser as Authenticatable;

class User extends Authenticatable
{
    /**
     * The followers that belong to the User
     *
     * @return IlluminateDatabaseEloquentRelationsBelongsToMany
     */
    public function followers()
    {
        return $this->belongsToMany(User::class, 'follow', 'user_id', 'follower_id');
    }
}

In your controller.

/**
 * Gets the user with all his followers.
 *
 * @param string $username
 * @return IlluminateHttpResponse
 */
public function getProfile($username){

    $user = User::with('followers')->where('username', $username)->firstOrFail();
    
    return view('design2.profile', [
        'user' => $user,
    ]);
}

In your blade.

@foreach($user->followers as $follower)
    <h6>
        <span class="text-secondary">
            <strong>{{$follower->name}}</strong>
        </span>
    </h6>
@foreach

And that is all.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...