Laravel:Same Variable在不同的视图中不起作用

问题描述 投票:0回答:1

如果我使用变量测试一切正常:

我的控制器:

public function index()
{
    $row = Test::count();
    $tests = Test::latest()->paginate($row);
    return view('tests.index',compact('tests'));
}

public function show(Test $test)
{
    return view('tests.show', compact('test'));
}

我的索引视图:

@foreach($tests as $test)
   <div class="event">
     <h4 class="eventtitle text-j4y-dark">{{ $test->body }}</h4>
     <a href="{{ route('tests.show', $test->id) }}">Mehr lesen »</a>
   </div>
@endforeach

我的秀景:

<h4 class="eventtitle text-j4y-dark">{{ $test->body }}</h4>

但是,如果我使用变量事件索引页面,那么它会向我展示我的所有事件,但如果我点击一个事件获取更多详细信息,它只显示我什么都没有,但我不知道为什么?它的代码只有不同的变量? 我不知道该怎么办

我的控制器:

public function index()
{
    $row = Event::count();
    $events = Event::latest()->paginate($row);
    return view('tests.index',compact('events'));
}

public function show(Event $event)
{
    return view('tests.show', compact('event'));
}

我的索引视图:

@foreach ($events as $event)
  <div class="event">
    <h4 class="eventtitle text-j4y-dark">{{ $event->eventtitel }}</h4>
    <div class="date"><i class="far fa-calendar-alt"></i>{{ date("d.m.Y", strtotime($event->datum)) }}</div>
    <p class="eventtext" id="eventtext">{{ $event->eventbeschreibung }}</p>
    <a href="{{ route('tests.show', $event->id) }}">Mehr lesen »</a>
  </div>
@endforeach

我的秀景:

<div class="event">
  <h4 class="eventtitle">{{ $event->eventtite l}}</h4>
  <div class="date"><i class="far fa-calendar-alt"></i>{{ date("d.m.Y", strtotime($event->datum)) }}</div>
  <p class="eventtext" id="eventtext">{{ $event->eventbeschreibung }}</p>
</div>

如果我使用dd($ test)它会告诉我这个:

Test {#692 ▼
#fillable: array:1 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:4 [▶]
#original: array:4 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
 }

如果我使用dd($ event)它会告诉我这个:

Event {#660 ▼
#fillable: array:5 [▶]
+timestamps: false
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▶]

}

如果我在我的控制器中使用echo $ event它会告诉我:[]

如果我使用print_r($ event)

它告诉我这个:

App\Event Object ( [fillable:protected] => Array ( [0] => firmenname [1] => eventtitel [2] => eventbeschreibung [3] => datum [4] => anhang ) [timestamps] => [connection:protected] => [table:protected] => [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => [wasRecentlyCreated] => [attributes:protected] => Array ( ) [original:protected] => Array ( ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) 
laravel
1个回答
0
投票

好吧,你的代码有很多问题。首先是分页。

$row = Event::count(); //This is basically counting all the events you have
$events = Event::latest()->paginate($row); // And here u telling to pageinit with all events in single page. 

我个人认为这不是正确的方法。如果我不得不这样做,我会这样做

 $events = Event::orderBy('created_at','desc')->get();  //This will allthe even in single page with the latest one. 

如果您只需要所有事件并且不需要排序,我会这样做

 $events = Event::all(); //This is give all the events

回到你的问题。

在控制器方法内定义的变量保留在该方法和该方法呈现的视图内。除非您在提供商中全局声明它,否则它不会通过另一个刀片。

对于你的情况,我会做这样的事情

路线

Route::get('events',['as'=>'events','uses'=>'ControllerName@index']);
Route::get('event/{id}',['as'=>'event.detail','uses'=>'ControllerName@show']);

调节器

public function index()
{
    $data['events'] = Event::orderBy('created_at','desc')->get(); 
    return view('tests.index',$data);
}

public function show($id)
{
  $data['eventDetail'] = Event::where('id',$id)->first();
  return view('tests.show',$data);
}

索引视图

@foreach ($events as $event)
  <div class="event">
    <h4 class="eventtitle text-j4y-dark">{{ $event->eventtitel }}</h4>
   <<CODE HERE MORE CODE >>
    <a href="{{route('event.detail',['id'=>$event->id]) }}">Mehr lesen »</a>
  </div>
@endforeach

活动详情视图

<div class="event">
  <h4 class="eventtitle">{{ $eventDetail->eventtite l}}</h4>
  <div class="date"><i class="far fa-calendar-alt"></i>{{ date("d.m.Y", strtotime($eventDetail->datum)) }}</div>
  <p class="eventtext" id="eventtext">{{ $eventDetail->eventbeschreibung }}</p>
</div>

希望这可以帮助

© www.soinside.com 2019 - 2024. All rights reserved.