简体   繁体   English

Laravel 5.4-未定义变量:将变量从控制器传递到视图

[英]Laravel 5.4 - Undefined variable: passing variable from controller to view

I'm new to Laravel. 我是Laravel的新手。 I have searched this site but can't find specific help. 我已经搜索了该站点,但是找不到特定的帮助。 I'm trying to pass a value of a variable(which is $Like) from Controller to the blade view, but the browser gives an error of 我正在尝试将Controller的变量(即$ Like)的值传递给刀片视图,但是浏览器给出了一个错误

Undefined variable: Like 未定义变量:喜欢

Here is the part of my view code (dashboard.blade.php): 这是我的视图代码(dashboard.blade.php)的一部分:

@foreach($posts as $post)
                <article class="post" data-postid="{{ $post->id }}">
                    <div class="interaction">
                        <a href="#" class="like">{{$Like }}</a> |
                    </div>
                </article>
 @endforeach

<script>
        var token = '{{ Session:: token() }}';
        var urlLike = '{{ route('like') }}';
</script>

part of my controller(PostController.php) code: 我的控制器(PostController.php)代码的一部分:

public function LikePost(Request $request) // post type
    {

        $post_id = $request['postId']; // postId from script.js
        $is_Like = $request['isLike'] === 'true'; // from script.js ...

        $update = false;
        $post = Post::find($post_id);
        $user = Auth::user();
        $like = $user->likes()->where('post_id', $post_id)->first(); 

        $like->like = $is_Like;
        $like->user_id = $user->id;
        $like->post_id = $post->id;

        $Like = $like ? $like->like == 1 ? 'You like this' : 'Like' : 'Like';
        return redirect()->route('dashboard')->with('Like', $Like);
    }

here is the part of my route code: 这是我的路线代码的一部分:

//for like post
Route::post('/like', [
    'uses'=> 'PostController@LikePost',
    'as'=> 'like'
]);

here is my script.js: 这是我的script.js:

$('.like').on('click', function (event) {
    event.preventDefault();
    postId = event.target.parentNode.parentNode.dataset['postid'];
    // check whether previous element of like/dislike is null or not
    var isLike = event.target.previousElementSibling == null;

    $.ajax({
       method: 'POST',
       url: urlLike,
       data: {
            isLike: isLike,
            postId: postId,
            _token: token
      }
    }).done(function () {
        event.target.innerText = isLike ? event.target.innerText == 'Like' ? 'You like this' : 'Like' : event.target.innerText == 'Dislike' ? 'You dislike this' : 'Dislike';
        if (isLike) {
            event.target.nextElementSibling.innerText = 'Dislike';
        } else {
            event.target.previousElementSibling.innerText = 'Like';
        }
    });
});

I tried in many ways. 我尝试了很多方法。 but every time it shows me an error, 但是每次显示错误时,

undefined variable: Like in dashboard.blade.php 未定义的变量:就像在dashboard.blade.php中一样

Anyone help please..... 任何人都请帮助.....

You are redirecting to a route, which in turn calls its own controller method that passes down variables to dashboard.blade.php 您将重定向到路由,该路由又调用其自己的控制器方法,该方法将变量传递给dashboard.blade.php

When you do redirect()->route('dashboard')->with('Like', $Like) you are essentially flashing some data to the session. 当您进行redirect()->route('dashboard')->with('Like', $Like)时,实际上是在向会话中刷新一些数据。

You need to use session('Like') to access variables in blade when redirecting to a route and flashing variables. 重定向到路由并刷新变量时,需要使用session('Like')访问Blade中的变量。

@foreach($posts as $post)
                <article class="post" data-postid="{{ $post->id }}">
                    <div class="interaction">
                    @if (session('Like'))
                        <div class="alert alert-success">
                            <a href="#" class="like">{{ session('Like') }}</a> |
                        </div>
                    @endif

                    </div>
                </article>
 @endforeach

<script>
        var token = '{{ Session:: token() }}';
        var urlLike = '{{ route('like') }}';
</script>

Read more here https://laravel.com/docs/5.4/responses#redirecting-with-flashed-session-data 在此处阅读更多内容https://laravel.com/docs/5.4/responses#redirecting-with-flashed-session-data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM