Ich habe ein Problem bei der Erstellung einer Angebots-App festgestellt. Die Fehlermeldung lautet:"Versuchen, Eigentum von Nicht-Objekt zu erhalten" in Laravel 5
<div class="info">Created by <a href="#"><?php echo e($quotes[$i]->authors->name); ?></a> on <?php echo e($quotes[$i]->created_at); ?> </div>
Fehler:
Trying to get property of non-object (View: D:\Bureaublad\Websites\Oefenen laravel\firstapp\resources\views\index.blade.php)
Mein 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>
Mein 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]);
}
}
Meine Routen-Datei :
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'
]);
Wie kann ich herausfinden, wo das Problem liegt?
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');
}
}
Kann geschlossen werden unter: _Diese Frage wurde durch ein Problem verursacht, das nicht mehr reproduziert werden kann oder ein einfacher Tippfehler. Während ähnliche Fragen hier zum Thema gehören, wurde diese in einer Weise gelöst, die den zukünftigen Lesern kaum helfen wird. – halfer