大家好我想要从不同的表中获取值,但是相同的数据库并在下拉列表中打印它们,尝试了几种方法,但是我得到了这个错误
调用未定义的方法Illuminate \ Auth \ SessionGuard :: myVehicles()(查看:/home/vagrant/www/martin/resources/views/templates/applications.blade.php)
我想从包含所有车辆的表中获取它们并打印所有车辆,以便用户可以选择数据库中的一辆车......这里是车型
<?php
namespace Martin\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Vehicle extends Authenticatable
{
use Notifiable;
//
use Notifiable;
protected $guard="vehicle";
protected $table="vehicles";
protected $fillable=[
'id',
'vehicle_registration_number',
'vehicle_type',
'vehicle_size',
];
public function user(){
return $this->belongsTo('Martin\Models\User');
}
public function myVehicles(){
$vehicles = Martin\Models\Vehicle::all();
foreach ($vehicles as $vehicle) {
echo $vehicle->vehicle_registration_number."-".$vehicle->vehicle_type."-".$vehicle->vehicle_size.' seaters <br/>';
}
}
}
&#13;
以下是下拉列表我想在applications.blade.php中显示我的数据,以便用户可以选择其中一辆车......
<select name="vehicle_registration_number" class="form-control">
{{Auth::guard('vehicle')->myVehicles()}}
</select>
&#13;
我也可能会问你给选择框分配了什么名称你是否指定了与输入的原始名称相同的名称,即原始名称的文本区域?请回复...如果需要进一步澄清,请准备好......谢谢!
答案 0 :(得分:0)
First, you should use a controller to handle your models, rather than trying to handle a class from the class itself. If so, you need to create static methods.
In whatever controller you are calling that page, you can get all vehicles
public function page()
{
//whatever you are doing here
...
$vehicles = Martin\Models\Vehicle::get();
foreach ($vehicles as $vehicle) {
$vehicle->description = $vehicle->vehicle_registration_number ."-". $vehicle->vehicle_type ."-". $vehicle->vehicle_size .' seaters';
}
return view('view_name', compact('vehicles'));
}
Then in your view you loop through vehicles in your select
<select name="vehicle_registration_number" class="form-control">
<option>Select Vehicle</option><!--selected by default-->
@foreach($vehicles as $vehicle)
<option value="{{ $vehicle->vehicle_registration_number }}">
{{ $vehicle->description }}
</option>
@endforeach
</select>