【发布时间】:2020-12-11 19:24:28
【问题描述】:
我对 laravel 模型关系有疑问。代码如下:
路线:
Route::get('/customers','Back\CustomerController@index')->name('admin.customer');
模型\Customer.php:
class Customer extends Model{
public function getData(){
return $this->hasMany('App\Models\Datapanel','name','name');
}
}
模型\Datapanel.php:
class Datapanel extends Model{
//
}
客户控制器.php:
class CustomerController extends Controller{
public function index(){
$customers=Customer::orderBy('created_at','desc')->get();
return view('back.customer',compact('customers'));
}
}
customer.blade.php:
@foreach ($customers as $customer)
<div class="tab-pane" id="{{$customer->name}}" role="tabpanel">
{{$customer->getData}} //this line working and calling all data of the same name, but in array format.
{{$customer->getData->comment}} //this line is what I want, but it doesn't work and gives the error Property [comment] does not exist on this collection instance
{{$customer->getData->first()->comment}} //this line working, but calling first data and i want all the data of the same name.
{{$customer->getData->all()->comment}} //I wish there was something like but it donesn't work.
</div>
@endforeach
【问题讨论】:
标签: php laravel relationship