【问题标题】:Laravel - summing price on my search pageLaravel - 在我的搜索页面上求和价格
【发布时间】:2019-08-07 07:04:03
【问题描述】:

所以几分钟前,我在我的项目中获得了使用whereDate 方法求和价格的帮助,现在我只需要在我的搜索页面上将所有这些价格与所有付款方式相加,所以我的搜索功能是日期,当我输入我想要的日期时,它会显示该日期的帖子,我需要将价格总和放在表格页脚中。让我给你看代码。

这是我的home.blade.php,我的常规价格总和如下:

<tfoot>
                <tr>
                    <th>UKUPAN IZNOS:&nbsp;&nbsp;{{ Auth::user()->posts()->whereDate('created_at','=',$date)->sum('cijena') }}&euro;</th>
                    <th>KARTICA:&nbsp;&nbsp;{{ Auth::user()->posts()->whereDate('created_at','=',$date)->where('placanje', 'Kartica')->sum('cijena')}}&euro;</th>
                    <th>GOTOVINA:&nbsp;&nbsp;{{ Auth::user()->posts()->whereDate('created_at','=',$date)->where('placanje', 'Gotovina')->sum('cijena')}}&euro;</th>
                    <th>VIRMAN:&nbsp;&nbsp;{{ Auth::user()->posts()->whereDate('created_at','=',$date)->where('placanje', 'Virman')->sum('cijena')}}&euro;</th>
                    <th>NK:&nbsp;&nbsp;{{ Auth::user()->posts()->whereDate('created_at','=',$date)->where('placanje', 'NK')->sum('cijena')}}&euro;</th>
                </tr>
</tfoot>

HomeController这样的索引函数:

public function index()
    {
        $date = new Carbon(request('date'));

        $posts = Post::where('user_id', Auth::id())
                ->whereDate('created_at','=',$date)
                ->orderBy('created_at', 'DESC')
                ->paginate(30); //add {{ $posts->links() }} if paginate is enabled
        return view('home', compact('date', $date))->with('posts', $posts);
    }

现在我的搜索页面date range.blade.php是这样的:

<tfoot>
                        <tr>
                            <th>UKUPAN IZNOS:&nbsp;&nbsp;{{ Auth::user()->posts()->whereDate('created_at','=',$date)->sum('cijena') }}&euro;</th>
                            <th>KARTICA:&nbsp;&nbsp;{{ Auth::user()->posts()->whereDate('created_at','=',$date)->where('placanje', 'Kartica')->sum('cijena')}}&euro;</th>
                            <th>GOTOVINA:&nbsp;&nbsp;{{ Auth::user()->posts()->whereDate('created_at','=',$date)->where('placanje', 'Gotovina')->sum('cijena')}}&euro;</th>
                            <th>VIRMAN:&nbsp;&nbsp;{{ Auth::user()->posts()->whereDate('created_at','=',$date)->where('placanje', 'Virman')->sum('cijena')}}&euro;</th>
                            <th>NK:&nbsp;&nbsp;{{ Auth::user()->posts()->whereDate('created_at','=',$date)->where('placanje', 'NK')->sum('cijena')}}&euro;</th>
                        </tr>
</tfoot>

我在DateRangeController 中的搜索和索引功能如下:

public function index()
    {
        $date = new Carbon(request('date'));

        $posts = Post::where('user_id', Auth::id())
                ->whereDate('created_at','=',$date)
                ->orderBy('created_at', 'DESC')
                ->paginate(30);
        return view('daterange', compact('date', $date))->with('posts', $posts);
    }



    public function search(){
        $user_id = auth()->user()->id;
        $user = User::find($user_id);
        return view('daterange')->with('posts', $user->posts);
    }

我在web.php 的路线是这样的:

Route::get('/daterange', 'DateRangeController@index');
Route::get('/daterange', 'DateRangeController@search');
Route::post('/daterange',function(){
    $q = Input::get ( 'q' );
    $post = auth()->user()->posts()->where('datum_preuz','LIKE', '%'.$q.'%')->get();
    if(count($post) > 0)
        return view('daterange')->withDetails($post)->withQuery ( $q );
    else return view ('daterange')->withMessage('Nema rezultata Vaše pretrage. Probajte ponovo!');
});

但它显示错误Undefined variable: date 有什么解决办法吗?

【问题讨论】:

    标签: laravel search sum


    【解决方案1】:

    首先,你可以恢复这个

     $user_id = auth()->user()->id;
     $user = User::find($user_id);
     return view('daterange')->with('posts', $user->posts);
    

    只有这个

     return view('daterange')->with('posts',auth()->user()->posts);
    

    关于你的错误:

    我认为您对紧凑型的使用有误。 Compact 仅接收字符串或变量名称的字符串数组作为参数。所以'date'是正确的,buyt $date 是错误的。

    通常你可以像这样使用 compact

    return view('myview', compact('date')); 
    

    return view('myview', compact(['date', 'anothervar']); 
    

    请试试这个,然后告诉我,:)

    【讨论】:

    • Manuel 很抱歉这个迟到的答案,我试过你告诉我的,但仍然是同样的错误Undefined variable: date
    • 因为 var 不存在。我不确定你是否可以使用 request('date') .... 通常我使用 request()->date 或 $request->get('date') 或 Input::get('date'); .也许您可以在创建后转储 dd($date) 以查看它是否有东西。
    猜你喜欢
    • 1970-01-01
    • 2019-12-18
    • 2023-01-31
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    相关资源
    最近更新 更多