Php 拉威尔:可以';不可接近的雄辩关系

Php 拉威尔:可以';不可接近的雄辩关系,php,laravel,Php,Laravel,我创建了Post和用户模型,并定义了两者之间的一对多关系,如下所示: User.php <?php namespace App; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { /** * The attributes that are mass assignable. * * @var array

我创建了Post和用户模型,并定义了两者之间的一对多关系,如下所示:

User.php

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

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

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    public function posts () {
        return $this->hasMany(Post::class,'post_author');
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = [
        'post_title', 'post_content', 'post_author', 'post_type', 'created_at', 'updated_at',
    ];
    public function user() {
        return $this->belongsTo(User::class,'id');
    }
    public function postfields() {
        return $this->hasMany(PostField::class);
    }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Post;

use App\User;

class BlogController extends Controller
{
    public function index(Post $post) {
        $blog_posts = Post::with('user')->where('post_type', '=', 'blog')->get();
        return view('blog', compact('blog_posts'));
    }
}
@extends('layouts.front-header')

@section('content')
<style>
    .hero-image {
        background: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)), url(images/hero-2.jpg);
    }
</style>


<div class="hero-image hero-image-inner-page">
    <div class="hero-image-inner">
        <div class="text">
            <h1>Blog</h1>
        </div>
    </div>
</div>



<main>
    <section class="blog">
        <div class="container">
        <div class="posts">
        <div class="items">
            <!-- 
            <div class="post item">
                <h3 data-field="post_title"></h3>
                <p data-field="post_content"></p>
                <img src="http://fiddle-earth.com/updates/madsa/img/image00.png" alt="">
                <div class="post-data">
                    <img data-field="author_profile_picture" alt="">
                    <p>posted by <strong data-field="post_author"></strong> on <span data-field="post_date"></span></p>
                </div>
            </div> -->


            @foreach ($blog_posts as $blog_post)
                <div class="post item">
                    <h3 data-field="post_title">{{ $blog_post->post_title }}</h3>
                    <p data-field="post_content">{{ $blog_post->post_content }}</p>
                    <!-- <img src="http://fiddle-earth.com/updates/madsa/img/image00.png" alt=""> -->
                    <div class="post-data">
                        <img data-field="author_profile_picture" alt="">
                        @if($blog_post->user()!=null)
                            {{ $blog_post->user->name }}
                        @endif
                        <p>posted by <strong data-field="post_author"></strong> on <span data-field="post_date"></span></p>
                    </div>
                </div>
            @endforeach

        </div>
        <!-- <a href="#" class="items-load">Load more</a> -->
        </div>
        </div>
    </section>
</main>




<script src="assets/js/loadmore.js"></script>
<script>
$('.posts').loadmore({
    source: 'assets/js/json/blog-json.php',
    img: 'uploads/',
    step: 4
});
</script>
@endsection
Blog.blade.php

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

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

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    public function posts () {
        return $this->hasMany(Post::class,'post_author');
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = [
        'post_title', 'post_content', 'post_author', 'post_type', 'created_at', 'updated_at',
    ];
    public function user() {
        return $this->belongsTo(User::class,'id');
    }
    public function postfields() {
        return $this->hasMany(PostField::class);
    }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Post;

use App\User;

class BlogController extends Controller
{
    public function index(Post $post) {
        $blog_posts = Post::with('user')->where('post_type', '=', 'blog')->get();
        return view('blog', compact('blog_posts'));
    }
}
@extends('layouts.front-header')

@section('content')
<style>
    .hero-image {
        background: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)), url(images/hero-2.jpg);
    }
</style>


<div class="hero-image hero-image-inner-page">
    <div class="hero-image-inner">
        <div class="text">
            <h1>Blog</h1>
        </div>
    </div>
</div>



<main>
    <section class="blog">
        <div class="container">
        <div class="posts">
        <div class="items">
            <!-- 
            <div class="post item">
                <h3 data-field="post_title"></h3>
                <p data-field="post_content"></p>
                <img src="http://fiddle-earth.com/updates/madsa/img/image00.png" alt="">
                <div class="post-data">
                    <img data-field="author_profile_picture" alt="">
                    <p>posted by <strong data-field="post_author"></strong> on <span data-field="post_date"></span></p>
                </div>
            </div> -->


            @foreach ($blog_posts as $blog_post)
                <div class="post item">
                    <h3 data-field="post_title">{{ $blog_post->post_title }}</h3>
                    <p data-field="post_content">{{ $blog_post->post_content }}</p>
                    <!-- <img src="http://fiddle-earth.com/updates/madsa/img/image00.png" alt=""> -->
                    <div class="post-data">
                        <img data-field="author_profile_picture" alt="">
                        @if($blog_post->user()!=null)
                            {{ $blog_post->user->name }}
                        @endif
                        <p>posted by <strong data-field="post_author"></strong> on <span data-field="post_date"></span></p>
                    </div>
                </div>
            @endforeach

        </div>
        <!-- <a href="#" class="items-load">Load more</a> -->
        </div>
        </div>
    </section>
</main>




<script src="assets/js/loadmore.js"></script>
<script>
$('.posts').loadmore({
    source: 'assets/js/json/blog-json.php',
    img: 'uploads/',
    step: 4
});
</script>
@endsection
@extends('layouts.front header'))
@节(“内容”)
.英雄形象{
背景:线性梯度(rgba(0,0,0,0.4),rgba(0,0,0,0.4)),url(images/hero-2.jpg);
}
博客
$('.posts').loadmore({
来源:“assets/js/json/blog json.php”,
img:'上传/',
步骤:4
});
@端部

据我所知,Post模型中的
用户关系导致了异常。所以,当你打电话时:

{{ $blog_post->user->name }}
它找不到与该帖子关联的用户。我猜
User
表的外键与Laravel的不同:

Eloquent通过检查关系方法的名称并用_id作为方法名称的后缀来确定默认外键名称。但是,如果注释模型上的外键不是post_id,则可以将自定义外键名称作为第二个参数传递给belongsTo方法:

所以在你的情况下,应该是这样的:

public function user()
{
    return $this->belongsTo('App\User', 'post_user_id'); //Replace post_user_id with the id convention you are using.
}
{{ $blog_post->user()->name }}
更新:

感谢您在$blog_帖子上的dd。我可以看到正在提取用户,但没有正确调用它。这是一个函数,所以你需要把它当作一个函数。修改刀片文件以检索用户,如下所示:

public function user()
{
    return $this->belongsTo('App\User', 'post_user_id'); //Replace post_user_id with the id convention you are using.
}
{{ $blog_post->user()->name }}
这将获得有效的用户集合

更新2:

正如我从转储中看到的,来自第二个
Post
的用户关系为空。在创建帖子时,请确保它不为null,若要在blade中处理它,请将其包装为null检查:

@foreach ($blog_posts as $blog_post)
<div class="post item">
    {{ $blog_post->post_title }}
    {{ $blog_post->post_content }}
    @if($blog_post->user()!=null)
        {{ $blog_post->user()->name }}
    @endif
</div>
@endforeach
@foreach($blog\u posts作为$blog\u post)
{{$blog\u post->post\u title}
{{$blog\u post->post\u content}
@如果($blog\u post->user()!=null)
{{$blog\u post->user()->name}
@恩迪夫
@endforeach
更新3:

它实际上应该作为属性而不是函数调用,因为当作为函数调用时,它返回“BelongsTo”。请立即尝试以下操作:

@foreach ($blog_posts as $blog_post)
<div class="post item">
    {{ $blog_post->post_title }}
    {{ $blog_post->post_content }}
    @if($blog_post->user!=null)
        {{ $blog_post->user->name }}
    @endif
</div>
@endforeach
@foreach($blog\u posts作为$blog\u post)
{{$blog\u post->post\u title}
{{$blog\u post->post\u content}
@如果($blog\u post->user!=null)
{{$blog\u post->user->name}
@恩迪夫
@endforeach
我在这里复制了同一个问题,这解决了它。让我知道它是否适合您。

我没有发现错误,请尝试

composer转储自动加载

php artisan视图:清除

尝试以下操作:

而不是

@foreach ($blog_posts as $blog_post)
    <div class="post item">
        {{ $blog_post->post_title }}
        {{ $blog_post->post_content }}
        {{ $blog_post->user->name }}
    </div>
@endforeach
@foreach($blog\u posts作为$blog\u post)
{{$blog\u post->post\u title}
{{$blog\u post->post\u content}
{{$blog\u post->user->name}
@endforeach
使用以下命令:

    @foreach ($blog_posts as $blog_post)
            <div class="post item">
                {{ $blog_post->post_title }}
                {{ $blog_post->post_content }}
                @foreach($blog_post->user as $user)
                    {{ $user->name }}
                @endforeach
    @endforeach
@foreach($blog\u posts作为$blog\u post)
{{$blog\u post->post\u title}
{{$blog\u post->post\u content}
@foreach($blog\u post->user as$user)
{{$user->name}
@endforeach
@endforeach

嘿,我刚刚更新了问题,以便包含外键参数,不幸的是,我得到了一个不同的错误。谢谢你的回答。你对新的错误有什么想法吗>你能在你的博客控制器中添加$blog_帖子并将输出粘贴到这里,这样我就可以看到发生了什么。dd($blog_posts);刚刚添加到问题中,希望有帮助。@6rs_Leon你能展开第一篇文章,然后展开
原始
锚定和所有子树吗。同时展开
关系
锚及其所有子树。我们的目标是查看
用户是什么。完整的结构现在添加到问题中。它返回以下错误:尝试获取非对象的属性(视图:C:\MAMP\htdocs\Biota New\resources\views\blog.blade.php)尝试将函数重命名为users(),并在您的刀片视图中尝试用户,当然,看看这是否有任何区别。试着重新启动你的服务器。我在另一个问题中看到了你的答案,那是因为第二个DD(blog_posts)返回给你:#relations:array:1[▼         “user”=>null]所以user=null意味着你得到的是你被要求得到的!