我试图在基于客户订单的模型上转换为模型绑定。我的路线:
Route::model('order', 'App\Models\Order');
Route::resource('orders', 'OrderController');
这允许我通过我的控制器提取订单进行编辑(也可以抓取状态来填充表格并传递登录用户):
public function index(Order $order)
{
$orders = $order->get();
return view('orders.index', compact('orders'));
}
我的orders.index会正确显示$ order-> id但是当我尝试循环通过hasMany关系连接的Actions时,什么都没有显示。或者我尝试通过user_id显示属于User的$ order-> user-> firstname。
@foreach( $order->actions as $action )
{{ $action->type->type }}
@endforeach
来自我的动作模型:
public function order()
{
return $this->belongsTo('\App\Models\Order', 'order_id');
}
来自我的订单模式:
public function actions()
{
return $this->hasMany('\App\Models\Action', 'order_id');
}
以下是订单转储的摘录:
`Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Models\Order Object
(
[table:protected] => orders
[timestamps] => 1
[dates:protected] => Array
(
[0] => deleted_at
)
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[attributes:protected] => Array
(
[id] => 1
[created_at] => 2015-03-16 23:42:45
[updated_at] => 2015-03-19 04:37:53
[deleted_at] =>
[user_id] => 16
[status_id] => 5
[address_id] => 5
[datetime_pickup_actual] =>
[datetime_delivery_actual] =>
[datetime_pickup_requested] => 2015-03-20 17:00:00
[datetime_delivery_requested] => 2015-03-21 17:00:00
[hold] => 0
[weight] => 20
)
[original:protected] => Array
(
[id] => 1
[created_at] => 2015-03-16 23:42:45
[updated_at] => 2015-03-19 04:37:53
[deleted_at] =>
[user_id] => 16
[status_id] => 5
[address_id] => 5
[datetime_pickup_actual] =>
[datetime_delivery_actual] =>
[datetime_pickup_requested] => 2015-03-20 17:00:00
[datetime_delivery_requested] => 2015-03-21 17:00:00
[hold] => 0
[weight] => 20
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
[forceDeleting:protected] =>
)`
答案 0 :(得分:0)
在控制器中压缩orders
时,您应该在orders
模板中使用Blade
:
@foreach( $orders->actions as $action )
{{ $action->type->type }}
@endforeach
您还应该知道控制器中Order
方法中的index
与路径模型绑定没有任何共同之处。对于index
,它根本不会被使用。
对于其他方法(显示/编辑/删除),它不会起作用,因为您使用错误的名称进行绑定。它应该是:
Route::model('orders', 'App\Models\Order');
而不是
Route::model('order', 'App\Models\Order');
答案 1 :(得分:0)
型号名称(MoneyEntry)
更改自
apply{
//block
}
到
Route::resource('moneyentries', 'MoneyEntryController');
有把戏。