将变量传递给视图,而不是在laravel 5.1中工作

时间:2016-02-29 12:37:34

标签: php laravel view controller laravel-5.1

我是laravel的新手,我想通过with传递我的变量来查看 这是我的控制器,例如:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Prices ;

class PricesController extends Controller
{

        function getIndex()
        {
            $prices =   Prices::orderBy( 'id' , 'desc' )-> paginate(2) ;

            return view('home')->nest('content' , 'price', compact('prices')  )   ->with( [ 'title' => 'prices page' , 'message' => 'my message text ' ]) ;

        }
}

这是我的master.blade.php

<!doctype html>
<html lang="fa">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>    {{ $title  }}</title>

    <link rel="stylesheet" href="{{ url()  }}/css/bootstrap.css">
    <link rel="stylesheet" href="{{ url()  }}/css/style.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>

    <![endif]-->
</head>
<body>

<div class="container">
    @yield("main")
</div><!-- container -->



<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->

<script src="{{ url()  }}/js/bootstrap.min.js"></script>
<script src="{{ url()  }}/js/dropdown.js"></script>
<script src="{{ url()  }}/js/collapse.js"></script>
<script src="{{ url()  }}/js/transition.js"></script>
<script src="{{ url()  }}/js/tab.js"></script>
</body>
</html>

这是home.blade.php

@extends('master')

@section('main')
    @include('top_menu')

    <div class="row">
        <section class="col-sm-12 col-md-12   content">

            <div class="row">
                <section class="col-md-12">
                    <div class="in">
                        {!! $content !!}
                    </div>
                </section>
            </div>

        </section>
    </div>


@stop

这是我的price.blade.php

@if(   empty($prices)     )
    <div class="alert alert-warning">
        No result
    </div>
@else


    @if( !   isset(  $message)     )
            No message
    @else
        {{ $message  }}

        @endif

    <table class="table table-striped">
        <tr>
            <th>name</th>
            <th>yeat</th>
            <th>qnty </th>
        </tr>

    @foreach( $prices as $price )
        <tr>
            <td>  {{$price->product}} </td>
            <td>  {{$price->yaer}} </td>
            <td>  {{$price->quantity}} </td>
        </tr>
    @endforeach
    </table>

    <center>    {!! $prices->render() !!} </center>


@endif
<{1}} title的输出<title> {{ $title }}</title>中的

传递良好,它会在页面标题中显示: 价格页面 ,但在master.balde.php的这一部分中:

price.blade.php

输出是:

  

没有maessage

我可以访问@if( ! isset( $message) ) No message @else {{ $message }} @endif 中的$message$title,但home.blade.php

中没有

我该如何解决?

1 个答案:

答案 0 :(得分:1)

单词中出现错误 - maessage,也许您发送message,但尝试使用maessage。请检查一下。

更新

您的代码现在看起来很好。但是,请查看@if (empty($prices))部分。如果(empty($prices)返回true,您的$message部分代码将永远不会被执行。可能这就是为什么你没有看到my message text

更新2:

return view('home')
    ->nest('content', 'price', array(
        'prices' => $prices,
        'title' => 'prices page',
        'message' => 'my message text'
        ))
    ->with([ 'title' => 'prices page', 'message' => 'my message text']);