第一次使用Laravel和PHP。我试图在表 profile 中插入行,其中属性uID是引用 user 表上的uID的外键,但出现错误。如何仅插入配置文件的summary属性并自动设置uID(外键)。我可以毫无问题地插入具有uID的用户。这是我的模型和控制器文件。谢谢!
用户模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class user extends Model
{
// specify which attributes can be filled out during registration
public $timestamps = false;
protected $fillable=['firstname','lastname','email','password',];
public function profile(){
return $this->hasOne(profile::class);
}
}
个人资料模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class profile extends Model
{
//
public $timestamps = false;
protected $fillable = ['summary',];
public function user(){
return $this->belongsTo(user::class);
}
}
个人资料迁移
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProfilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// create profile table
Schema::create('profiles', function (Blueprint $table) {
$table->increments('pID');
$table->timestamp('created_at')->useCurrent();
$table->string('summary')->default('');
$table->unsignedInteger('uID');
$table->foreign('uID')->references('uID')->on('users')->onDelete('cascade');
});
}
}
配置文件控制器
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\profile;
class ProfileController extends Controller
{
public function create()
{
//
return view('profile.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// used to store user profile after validation
$this->validate($request,[
'summary' => 'required'
]);
$profile = new profile([
'summary' => $request->get('summary'),
'uID' => $request->user()->uID
]);
return redirect()->route('profile.create')->with('success','Profile created');
}
}
答案 0 :(得分:1)
欢迎来到Laravel:)
在您的配置文件上,需要在用户方法模型上定义外键。
public function user(){
return $this->belongsTo(user::class, 'uID');
}
答案 1 :(得分:0)
好的,有几件事可能需要解决:
使用resource "aws_launch_configuration" "instances" {
image_id = "ami-0fad7824ed21125b1"
instance_type = "${var.instance_type}"
security_groups = ["${aws_security_group.security_group_ec2.id}"]
key_name = "${var.key_pair_name}"
user_data = "${data.template_file.user_data.rendered}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "scaling_group" {
launch_configuration = "${aws_launch_configuration.instances.id}"
availability_zones = ["${var.availability_zones_names}"]
load_balancers = ["${var.elb_id}"]
health_check_type = "ELB"
min_size = "${var.min_size}"
max_size = "${var.max_size}"
tags = {
key = "Name"
value = "terraformUpAndRunning-${var.cluster_name}"
propagate_at_launch = true
}
wait_for_capacity_timeout = "5m"
}
声明您的课程。它是capitalised letter
而不是Profile
。也要避免使用复数形式。有关profile
;
您在PSR-x
方面偏离了laravel方式。因此,您需要在关系中指定与之关联的键:
另外,请注意我如何从用户->用户更改您的方法名称
foreign keys
以及您的个人资料关系:
public function users()
{
return $this->belongsTo(user::class, 'id', 'uID');
}
答案 2 :(得分:0)
嗨,将您的数据库放在这里,并为模型添加外键