如何将变量传递到另一个页面/视图?

时间:2016-11-08 11:42:02

标签: php variables laravel-5.3

我想有以下场景。用户选择两个日期时段,在该时间段内从数据库中选择联系人。

然后在下一页上,我想显示传递的联系人,还有一个文本区域字段,用户可以在其中键入消息。

问题是,当下一页加载$contacts变量为空时。那么如何将变量传递到两个页面呢?

以下是Select Time Period

的代码
<form method="POST" action="selectContacts">
    {{ csrf_field() }}
    <div class="row">
        <div class="col-lg-3 col-sm-3 col-xs-6">
            <div class="form-group{{ $errors->has('date') ? ' has-error' : '' }}">
                <label for="Event Date">Start Date</label>
                <input type ='hidden' name='email' value='{{$email}}'>
                <input type ='hidden' name='ip' value='{{$ip}}'>
                <input name="start" type="text" class="form-control" id='fromperiod'  placeholder="Date" required 
                @if ($errors->has('date'))
                    <span class="help-block">
                        <strong>{{ $errors->first('date') }}</strong>
                    </span>
                @endif
            </div>  
        </div>
    </div>
    <div class="row">
        <div class="col-lg-3 col-sm-3 col-xs-6">
            <div class="form-group{{ $errors->has('date') ? ' has-error' : '' }}">
                <label for="Event Date">End Date</label>
                <input name="stop" type="text" class="form-control" id='toperiod'  placeholder="Date" required 
                @if ($errors->has('date'))
                    <span class="help-block">
                        <strong>{{ $errors->first('date') }}</strong>
                    </span>
                @endif
            </div>  
        </div>
    </div>
    <div class="row">
        <div class="col-lg-12 col-sm-12 col-xs-12">
            <input type="submit" class="btn btn-primary pull-right" value="Select">
            <a href="sendsms" class="btn btn-default pull-left">
                <i class="fa fa-arrow-left" aria-hidden="true"></i> Back
            </a>
        </div>
    </div>
</form>

然后是处理selectContacts路由的控制器。

public function SelectContacts(Request $request)
{
    $Startdate=$request->start;
    $Stopdate=$request->stop;
    validator = Validator::make($request->all(), [
        'start' => 'required|min:10',
        'stop' => 'required|min:10'
    ]);

    if ($validator->fails()) {
        return redirect('/sendsms')
               ->withErrors($validator)
               ->withInput();
    }
    $sdate=date_create("$Startdate");
    $start = date_format($sdate,"Y/m/d H:i:s");

    $date=date_create("$Stopdate");
    $stop = date_format($date,"Y/m/d H:i:s");

    $contacts = DB::table('Payment')
                    ->whereBetween('time_paid', [$start, $stop])
                    ->paginate(5); 

    //next page 
    return view('BulkSMS.send', 'contacts'=>$contacts]);
}

然后最后处理文本区域的视图和联系人的显示:

<form class="form-horizontal" role="form" method="post" action="/fun/sendbulk"> 
    {{ csrf_field() }}
    <div class="form-group">
        <label for="message" class="col-sm-1 control-label">Message</label>
        <div class="col-sm-6">
            <textarea class="form-control" rows="4" name="message" required="Please Type Your Message Here" placeholder="Message"></textarea>
        </div>
        <input type ='hidden' name='email' value='{{$email}}'>
        <input type ='hidden' name='ip' value='{{$ip}}'>
    </div>
    <div class="form-group">
        <div class="col-sm-6 col-sm-offset-1">
            <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary pull-right">
            <a href="sendsms" class="btn btn-default pull-left">
                <i class="fa fa-arrow-left" aria-hidden="true"></i> Cancel
            </a>
        </div>
    </div>  
</form>

<div class="row">
    <div class="col-sm-4 col-sm-offset-2">
        <table class="table table-hover">
            <thead>                    
                <th>ID</th><th>Name</th> <th>Mobile Number</th> <th>Remove</th>
            </thead>
            <tbody>
                @foreach($contacts as $contact)
                    <tr>
                        <td> {{$contact->id }} </td>
                        <td> {{$contact->name}}</td>
                        <td> {{$contact->msisdn}}</td>
                        <td>
                            <form action= '' method='post'>
                                {{csrf_field()}}
                                <input type="radio" name="remove" value="other">
<!-- <input type='submit' name='submit' value='submit'> -->
                            </form>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
        <div class="pull-right">{{ $contacts->links() }}</div>
    </div>
</div>

最后,处理/fun/sendbulk路由的控制器。

public function sendsms(Request $request){
  $message=$request->message;
  $validator = Validator::make($request->all(), [
      'message' => 'required|max:160',]);
  if ($validator->fails()) {
      return redirect('/selectContacts')
             ->withErrors($validator)
          ->withInput();} 
   $mob_numbers = implode(", " , $contacts);
   $serviceArguments = array(
        "mobilenumber" => $mobileNum,
        "message" => $message
        );
  $client = new SoapClient("http://#smsws?wsdl");

  $result = $client->process($serviceArguments)  
  }

请咨询

3 个答案:

答案 0 :(得分:1)

在smssend函数中将此添加到顶部。

$start=$request->start;
$stop=$request->stop;

$contacts = DB::table('Payment')
  ->whereBetween('time_paid', [$start, $stop])
  ->paginate(5); 
编辑:我看到你需要做更多的事情: 在选择联系人路线:

return view('BulkSMS.send', 
    'contacts'=>$contacts,
     'start' => $start,
     'stop' => $stop
);

在联系人视图中:

<input type ='hidden' name='start' value='{{$start}}'>
<input type ='hidden' name='stop' value='{{$stop}}'>

答案 1 :(得分:0)

我注意到你这样做了:

//next page 
return view('BulkSMS.send', 
'contacts'=>$contacts,
]);

看来你错过了开口方括号。我会改变它:

//next page 
return view('BulkSMS.send', compact('contacts');

答案 2 :(得分:0)

方式:1

return view('YourViewPage', ['VariableName' => $value]);

方式:2

return view('YourViewPage')with->('VariableName' => $value );

方式:3

$VariableName = "Your data";
return view('YourViewPage', compact('VariableName'));

在查看页面调试

{{dd($ VariableName)}}