我怎样才能将它从一张桌子存储到另一张桌子?
Posted
技术标签:
【中文标题】我怎样才能将它从一张桌子存储到另一张桌子?【英文标题】:How can i store that from one table to another? 【发布时间】:2021-10-10 18:38:08 【问题描述】:嗨,我正在使用 Laravel 7,并且在将表的 id 存储在另一个表上时处理了一些问题。我有一个名为 customers 的表和另一个名为 contacts 的表,其中一个客户可能有很多联系人,而一个联系人只属于一个客户。
客户.php:
public function contacts()
return $this->hasMany(Contact::class);
这是我建立关系的方法联系人。
以下是我在 Contact.php 中的客户方法:
public function customer()
return $this->belongsTo(Customer::class,'id_account','id');
在我的联系人表中,我有一个名为 id_account 的列,我希望在那里存储客户 ID。所以我试图通过执行以下操作来实现这一点:
namespace App\Http\Controllers\Backend;
use Illuminate\Http\Request;
use App\Model\Contact;
use App\Model\Customer;
use App\Http\Controllers\Controller;
use Auth;
use Session;
class ContactController extends Controller
//
public function view()
$allData = Contact::all();
return view('backend.contact.view-contact', compact('allData'));
public function add()
$customers = Customer::all();
$contact['customers'] = Customer::all();
return view('backend.contact.add-contact', compact('customers'));
public function store(Request $request)
$customers = Customer::all();
$contact = new Contact();
$contact->name = $request->name;
$contact->lastname = $request->lastname;
$contact->id_account = Customer::contacts('id');
$contact->idOwner = Auth::user()->id;
$contact->position = $request->position;
$contact->mobile_no = $request->mobile_no;
$contact->email = $request->email;
$contact->save();
Session::flash('success');
return redirect()->route('contact.view', compact('contact','customers'));
我尝试了几种方法,例如:
$contact->id_account = Customer::contacts('id');
或者
$contact->id_account = Customer::get('id');
$contact->id_account = $request->$customers->id;
但是这些都不起作用,我是 Laravel 的新手,我真的在为这个问题苦苦挣扎。不同类型的错误是:
Invalid datetime format: 1366 Incorrect integer value: '["id":1]' for column `axis`.`contacts`.`id_account` at row 1 (SQL: insert into `contacts` (`name`, `lastname`, `id_account`, `idOwner`, `position`, `mobile_no`, `email`, `updated_at`, `created_at`) values (Mateo, Ghidini, ["id":1], 1, pasante, 12345678, mateo.ghidini@hotmail.com, 2021-08-05 18:31:12, 2021-08-05 18:31:12))
另一个错误:
Trying to get property 'id' of non-object
最后:
Non-static method App\Model\Customer::contacts() should not be called statically
<div class="form-group col-md-6">
<label for="account">Cuenta</label>
<select name="account" id="" class="form-control">
<option value="">Seleccione el holding de la empresa</option>
@foreach ($customers as $key => $customer)
<option value="$customer->id">$customer->holding</option>
@endforeach
</select>
</div>
【问题讨论】:
【参考方案1】:当您尝试保存时,您需要确切知道要在该行中存储哪个Customer
的 ID。因此,您可以在表单上显示<select ...>
并让用户选择Customer
在resources/views/backend/contact/add-contact.blade.php
<!-- I assume you have a form and other inputs here -->
<form>
<!-- add a select-option for Customer -->
<select name="customer_id">
@foreach($customers as $customer)
<option value=" $customer->id ">
$customer->name
</option>
@endforeach
</select>
</form>
现在您可以从控制器中使用它
public function store(Request $request)
$customers = Customer::all();
$contact = new Contact();
$contact->name = $request->name;
$contact->lastname = $request->lastname;
$contact->id_account = $request->customer_id; // here we used customer_id from form
$contact->idOwner = Auth::user()->id;
$contact->position = $request->position;
$contact->mobile_no = $request->mobile_no;
$contact->email = $request->email;
$contact->save();
Session::flash('success', 'Contact saved to selected customer.');
return redirect()->route('contact.view', compact('contact','customers'));
【讨论】:
我确实有你展示的表格,使用完全相同的 select 和 foreach,但是当我尝试做 $contact->id_account = $request->customer_id 我得到'未定义的变量客户'我'将编辑问题以显示给您。以上是关于我怎样才能将它从一张桌子存储到另一张桌子?的主要内容,如果未能解决你的问题,请参考以下文章