dongzheng8463 2018-11-21 14:57
浏览 433
已采纳

未定义的变量:Laravel视图中的user_id

The Routes Route::get('/adminContacts/{user_id}', 'AdminContactsController@index')->name('adminContacts.index')->middleware('is_admin'); Route::get('/adminContacts/{user_id}/create', 'AdminContactsController@create')->name('adminContacts.create')->middleware('is_admin');

adminContactController

public function index($user_id)
{
    // Confirm User exists
    User::findOrFail($user_id);

    $filter = request('filter', NULL);
    $contacts = NULL;

    if ($filter == NULL)
        $contacts = Contacts::query()->where('owner_id', $user_id)->sortable()->paginate(5);
    else
    $contacts = Contacts::query()->where('owner_id', $user_id)->where('name', 'like', '%'.$filter.'%')
                                 ->orWhere('number', 'like', '%'.$filter.'%')
                                 ->sortable()->paginate(5);

    return view('adminContacts.index')->withContacts($contacts)->withUserId($user_id);
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create($user_id)
{
    return view('adminContacts.create')->withUserId($user_id);
}

index.blade.php

<div class="container">
    <h1 class="jumbotron">Sample Phone Book</h1>

    <a href="{{ route('adminContacts.create', ['user_id' => $user_id] ) }}" class="btn btn-primary btn-block" style="margin-bottom: 5px">Add Contact</a>
</div>

<!--Search Field -->
<div class="container">
    <form method="GET" action="{{ route('adminContacts.index') }}">
        <input type="text" name='filter' class='input' placeholder='Search' value="{{ request('filter') }}">
            @if (request('filter'))
                <a class="btn btn-primary btn-sm" href="{{ route('adminContacts.index') }}">X</a>
            @endif
    </form>
</div>

<!-- Table -->
<div class="container">
    <div class="row" >
        <table class="table table-hover" id="contactsTable">
            <thead>
                <tr>
                    <th>#</th>
                    <th>@sortablelink('name', 'Contact Name')</th>
                    <th>@sortablelink('number', 'Phone Number')</th>
                    <th>
                        <button class="btn btn-primary btn-sm">Prepend</button>
                    </th>
                </tr>
            </thead>

        <!--Loop through all the cutomers and output them on the table-->
        @foreach($contacts as $contact)
            <tbody>
                <tr>
                    <td>{{ $contact->id }}</td>
                    <td>{{ $contact->name }}</td>
                    <td>{{ $contact->number }}</td> 
                    <td>
                        <a href="{{ route('adminContacts.edit', $contact->id) }}" class="btn btn-primary btn-sm">View</a>   
                    </td>
                </tr>
            </tbody>
        @endforeach
        </table>

        {!! $contacts->appends(\Request::except('page'))->render() !!}
    </div>  
</div>

The error is: Undefined variable: user_id (View: D:\laragon\www\SampleContactsesources\views\adminContacts\index.blade.php)

Am I missing something? The idea is to pass the $user_id to the adminContact.create form where i can use it to set $contact->owner_id = $user_id; so the entered contact apears under that users table.

  • 写回答

3条回答 默认 最新

  • douhui9192 2018-11-22 14:16
    关注

    Found the problem.

    @extends('layouts.app')
    
    @section('content')
    
    <div class="container">
        <h1 class="jumbotron">Sample Phone Book</h1>
    
        <a href="{{ route('adminContacts.create', ['user_id' => $user_id] ) }}" class="btn btn-primary btn-block" style="margin-bottom: 5px">Add Contact</a>
    </div>
    
    <!--Search Field -->
    <div class="container">
        <form method="GET" action="{{ route('adminContacts.index', $user_id) }}">
            <input type="text" name='filter' class='input' placeholder='Search' value="{{ request('filter') }}">
                @if (request('filter'))
                    <a class="btn btn-primary btn-sm" href="{{ route('adminContacts.index',  $user_id) }}">X</a>
                @endif
        </form>
    </div>
    
    <!-- Table -->
    <div class="container">
        <div class="row" >
            <table class="table table-hover" id="contactsTable">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>@sortablelink('name', 'Contact Name')</th>
                        <th>@sortablelink('number', 'Phone Number')</th>
                        <th>
                            <button class="btn btn-primary btn-sm">Prepend</button>
                        </th>
                    </tr>
                </thead>
    
            <!--Loop through all the cutomers and output them on the table-->
            @foreach($contacts as $contact)
                <tbody>
                    <tr>
                        <td>{{ $contact->id }}</td>
                        <td>{{ $contact->name }}</td>
                        <td>{{ $contact->number }}</td> 
                        <td>
                            <a href="{{ route('adminContacts.edit', $contact->id) }}" class="btn btn-primary btn-sm">View</a>   
                        </td>
                    </tr>
                </tbody>
            @endforeach
            </table>
    
            {!! $contacts->appends(\Request::except('page'))->render() !!}
        </div>  
    </div>
    
    @endsection
    

    Anywhere where there is a {{ route('adminContacts.index') }} it also needed the $user_id to be passed along with it because I'm also using it in the routes as '/adminContacts/{user_id}'

    Thank you for all the help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加