我如何在 Laravel 的同一视图中检索或返回不同的记录?

请尝试在仪表板视图上检索事件数据和学生数据,但它们似乎相互冲突。学生记录也显示在我的事件记录应该显示的位置。这是它的样子:

http://img4.mukewang.com/63aeac250001529013570766.jpg

航线


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

事件控制器.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);

    }

}

学生注册.php


public function getStudents(){

    $Students = Student::all();


    return view('dashboard') -> with('dashboard', $Students);

}


一只萌萌小番薯
浏览 91回答 2
2回答

月关宝盒

为什么用相同的名称调用两个事件。你改变你的代码如下。studentRegister.phppublic function getStudents(){&nbsp; &nbsp; $students = Student::all();&nbsp; &nbsp; $msgevents = Event::all()->orderBy('id', 'DESC');&nbsp; &nbsp; return view('dashboard', [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'students' => $students,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'msgevents' => $msgevents&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;]);}不要忘记use App\Event;在文件中添加。dashboard.blade.php<div class="paginate-no-scroll 5">&nbsp; &nbsp; &nbsp; <div class="items">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @if(count($msgevents) > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach ($msgevents as $msg)&nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <svg class="bd-placeholder-img mr-2 rounded" width="24" height="24" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: 32x32"><rect width="30%" height="30%" fill="#007bff"/><text x="50%" y="50%" fill="#007bff" dy=".3em"></text></svg>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{$msg -> message}}&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@endforeach&nbsp; &nbsp; &nbsp; &nbsp; @endif<table id="tableData" class="table table-bordered table-striped">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; <th>#</th>&nbsp; &nbsp; <th>Student ID</th>&nbsp; &nbsp; <th>First Name</th>&nbsp; &nbsp; <th>Last Name</th>&nbsp; &nbsp; <th>Action</th>&nbsp; </thead>&nbsp; &nbsp; @if(count($students) > 0)&nbsp; &nbsp; &nbsp; @foreach ($students as $std)&nbsp; <tr>&nbsp; &nbsp; <td style="text-align:center"><img src="uploads/{{$std->image}}" style="width: 50px;height: 50px; border-radius: 50%" alt="{{$std->image}}"></td>&nbsp; &nbsp; <td style="text-align:center; padding-top: 25px;" >{{$std->studentID}}</td>&nbsp; &nbsp; <td style="text-align:center; padding-top: 25px;">{{$std->fName}}</td>&nbsp; &nbsp; <td style="text-align:center; padding-top: 25px;">{{$std->surName}}</td>&nbsp; &nbsp; <td style="text-align:center; padding-top: 25px;"><a href="" class="btn btn-primary"> view</a>&nbsp;&nbsp; &nbsp; </td>&nbsp; </tr>&nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; @endif&nbsp; </tbody>&nbsp; &nbsp; &nbsp; &nbsp; </table>

神不在的星期二

您在控制器中使用具有相同with变量的相同视图。dashboard在 EventController.php getEvent 中:return view('dashboard') -> with('dashboard', $msgevent);在 studentRegister.php getStudents 中: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">&nbsp; &nbsp; <div class="items">&nbsp; &nbsp; &nbsp; &nbsp; @if($events && count($events) > 0)&nbsp; &nbsp; &nbsp; &nbsp; @foreach ($events as $msg)&nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <svg class="bd-placeholder-img mr-2 rounded" width="24" height="24" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: 32x32">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <rect width="30%" height="30%" fill="#007bff"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <text x="50%" y="50%" fill="#007bff" dy=".3em"></text>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </svg>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong class="d-block text-gray-dark">{{$msg -> title}}</strong> | {{$msg -> deadline}} |<b&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; style="color: #25027f"> @username</b> | <a href="#" style="text-decoration: underline;">Edit</a> |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="/delete/{!! $msg -> id !!}" style="text-decoration: underline;">Delete</a><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{$msg -> message}}&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; &nbsp; &nbsp; <table id="tableData" class="table table-bordered table-striped">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>#</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Student ID</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>First Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Last Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Action</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @if($students && count($students) > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach ($students as $std)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td style="text-align:center"><img src="uploads/{{$std->image}}" style="width: 50px;height: 50px; border-radius: 50%" alt="{{$std->image}}"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td style="text-align:center; padding-top: 25px;">{{$std->studentID}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td style="text-align:center; padding-top: 25px;">{{$std->fName}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td style="text-align:center; padding-top: 25px;">{{$std->surName}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td style="text-align:center; padding-top: 25px;"><a href="" class="btn btn-primary"> view</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; &nbsp; &nbsp; </table>
打开App,查看更多内容
随时随地看视频慕课网APP