Laravel:同一变量在不同视图中不起作用

时间:2018-08-30 08:46:19

标签: laravel

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

我的控制器:

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] => * ) ) 

1 个答案:

答案 0 :(得分: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>

希望这会有所帮助