如何在 Laravel 的同一视图中检索或返回不同的记录?
Posted
技术标签:
【中文标题】如何在 Laravel 的同一视图中检索或返回不同的记录?【英文标题】:How can i retrieve or return different records on the same view in Laravel? 【发布时间】:2020-04-19 01:37:23 【问题描述】:请尝试在仪表板视图中检索事件数据和学生数据,但它们似乎相互冲突。学生记录也显示在我的事件记录应该显示的位置。 看起来是这样的:
路线
Route::post('/dashboard/submit', 'EventController@submit');
Route::get('/dashboard', 'EventController@getevent');
Route::post('/dashboard/student/pay', 'PaymentController@submit');
Route::post('register','StudentRegister@register');
Route::get('/dashboard', 'StudentRegister@getStudents');
EventController.php
class EventController extends Controller
public function submit(Request $request)
$message = new Event;
$message->title = $request->input('title');
$message->deadline = $request->input('deadline');
$message->message = $request->input('message');
$message->save();
return redirect('/dashboard')->with('status', 'Event Added');
public function getevent()
$msgevent = Event::orderBy('id', 'DESC')->get();
return view('dashboard')->with('dashboard', $msgevent);
studentRegister.php
public function getStudents()
$Students = Student::all();
return view('dashboard') -> with('dashboard', $Students);
我的 dashboard.blade.php
中有这些<div class="paginate-no-scroll 5">
<div class="items">
@if(count($dashboard) > 0)
@foreach ($dashboard as $msg)
<div>
<svg class="bd-placeholder-img mr-2 rounded" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: 32x32">
<rect fill="#007bff"/>
<text x="50%" y="50%" fill="#007bff" dy=".3em"></text>
</svg>
<p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">
<strong class="d-block text-gray-dark">$msg -> title</strong> | $msg -> deadline |<b
style="color: #25027f"> @username</b> | <a href="#" style="text-decoration: underline;">Edit</a> |
<a href="/delete/!! $msg -> id !!" style="text-decoration: underline;">Delete</a><br>
$msg -> message
</div>
@endforeach
@endif
<table id="tableData" class="table table-bordered table-striped">
<thead>
<th>#</th>
<th>Student ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Action</th>
</thead>
@if(count($dashboard) > 0)
@foreach ($dashboard as $std)
<tr>
<td style="text-align:center"><img src="uploads/$std->image" style="width: 50px;height: 50px; border-radius: 50%" ></td>
<td style="text-align:center; padding-top: 25px;">$std->studentID</td>
<td style="text-align:center; padding-top: 25px;">$std->fName</td>
<td style="text-align:center; padding-top: 25px;">$std->surName</td>
<td style="text-align:center; padding-top: 25px;"><a href="" class="btn btn-primary"> view</a>
</td>
</tr>
@endforeach
@endif
</tbody>
</table>
【问题讨论】:
【参考方案1】:为什么你用同名调用两个事件。你改变你的代码如下。
studentRegister.php
public function getStudents()
$students = Student::all();
$msgevents = Event::all()->orderBy('id', 'DESC');
return view('dashboard', [
'students' => $students,
'msgevents' => $msgevents
]);
不要忘记在文件中添加use App\Event;
。
dashboard.blade.php
<div class="paginate-no-scroll 5">
<div class="items">
@if(count($msgevents) > 0)
@foreach ($msgevents as $msg)
<div>
<svg class="bd-placeholder-img mr-2 rounded" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: 32x32"><rect fill="#007bff"/><text x="50%" y="50%" fill="#007bff" dy=".3em"></text></svg>
<p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">
<strong class="d-block text-gray-dark">$msg -> title</strong> | $msg -> deadline |<b style="color: #25027f"> @username</b> | <a href="#" style="text-decoration: underline;">Edit</a> | <a href="/delete/!! $msg -> id !!" style="text-decoration: underline;">Delete</a><br>
$msg -> message
</div>
@endforeach
@endif
<table id="tableData" class="table table-bordered table-striped">
<thead>
<th>#</th>
<th>Student ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Action</th>
</thead>
@if(count($students) > 0)
@foreach ($students as $std)
<tr>
<td style="text-align:center"><img src="uploads/$std->image" style="width: 50px;height: 50px; border-radius: 50%" ></td>
<td style="text-align:center; padding-top: 25px;" >$std->studentID</td>
<td style="text-align:center; padding-top: 25px;">$std->fName</td>
<td style="text-align:center; padding-top: 25px;">$std->surName</td>
<td style="text-align:center; padding-top: 25px;"><a href="" class="btn btn-primary"> view</a>
</td>
</tr>
@endforeach
@endif
</tbody>
</table>
【讨论】:
我得到未定义的变量:msgevents 现在我编辑了试试我的改变看看。从$msgevents = Event::orderBy('id', 'DESC')->get();
到$msgevents = Event::all()->orderBy('id', 'DESC');
。
这些是错误 [ * undefined variable:msgevents*, possible typo $msgevents 你的意思是 $students,最后修复错字]
你为什么要为同一个视图使用两个函数?有什么要求吗?否则我将更改代码。
删除一种方法,并在一种方法中同时添加。【参考方案2】:
您在控制器中使用具有相同with
变量dashboard
的相同视图。
在 EventController.php 中 获取事件:
return view('dashboard') -> with('dashboard', $msgevent);
在 studentRegister.php 中 获取学生:
return view('dashboard') -> with('dashboard', $Students);
在相应的文件中将它们更改为更合适的名称,例如:
return view('dashboard') -> with('events', $msgevent);
return view('dashboard') -> with('students', $Students);
然后在您的 blade
文件中使用这些名称,例如:
<div class="paginate-no-scroll 5">
<div class="items">
@if($events && count($events) > 0)
@foreach ($events as $msg)
<div>
<svg class="bd-placeholder-img mr-2 rounded" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: 32x32">
<rect fill="#007bff"/>
<text x="50%" y="50%" fill="#007bff" dy=".3em"></text>
</svg>
<p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">
<strong class="d-block text-gray-dark">$msg -> title</strong> | $msg -> deadline |<b
style="color: #25027f"> @username</b> | <a href="#" style="text-decoration: underline;">Edit</a> |
<a href="/delete/!! $msg -> id !!" style="text-decoration: underline;">Delete</a><br>
$msg -> message
</div>
@endforeach
@endif
<table id="tableData" class="table table-bordered table-striped">
<thead>
<th>#</th>
<th>Student ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Action</th>
</thead>
@if($students && count($students) > 0)
@foreach ($students as $std)
<tr>
<td style="text-align:center"><img src="uploads/$std->image" style="width: 50px;height: 50px; border-radius: 50%" ></td>
<td style="text-align:center; padding-top: 25px;">$std->studentID</td>
<td style="text-align:center; padding-top: 25px;">$std->fName</td>
<td style="text-align:center; padding-top: 25px;">$std->surName</td>
<td style="text-align:center; padding-top: 25px;"><a href="" class="btn btn-primary"> view</a>
</td>
</tr>
@endforeach
@endif
</tbody>
</table>
【讨论】:
我现在得到未定义的变量:事件 由于我们使用相同的刀片文件在 2 个控制器中渲染,我们应该在使用前检查传入的变量是否存在到视图中。我已经更新了答案中的刀片文件以显示,例如:@if($events && count($events) > 0)
和 @if($students && count($students) > 0)
。以上是关于如何在 Laravel 的同一视图中检索或返回不同的记录?的主要内容,如果未能解决你的问题,请参考以下文章
无法在视图中显示或检索文件,该文件以路径的形式上传到数据库中。 (使用 laravel )