Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 属于laravel 5.8中的关系_Php_Laravel_Eloquent - Fatal编程技术网

Php 属于laravel 5.8中的关系

Php 属于laravel 5.8中的关系,php,laravel,eloquent,Php,Laravel,Eloquent,我想在post和user之间建立一个关系,这样就可以通过$user->posts()获得所有的post,并通过$post->user()获得用户 我做了belongsTo功能,但我需要一个方法,这样当我得到用户时,我可以找到他的所有帖子 另外,当我获得我使用的帖子\App\post::find(1)->user()->name时,它不会返回任何内容 后模型 class Post extends Model { public function user() { return

我想在post和user之间建立一个关系,这样就可以通过$user->posts()获得所有的post,并通过$post->user()获得用户

我做了belongsTo功能,但我需要一个方法,这样当我得到用户时,我可以找到他的所有帖子 另外,当我获得我使用的帖子\App\post::find(1)->user()->name时,它不会返回任何内容

后模型

class Post extends Model
{
    public function user() {
        return $this->belongsTo('App\User',null,'userid');
    }
}
后数据库结构

Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->longText('body');
            $table->string('thumbnail');
            $table->integer('views');
            $table->integer('likes');
            $table->bigInteger('userid');
            $table->integer('categoryid');
            $table->timestamps();
        });
用户模型中并没有任何内容,只有laravel默认值,因为用户并没有引用文章的列

用户数据库结构为Laravel默认值

Foreach代码:

@foreach(\App\Post::find(4)->user() as $user)
            <p>{{$user->name}}</p>
@endforeach
@foreach(\App\Post::find(4)->user()作为$user)
{{$user->name}

@endforeach

我希望它能得到用户名,但它没有…

使用
有很多关系

在用户模型中:

公共职能岗位()
{
返回$this->hasMany(Post::class,'userid','id');
}
现在,您可以获得所有用户帖子:

$posts=User::find($someId)->posts()->get();
//或
$posts=User::find($someId)->posts;
关于

希望有帮助。

在用户模型中

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\User;

class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Post;

class User extends Model
{
    /**
     * Get the posts for the user.
     */
    public function comments()
    {
        return $this->hasMany('App\Post');
    }
}

很高兴我能帮助你!:)非常感谢,你确实帮助了我