[英]Trying to get property 'id' of non-object on update function
我有一個簡單的博客,現在我可以在我的博客中添加、刪除元素我希望用戶能夠更新標簽。
這是我的 phpmyadmin 中的頁面列表標簽表
page_lit_tags table
id name
1 test1
2 test2
這是標簽表
這是標簽 controller
use Illuminate\Http\Request;
use App\Tag;
use App\PageList;
class TagController extends Controller
{
public function index()
{
$tags = Tag::all();
$pages = PageList::all();
return view('pages.index', compact('tags', 'pages'));
}
public function store(Request $request)
{
$this->validate($reguest, array('name'=>'required|max:255'));
$tag = new Tag;
$tag->name = $request->name;
$tag->save();
$request->session()->flash('succes', 'Tag was successful created!');
return redirect()->route('pages.index');
}
}
這是我的頁面列表 controller 更新 function
public function update(Request $request, $id)
{
$pages = PageList::find($id);
$pages->pagetitle = $request->get('pagetitle');
$pages->articlelist = $request->get('articlelist');
$pages->status = $request->get('status');
$pages->save();
$pages->tags()->saveMany([
new Tag(),
new Tag(),
]);
return redirect('/pages')->with('success', 'pages updated!');
}
這是我的頁面 inde.blade.php 我在其中顯示我的數據
<tbody>
@foreach ($pages as $page)
<tr>
<td>
{{$page->id}}
</td>
<td>
{{$page->pagetitle}}
</td>
<td>
{{$page->articlelist}}
</td>
<td>
<button type="button" class="btn btn-sm btn-success" data-toggle="modal" data-target="#exampleModal{{$page->id}}">
{{ __('view tags') }}
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal{{$page->id}}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Tag List</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<table class="table">
<thead class=" text-primary">
<th>
ID
</th>
<th>
name
</th>
</thead>
<tbody>
@foreach ($page->tags as $tag)
<tr>
<td>
{{ $tag->tag->id }}
</td>
<td>
{{ $tag->tag->name }}
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
@endforeach
</tbody>
注意:我對 laravel 伙計們太陌生了。
當我運行我的應用程序時,出現以下問題
Trying to get property 'id' of non-object (View: C:\custom-xammp\htdocs\royalad-dashboard\resources\views\pages\index.blade.php)
這是我的 repo存儲庫
我在我的代碼中做錯了什么?
如果$page->tags
給你一個標簽的集合,你應該簡單地做$tag->id
而不是$tag->tag->id
(對於 name 屬性也是如此)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.