【问题标题】:Weekly calendar with data from model and pivot table laravel包含来自模型和数据透视表 laravel 的数据的周历
【发布时间】:2020-08-25 01:47:52
【问题描述】:

我有两个模型

剧集 id 姓名

 public function programmation() 
    {
        return $this->hasOne(Programmation::class);
    }

    public function channels() 
    {
        return $this->belongsToMany(Channel::class, 'programmation', 'episode_id', 'channel_id');
    }

频道 id 姓名

public function episodes() 
{
    return $this->belongsToMany(Episode::class, 'programmation', 'channel_id', 'episode_id');
}

编程(数据透视表) episode_id 频道 ID 日期

我的控制器

public function index()
    {
        $episodes = Episode::with('channels', 'programmation')->get();
        return view('front.programmations.calendrier', compact('episodes'));
    }

我想要一个带有星期几、按频道过滤的剧集和日期的每周日历页面,就像这样:https://seriesaddict.fr/programmations

最好的方法是什么?用我的控制器中的关系获取我的剧集?在我的剧集模型中创建一个函数?

我现在的看法

<div class="row">
        <div class="col-md-12">

        <ul class="pagination">
            <a href="{{route('programmations.index', Carbon\Carbon::now()->startOfWeek()->subWeek()->format('j/m/Y'))}}"><li class="left-arrow"> </li></a>

          <li><p>Diffusion séries US : <br /><span  class="calendar-week">Semaine du {{ Carbon\Carbon::now()->startOfWeek()->format('j/m/Y')}} au {{ Carbon\Carbon::now()->endOfWeek()->format('j/m/Y') }} </span></p></li>

          <a href="{{route('programmations.index', Carbon\Carbon::now()->next(Carbon\Carbon::MONDAY)->format('j/m/Y'))}}"> <li class="right-arrow"> </li></a>
        </ul>

        </div>
      </div>

我也不知道如何让当前的 carbon 实例显示正确的日期

【问题讨论】:

    标签: laravel calendar relationship


    【解决方案1】:

    我首先将日期变量从您的控制器传递到您的视图。因此,在控制器中使用碳创建所需的变量,并使用 with() 将这些变量传递给刀片。

    简单示例:

    return view('yourview')->with(['episodes' => $episodes, 'now' => Carbon::now()]);

    【讨论】:

    • 但我认为我的问题是 Carbon::now() 不适合例如当我想将页面更改为下周时
    • 那么您应该使日期变量动态化。如果日历中的日期发生更改,您可以使用 javascript 发出新请求并相应地更新视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    相关资源
    最近更新 更多