尝试获取更新功能上非对象的属性“ id”

时间:2019-10-11 13:32:00

标签: php html mysql laravel laravel-blade

我有一个简单的博客,现在我希望用户能够更新标签。我可以在博客中添加,删除元素。

这是我的phpmyadmin中的页面列表标签表

function dropdDowndata() {
        var _env = getEnv();
        var data;
        getClientList().then(function(response) {
          data = response;
        });

};

function getClientList = function() {
    debugger;
    var resetResponse = "";
    var refId = GUIDUtil.Guid.newGuid().toString("N");
    var details = {};
    var params = {
        "Request": {
            "header": {
                "serviceContext": {
                    "apiVersion": "1.0",
                    "lineOfBusiness": "SPD"
                },
                "securityContext": {
                    "securityType": "apiKey",
                    "apiKey": "26283629239362127"
                }
            },
            "details": details
        }
    };
    var clientListParams = {
        url: getHostName() + '/test/client',
        data: params,
        method: 'POST',

        headers: {
            'Content-Type': 'application/json'
        }
    };

    return axios(clientListParams).then(function(response) {
        return response.data;

    }).catch(error);
};

这是标签表

enter image description here

这里是标签控制器

page_lit_tags table

    id  name

    1   test1
    2   test2

这是我的页面列表控制器更新功能

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

这是我的页面inde.blade.php,我正在其中显示数据

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

注意:我对laravel家伙很陌生。

运行我的应用程序时出现以下问题

<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>

这是我的仓库repository

我的代码在做什么错?

1 个答案:

答案 0 :(得分:1)

如果$page->tags为您提供了一个标签集合,则您只需做$tag->id而不是$tag->tag->id(对name属性也是如此)