我的某些观点是空的。有关各方是必须提供外键信息的各方。
我使用Laravel 5.8开发了一个应用程序,并且我有一个控制器可以检测不同的香水线,一个产品控制器和一个香水控制器。我将“产品”表的标识符迁移到“香水”表中。当我想在视图级别显示有关香水表的信息时,出现此错误:
尝试获取无对象的属性“名称”(显示:C:\ laragon \ www \ venome \ resources \ views \ admin \ fragrance \ index.blade.php)。
所以我更改了代码,但是现在部分视图为空。
路线
Route::group(['prefix' => 'admin', 'middleware' => 'auth', 'namespace' => 'admin'], function () {
Route::get('dashboard', 'DashboardController@index')->name('admin.dashboard');
Route::resource('ligne', 'LigneController');
Route::resource('produit', 'ProduitController');
Route::resource('fragrance', 'FragranceController');
});
控制器
namespace App\Http\Controllers\Admin;
use App\Fragrance;
use App\Http\Controllers\Controller;
use App\Ligne;
use App\Produit;
class FragranceController extends Controller
{
public function index()
{
$fragrances = Fragrance::all();
$lignes = Ligne::all();
$produits = Produit::all();
return view('admin.fragrance.index', compact('fragrances', 'lignes', 'produits'));
}
}
模型
class Fragrance extends Model
{
public function lignes()
{
return $this->belongsTo(Ligne::class);
}
public function produits()
{
return $this->belongsTo(Produit::class);
}
}
class Ligne extends Model
{
public function fragrances()
{
return $this->hasMany(Fragrance::class);
}
}
class Produit extends Model
{
public function fragrance()
{
return $this->hasMany(Fragrance::class);
}
}
//香水视图
@extends('layouts.auth')
@section('title','Fragrance')
@push('css')
@endpush
@section('content')
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<a href="{{ route('fragrance.create') }}" class="btn btn-primary">create</a>
@include('layouts.partial.message')
<div class="card">
<div class="card-header" data-background-color="purple">
<h4 class="title">Fragrance</h4>
</div>
<div class="card-content table-responsive">
<table id="table" class="table hover" cellspacing="0" width="100%">
<thead class="text-primary">
<th>ID</th>
<th>Ligne </th>
<th>Produit </th>
<th>Nom</th>
<th>Description</th>
<th>image</th>
<th>Créer le</th>
<th>Derniére Mis à Jour</th>
<th>Action</th>
</thead>
<tbody>
@foreach($fragrances as $key=>$fragrance)
<tr>
<td>{{ $key + 1 }}</td>
<td>{{ $fragrance->ligne->name }}</td>
<td>{{ $fragrance->produit->title }}</td>
<td>{{ $fragrance->name }}</td>
<td>{{ $fragrance->description }}</td>
<td><img class="img-responsive img-thumbnail" src="{{ asset('uploads/fragrance/'.$fragrance->image) }}" style="height: 100px; width: 100px" alt=""></td>
<td>{{ $fragrance->created_at }}</td>
<td>{{ $fragrance->updated_at }}</td>
<td>
<a href="{{ route('fragrance.edit',$fragrance->id) }}" class="btn btn-primary">Edit</a>
<form id="delete-form-{{ $fragrance->id }}" action="{{ route('fragrance.destroy',$fragrance->id) }}" style="display: none;" method="POST">
@csrf
@method('DELETE')
</form>
<button type="button" class="btn btn-danger" onclick="if(confirm('voulez-vous vraiment suppimer?')){
event.preventDefault();
document.getElementById('delete-form-{{ $fragrance->id }}').submit();
}else {
event.preventDefault();
}"><i class="material-icons">delete</i></button>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
//第二眼看到空表
@extends('layouts.auth')
@section('title','Fragrance')
@push('css')
@endpush
@section('content')
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<a href="{{ route('fragrance.create') }}" class="btn btn-primary">create</a>
@include('layouts.partial.message')
<div class="card">
<div class="card-header" data-background-color="purple">
<h4 class="title">Fragrance</h4>
</div>
<div class="card-content table-responsive">
<table id="table" class="table hover" cellspacing="0" width="100%">
<thead class="text-primary">
<th>ID</th>
<th>Ligne </th>
<th>Produit </th>
<th>Nom</th>
<th>Description</th>
<th>image</th>
<th>Créer le</th>
<th>Derniére Mis à Jour</th>
<th>Action</th>
</thead>
<tbody>
@foreach($fragrances as $key=>$fragrance)
<tr>
<td>{{ $key + 1 }}</td>
<td>
@if(!empty($fragrance->ligne)) {{ $fragrance->ligne->name }} @endif
</td>
<td>
@if(!empty($fragrance->produit)) {{ $fragrance->produit->title }} @endif
</td>
<td>{{ $fragrance->name }}</td>
<td>{{ $fragrance->description }}</td>
<td><img class="img-responsive img-thumbnail" src="{{ asset('uploads/fragrance/'.$fragrance->image) }}" style="height: 100px; width: 100px" alt=""></td>
<td>{{ $fragrance->created_at }}</td>
<td>{{ $fragrance->updated_at }}</td>
<td>
<a href="{{ route('fragrance.edit',$fragrance->id) }}" class="btn btn-primary">Edit</a>
<form id="delete-form-{{ $fragrance->id }}" action="{{ route('fragrance.destroy',$fragrance->id) }}" style="display: none;" method="POST">
@csrf
@method('DELETE')
</form>
<button type="button" class="btn btn-danger" onclick="if(confirm('voulez-vous vraiment suppimer?')){
event.preventDefault();
document.getElementById('delete-form-{{ $fragrance->id }}').submit();
}else {
event.preventDefault();
}"><i class="material-icons">delete</i></button>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
@push('scripts')
@endpush
我希望显示所有信息