견적을 작성하는 중 문제가 발생했습니다. 오류 메시지는 다음과 같습니다Laravel 5의 "non-object 속성 가져 오기 중"
<div class="info">Created by <a href="#"><?php echo e($quotes[$i]->authors->name); ?></a> on <?php echo e($quotes[$i]->created_at); ?> </div>
오류 :
Trying to get property of non-object (View: D:\Bureaublad\Websites\Oefenen laravel\firstapp\resources\views\index.blade.php)
내 index.blade.php :
@if(count($errors) > 0)
<section class="info-box fail">
@foreach($errors->all() as $error)
{{ $error }}
@endforeach
</section>
@endif
@if(Session::has('success'))
<section class="info-box success">
{{ Session::get('success') }}
</section>
@endif
<section class="quotes">
<h1>Latest Quotes</h1>
@for($i = 0; $i < count($quotes); $i++)
<article class="quote">
<div class="delete"><a href="{{ route('delete', ['quote_id' => $quotes[$i]->id]) }}">x</a></div>
{{ $quotes[$i]->quote }}
<div class="info">Created by <a href="#">{{ $quotes[$i]->authors->name }}</a> on {{ $quotes[$i]->created_at }} </div>
</article>
@endfor
<div class="Pagination">
Pagination
</div>
내 QuoteController.php :
<?php
namespace App\Http\Controllers;
use App\Author;
use App\Quote;
use Illuminate\Http\Request;
class QuoteController extends Controller
{
public function getIndex()
{
$quotes = Quote::all();
return view('index', ['quotes' => $quotes]);
}
public function postQuote(Request $request)
{
$this->validate($request, [
'author' => 'required|max:60|alpha',
'quote' => 'required|max:500'
]);
$authorText = ucfirst($request['author']);
$quoteText = $request['quote'];
$author = Author::where('name', $authorText)->first();
if (!$author) {
$author = new Author();
$author->name = $authorText;
$author->save();
}
$quote = new Quote();
$quote->quote = $quoteText;
$author->quotes()->save($quote);
return redirect()->route('index')->with([
'success' => 'Quote saved!'
]);
}
public function getDeleteQuote($quote_id)
{
$quote = Quote::find($quote_id);
$author_deleted = false;
if (count($quote->author->quotes) === 1) {
$quote->author->delete();
$author_deleted = true;
}
$msg = $author_deleted ? 'Quote and author deleted!' : 'Quote deleted!';
$quote->delete();
return redirect()->route('index')->with(['succes' => $msg]);
}
}
내 루트 파일 :
Route::get('/', [
'uses' => '[email protected]',
'as' => 'index'
]);
Route::post('/new', [
'uses' => '[email protected]',
'as' => 'create'
]);
Route::get('/delete/{quote_id}', [
'uses' => '[email protected]',
'as' => 'delete'
]);
문제의 위치를 어떻게 알 수 있습니까?
Author.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Author extends Model
{
public function quotes()
{
return $this->hasMany('App\Quote');
}
}
Quote.php
내가 말하는 게 아니에요<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Quote extends Model
{
public function author()
{
return $this->belongsTo('App\Author');
}
}
아래에서 마감 될 수 있습니다.이 질문은 더 이상 생방송 될 수없는 문제점이나 간단한 typographical 오류로 인한 것입니다. 비슷한 질문이 여기에 주제 일지 모르지만, 이것은 미래의 독자를 돕지 않을 방법으로 해결되었습니다 ._ – halfer