gpt4 book ai didi

php - Laravel 试图获取非对象的属性 'project_name'

转载 作者:搜寻专家 更新时间:2023-10-31 21:19:52 25 4
gpt4 key购买 nike

我是 Laravel 的新手,只是想指出一些问题。我在读取我的展示页面时遇到问题,当我打开展示页面 (show.blade.php) 时它返回此错误

Trying to get property 'project_name' of non-object (View: C:\Server\data\htdocs\ProjectCMS\resources\views\project\show.blade.php).

我不知道我做错了什么,因为我让它工作了,当我再次打开显示页面时它返回了错误。

如有任何帮助,我们将不胜感激!

ProjectController.php:

public function show($id)
{
$user_id = auth()->user()->id;
$project = Project::where('user_id', $user_id)->first();
$projectitem = Projectitem::where('project_id', $id)->first();

return view('project.show', [
'project' => $project,
'projectitem' => $projectitem,
]);
}

web.php 路由

Route::get('/projects/{id}', 'ProjectController@show');

show.blade.php:

@extends('layouts.app')

@section('content')

<div class="container">

<div class="well">

<div class="card mt-5 mb-5">
<h2 class="card-header">{{$projectitem->project_name}}</h2>
<div class="card-body">


<h2 class="card-text mt-4 mb-2"><b>Beschrijving:</b></h2><br>
{!!$projectitem->description!!}

<h2 class="mt-4 mb-2"><b>Tijdsduur(in uren):</b>&nbsp;{{$projectitem->time_span}} Uren</h2><br>

<h2 class="mt-2 mb-2"><b>Opdrachtgever:</b> &nbsp;{{$projectitem->client}}</h2>

<h2 class="mt-4 mb-2"><b>Text verslag:</b></h2><br>
{!!$projectitem->text_report!!}

<h2 class="mt-4 mb-2"><b>Foto's vooraf: &nbsp;</b></h2><br>
{!!$projectitem->images_before!!}
<h2 class="mt-4 mb-2"><b>Foto's achteraf: &nbsp;</b></h2><br>
{!!$projectitem->images_after!!}

<div class="row">
<div class="col-md-6">
<h2 class="mt-4"><b>Beschikbaar op: &nbsp;</b></h2>
<h4>Facebook:&nbsp;<?php if($projectitem->facebook == 1){echo "wel";}else{echo "niet";}?></h4>
<h4>Instagram:&nbsp;<?php if($projectitem->instagram == 1){echo "wel";}else{echo "niet";}?></h4>
<h4>Linkedin:&nbsp;<?php if($projectitem->linkedin == 1){echo "wel";}else{echo "niet";}?></h4>
<h4>Website:&nbsp;<?php if($projectitem->website == 1){echo "wel";}else{echo "niet";}?></h4>
</div>
</div>

<h4 class="mt-4 mb-2"><b>Aangemaakt door:</b>&nbsp;{{$projectitem->created_by}}</h4><br>
<div class="row">
<div class="col-md-6">
<h4 class="mt-4 mb-2 floatl "><b>Aangemaakt op:</b>&nbsp;{{$projectitem->created_at}}</h4><br>
</div>
<div class="col-md-6">
<h4 class="mt-4 mb-2 ml-1 mr-1 floatr "><b>Laatst gewijzigd op:</b>&nbsp;{{$projectitem->updated_at}}</h4><br>
</div>
</div>
</div>
</div>
</div>
</div>


@endsection

Projectitem.php(模型)

namespace App;

use Illuminate\Database\Eloquent\Model;

class Projectitem extends Model
{
protected $table = "project_item";

function project(){

return $this->belongsTo('App\Project');

}
}

Project.php(模型)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
protected $table = "project";



public function projectitem()
{
return $this->hasMany('App\Projectitem');
}

}

迁移projectitem表

 public function up()
{
Schema::create('project_item', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('project_name');
$table->string('client');
$table->text('description');
$table->float('time_span');
$table->text('text_report');
$table->text('images_before');
$table->text('images_after');
$table->boolean('facebook');
$table->boolean('instagram');
$table->boolean('linkedin');
$table->boolean('website');
$table->integer('belongs_to');
$table->string('created_by');
$table->timestamps();
});
}

最佳答案

检查您的数据库中是否有 Projectitem 模型的记录。

听起来它正在返回 null,这意味着没有找到您指定的 where 条件的结果。

关于php - Laravel 试图获取非对象的属性 'project_name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55278617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com