Попытка получить свойство 'id' необъекта в функции обновления - PullRequest
0 голосов
/ 11 октября 2019

У меня есть простой блог, я могу добавлять, удалять элементы в своем блоге, теперь я хочу, чтобы пользователь мог обновлять теги.

Вот таблица тегов списка страниц в моем phpmyadmin

page_lit_tags table

    id  name

    1   test1
    2   test2

Вот таблица тегов

enter image description here

Здесь находится контроллер тегов

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');
    }
}

Вот функция обновления контроллера списка моих страниц

  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">&times;</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)

Вот мой репозиторий репозиторий

Что я делаю неправильно в моих кодах?

1 Ответ

1 голос
/ 11 октября 2019

, если $page->tags дает вам набор тегов, вы должны просто сделать $tag->id вместо $tag->tag->id (и то же самое для атрибута name)

...