Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.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 尝试读取“属性”;用户名";论空_Php_Laravel 8 - Fatal编程技术网

Php 尝试读取“属性”;用户名";论空

Php 尝试读取“属性”;用户名";论空,php,laravel-8,Php,Laravel 8,在尝试在Laravel8上创建虚假帖子时,我遇到了一些错误,首先它没有创建帖子,然后我将用户名改为nullable,它创建了帖子,但我一直有错误 尝试读取null上的属性“username” 所以,我回到我的数据库,我把它改回无,但我仍然收到相同的错误代码,我会张贴我的代码现在 index.blade.php @extends('layouts.app') @section('content') <div class="flex justify-center"

在尝试在Laravel8上创建虚假帖子时,我遇到了一些错误,首先它没有创建帖子,然后我将用户名改为nullable,它创建了帖子,但我一直有错误

尝试读取null上的属性“username”

所以,我回到我的数据库,我把它改回无,但我仍然收到相同的错误代码,我会张贴我的代码现在

index.blade.php

@extends('layouts.app')

@section('content')

    <div class="flex justify-center">
        <div class="w-8/12 bg-white p-6 rounded-lg">
            <form action="{{ route('posts') }}" method="post" class="mb-4">
                @csrf
                <div class="mb-4">
                    <label for="body" class="sr-only">Body</label>
                    <textarea name="body" id="body" cols="30" rows="4" class="by-gray-100 border-2
                    w-full p-4 rounded lg @error('body') border-red-500 @enderror" placeholder="Post something!"></textarea>
                    @error('body')
                        <div class="text-red-500 mt-3 text-sm">
                            {{$message}}
                        </div>
                    @enderror
                </div>
                <div>
                    <button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded
                    font-medium">Post</button>
                </div>
            </form>
            @if ($posts->count())
                @foreach ($posts as $post)
                    <div class="mb-4">
                        <a href="" class="font-bold">{{ $post->user->username }}</a>
                        <span class="text-gray-600 text-sm">{{ $post->created_at->diffForHumans() }}</span>
                        <p class="mb-2">{{ $post->body }}</p>
                    </div>

                @endforeach

                {{ $posts->links() }}

            @else
                There are no posts...
            @endif
        </div>
    </div>

@endsection
@extends('layouts.app'))
@节(“内容”)
@csrf
身体
@错误('正文')
{{$message}}
@恩德罗
邮递
@如果($posts->count())
@foreach($posts作为$post)
{{$post->created_at->diffForHumans()}

{{$post->body}

@endforeach {{$posts->links()} @否则 没有帖子。。。 @恩迪夫 @端部
UserFactory.php

<?php

namespace Database\Factories;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class UserFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = User::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'username' => $this->faker->username,
            'name' => $this->faker->name,
            'email' => $this->faker->unique()->safeEmail,
            'email_verified_at' => now(),
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];
    }

    /**
     * Indicate that the model's email address should be unverified.
     *
     * @return \Illuminate\Database\Eloquent\Factories\Factory
     */
    public function unverified()
    {
        return $this->state(function (array $attributes) {
            return [
                'email_verified_at' => null,
            ];
        });
    }
}
<?php

namespace Database\Factories;

use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;

class PostFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Post::class;


    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'body' => $this->faker->sentence(20),
                ];
    }
}

这是因为我的代码中有一个现有的用户id,所以,我从数据库中删除了它,它工作了

在哪里/哪一行发生了错误<代码>{{$post->user->username}}
?看起来一个或多个帖子没有关联的用户。检查您的数据库
<?php

namespace Database\Factories;

use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;

class PostFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Post::class;


    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'body' => $this->faker->sentence(20),
                ];
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

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

   /**
     * Get the user that owns the Post
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}