【发布时间】:2021-08-14 08:06:23
【问题描述】:
我正在创建一个 laravel 应用程序,用户在其中登录并可以创建客户端并填写链接到客户端的申请表。
我正在尝试与许多用户建立关系,每个用户都有许多客户,这些客户有一个申请人。但我收到以下错误:
我对出了什么问题以及命名函数感到困惑。
客户端控制器:
public function createApplicant(){
$data = request()->validate([
'name' => 'required',
'dob' => 'required',
'age' => 'required',
'gender' => 'required',
'ethnicity' => 'required',
'country' => 'required',
'interpreter' => '',
'language' => 'required',
'homeAddress' => 'required',
'job' => 'required',
'workAddress' => 'required',
'email' => 'required',
'phone' => 'required',
'contact' => 'required',
'extra' => 'required',
]);
auth()->user()->clients()->applicant()->create([
'name'=>$data['name'],
'dob'=>$data['dob'],
'age'=>$data['age'],
'gender'=>$data['gender'],
'ethnicity'=>$data['ethnicity'],
'country'=>$data['country'],
'interpreter'=>$data['interpreter'],
'language'=>$data['language'],
'homeAddress'=>$data['homeAddress'],
'job'=>$data['job'],
'workAddress'=>$data['workAddress'],
'email'=>$data['email'],
'phone'=>$data['phone'],
'contact'=>$data['contact'],
'extra'=>$data['extra'],
]);
dd($data);
}
用户模型:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function clients(){
return $this->hasMany(Client::class);
}
}
客户端模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
use HasFactory;
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email'
];
public function applicant(){
return $this->hasOne(Applicant::class);
}
}
申请人型号:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Applicant extends Model
{
use HasFactory;
protected $fillable = [
'fullName',
'date_of_birth',
'age',
'gender',
'ethnicity',
'country',
'interpreter',
'language',
'homeAddress',
'job',
'workAddress',
'email',
'phone',
'contact',
'extra'
];
}
申请人表
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Applicants extends Migration
{
public function up()
{
Schema::create('applicants', function (Blueprint $table) {
$table->id();
$table->string('fullName');
$table->date('date_of_birth');
$table->string('age');
$table->string('gender');
$table->string('ethnicity');
$table->string('country');
$table->boolean('interpreter');
$table->string('language');
$table->string('homeAddress');
$table->string('job');
$table->string('workAddress');
$table->string('email')->unique();
$table->string('phone');
$table->string('contact');
$table->string('extra');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('applicants');
}
}
客户表
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Clients extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('clients', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('clients');
}
}
任何帮助将不胜感激。
谢谢!
【问题讨论】:
-
Laravel 如何选择合适的客户端?用户
hasMany()clients - Laravel 对此感到困惑。 -
什么意思?抱歉,我对 Laravel 有点陌生