Я сделал действительно простую функцию в Laravel 6, которая использует with ():
$forms = Form::with('itemsForms')->get();
return response()->json([
'code' => 200,
'data' => $forms
]);
Ниже приведены отношения между Form и ItemsForm:
//Form
protected $fillable = [
'title',
'subtitle',
'text',
'name',
'email',
'phone_number',
'address',
'board',
'date',
'file',
'purchasable',
'payment_for',
'invoice_amount',
];
protected $visible = [
'title',
'subtitle',
'text',
'name',
'email',
'phone_number',
'address',
'board',
'date',
'file',
'purchasable',
'payment_for',
'invoice_amount',
];
public function itemsForms()
{
return $this->hasMany('App\ItemsForm');
}
//ItemsForm
protected $fillable = [
'item_id', 'form_id'
];
public function form()
{
return $this->belongsTo('App\Form', 'form_id');
}
Дело в том, что он не получает никаких данных из ItemsForm. Вот кое-что из того, что я пробовал:
- Я пытался изменить параметр в
with
на другие похожие имена, но в каждом случае я получал ошибку «отношения не найдены» или что-то в этом роде. Когда я использую itemsForms
, я не получаю ошибки. - Я попытался отладить его, включив журнал запросов. Вот что я получил:
array:2 [
0 => array:3 [
"query" => "select * from `forms`"
"bindings" => []
"time" => 5.77
]
1 => array:3 [
"query" => "select * from `items_forms` where `items_forms`.`form_id` in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17)"
"bindings" => []
"time" => 1.03
]
]
- Я попытался получить данные
ItemsForm
, и он получил их без проблем (ItemsForm::all()
).
Любая идея о том, что может быть причиной этого?
Edit : схема для ItemsForm следующая:
Schema::create('items_forms', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('item_id');
$table->unsignedBigInteger('form_id');
$table->foreign('item_id')
->references('id')->on('items')
->onDelete('no action')
->onUpdate('no action');
$table->foreign('form_id')
->references('id')->on('forms')
->onDelete('no action')
->onUpdate('no action');
});