路线
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>
错误是: 未定义的变量:user_id(视图:D:\ laragon \ www \ SampleContacts \ resources \ views \ adminContacts \ index.blade.php)
我想念什么吗?这个想法是将$ user_id传递给adminContact.create表单,我可以在其中使用它来设置 $ contact-> owner_id = $ user_id; 因此输入的联系人会在该用户表下终止。
答案 0 :(得分:1)
我不认为您可以像现在那样将变量传递到视图中。
在您的控制器中,尝试更改此行:
return view('adminContacts.index')->withContacts($contacts)->withUserId($user_id);
对此:
return view('adminContacts.index')->with('contacts', $contacts)->with('user_id', $user_id);
答案 1 :(得分:1)
通过这种方式,您可以在将变量传递给视图时显式命名
return view('adminContacts.index')->with('user_id', $user_id);
或
return view('adminContract.index', compact('user_id'));
我不确定通过withUserId
传递给视图时变量的名称是什么,但是我猜这是问题所在。
答案 2 :(得分:0)
发现了问题。
@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
在有{{ route('adminContacts.index') }}
的任何地方,它也需要与$user_id
一起传递,因为我也在路由中将其用作'/adminContacts/{user_id}'
谢谢您的帮助。