【发布时间】:2015-06-30 21:49:08
【问题描述】:
我正在尝试将查询生成器传递给查看,并且我想打印它。查询生成器不返回null,但我无法传递或打印它。
控制器
public function search() {
$option1 = Request::get( 'option1' );
$option2 = Request::get( 'option2' );
$condition = Request::get( 'condition' );
$date_option = Request::get( 'dateOption' );
$option1_value = Request::get( 'option1_value' );
$option2_value = Request::get( 'option2_value' );
$fromDate = Request::get( 'fromDate' );
$toDate = Request::get( 'toDate' );
if ( $condition == 'no' ) {
$vehicles = Vehicle::with( 'brand', 'section', 'representive', 'buyer', 'seller', 'buyingPaymentType', 'sellingPaymentType' )->where( $option1, $option1_value )->get();
//return $vehicle;
}
if ( $condition == 'or' ) {
$vehicles = Vehicle::with( 'brand', 'section', 'representive', 'buyer', 'seller', 'buyingPaymentType', 'sellingPaymentType' )->where( $option1, $option1_value )->orWhere( $option2, $option2_value )->get();
}
if ( $condition == 'and' ) {
$vehicles = Vehicle::with( 'brand', 'section', 'representive', 'buyer', 'seller', 'buyingPaymentType', 'sellingPaymentType' )->where( $option1, $option1_value )->where( $option2, $option2_value )->get();
}
return redirect()->back()->with( 'vehicles', $vehicles );
//return $vehicles;
}
查看
@if(isset($vehicles))
@foreach($vehicles as $vehicle)
<td>{{ $vehicle->id }}</td>
@endforeach
@endif
我做错了什么?任何帮助将不胜感激。
【问题讨论】:
-
我很难看到你想要做什么。如果您正在尝试构建搜索表单并显示结果,请改用 GET 路由,这样您就不必重定向,可以将 URL 加入书签等。
-
@MartinBean,OP 正在尝试通过重定向传递其他数据。这可能是也可能不是表单提交数据的一部分。假设您正在登录一个用户,如果成功,您正在将除
user对象(例如,与该user关联的vehicle对象)之外的一些额外数据传递到下一页。 -
@ArmanH 它在我看来就像一个搜索表单,根据传递的选项构建一个查询。如果它是一个登录表单,忽略它的字段太多,为什么 OP 不直接根据经过身份验证的用户 ID 查询数据库?
-
这个特定示例的细节与如何通过重定向将数据从控制器传递到控制器这一更广泛的问题无关紧要。为什么 OP 做 A 而不是 B,由他来回答。
标签: php laravel laravel-5 url-redirection