我对Laravel很陌生,只是想指出点什么。我在读取显示页时遇到问题,当我打开显示页(show.blade.php)时,它会返回此错误。
试图获取非对象的属性“project_name”(视图:project_name)
我不知道我做错了什么,因为我让它正常工作,一旦我再次打开展示页,它就会返回错误。
任何帮助都将不胜感激!
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> {{$projectitem->time_span}} Uren</h2><br>
<h2 class="mt-2 mb-2"><b>Opdrachtgever:</b> {{$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: </b></h2><br>
{!!$projectitem->images_before!!}
<h2 class="mt-4 mb-2"><b>Foto's achteraf: </b></h2><br>
{!!$projectitem->images_after!!}
<div class="row">
<div class="col-md-6">
<h2 class="mt-4"><b>Beschikbaar op: </b></h2>
<h4>Facebook: <?php if($projectitem->facebook == 1){echo "wel";}else{echo "niet";}?></h4>
<h4>Instagram: <?php if($projectitem->instagram == 1){echo "wel";}else{echo "niet";}?></h4>
<h4>Linkedin: <?php if($projectitem->linkedin == 1){echo "wel";}else{echo "niet";}?></h4>
<h4>Website: <?php if($projectitem->website == 1){echo "wel";}else{echo "niet";}?></h4>
</div>
</div>
<h4 class="mt-4 mb-2"><b>Aangemaakt door:</b> {{$projectitem->created_by}}</h4><br>
<div class="row">
<div class="col-md-6">
<h4 class="mt-4 mb-2 floatl "><b>Aangemaakt op:</b> {{$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> {{$projectitem->updated_at}}</h4><br>
</div>
</div>
</div>
</div>
</div>
</div>
@endsectionProjectitem.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');
}
}迁移项目表
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();
});
}发布于 2019-03-21 10:54:01
检查您的数据库中是否有Projectitem模型的记录。
它听起来像是在返回null,这意味着您指定的where条件没有结果。
发布于 2019-03-21 10:51:41
在您的刀片中使用如下代码:
$projectitem[' project_name'];如果它给出了与undefined index相关的错误,请将索引放在值之前,如下
$projectitem[0][' project_name']; 发布于 2019-03-21 11:32:18
这是什么
尝试获取非对象属性意味着--您正在使用ProjectItem模型数据,而该数据不存在!!
溶液
您可以通过findOrFail()或firstOrFail()进行更好的检查。就像这样-
$project = Project::where('user_id', $user_id)->firstOrFail();
$projectitem = Projectitem::where('project_id', $id)->firstOrFail();阅读更多关于这些-> What is the difference between find(), findOrFail(), first(), firstOrFail(), get(), list(), toArray()的信息
https://stackoverflow.com/questions/55278617
复制相似问题