Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我使用的是Laravel5.8,无法将数据插入mysql数据库_Php_Mysql_Laravel_Eloquent_Mamp - Fatal编程技术网

Php 我使用的是Laravel5.8,无法将数据插入mysql数据库

Php 我使用的是Laravel5.8,无法将数据插入mysql数据库,php,mysql,laravel,eloquent,mamp,Php,Mysql,Laravel,Eloquent,Mamp,我正在使用Laravel5.8并学习CRUD。我有2个表,用户表和地址表。我想将用户\u id连接到 地址表。但是我的代码不起作用。我无法插入数据 我已经迁移了2个表,并且能够添加列。 我插入了一个用户的数据。所以表中有一个用户的信息 我正在本地服务器中使用MAMP和mysql服务器 1.App/Address.php protected $fillable = [ 'name' ];// 2.App/User.php public function address(){ r

我正在使用Laravel5.8并学习CRUD。我有2个表,用户表和地址表。我想将用户\u id连接到

地址表。但是我的代码不起作用。我无法插入数据

我已经迁移了2个表,并且能够添加列。 我插入了一个用户的数据。所以表中有一个用户的信息

我正在本地服务器中使用MAMP和mysql服务器

1.App/Address.php

protected $fillable = [
     'name'
];//
2.App/User.php

public function address(){
    return $this->hasOne('App\Address', 'foreign_key');
}
public function address(){
    return $this->hasOne('App\Address', 'user_id','id');
}
3.用户稳定

Schema::create('users', function (Blueprint $table) {
        $table->Increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
4.地址表

Schema::create('addresses', function (Blueprint $table) {
        $table->Increments('id');
        $table->integer('user_id')->unsigned()->nullable();
        $table->string('name');
        //$table->timestamps();
    });
Schema::create('addresses', function (Blueprint $table) {
        $table->Increments('id');
        $table->integer('user_id')->unsigned()->nullable();
        $table->string('name');

        $table->foreign('user_id')->references('id')->on('user'); // this line is the forgein key.
    });
5.routes/web.php

use App\User;
use App\Address;
Route::get('/', function () {
return view('welcome');
});

Route::get('/insert', function(){
$user = User::findOrFail(1);
$address = new Addresses(['name'=>'1234 Houston av NY NY 234']);
$user->address()->save($address);
});
日志文件上没有显示任何内容,因此我无法找出它不起作用的原因。

在您的地址表中

更新您的routes/web.php,如下所示

Route::get('/insert', function(){
    $user = User::findOrFail(1);
    $address = new Addresses();
    $address->name = '1234 Houston av NY NY 234';
    $address->user_id = $user->id;
    $address->save();
});
在地址表中

更新您的routes/web.php,如下所示

Route::get('/insert', function(){
    $user = User::findOrFail(1);
    $address = new Addresses();
    $address->name = '1234 Houston av NY NY 234';
    $address->user_id = $user->id;
    $address->save();
});

用户模型中的关系无效。您应该更正此项以处理关系

2.App/User.php

public function address(){
    return $this->hasOne('App\Address', 'foreign_key');
}
public function address(){
    return $this->hasOne('App\Address', 'user_id','id');
}
另一种将您的地址分配给用户的方法—替代方法和提示:

$address = new Addresses(['name'=>'1234 Houston av NY NY 234', 'user_id',$user->id]);
$adresss->save();
我认为您还应该将关系添加到数据库迁移中。在你的代码中你没有

4.地址表

Schema::create('addresses', function (Blueprint $table) {
        $table->Increments('id');
        $table->integer('user_id')->unsigned()->nullable();
        $table->string('name');
        //$table->timestamps();
    });
Schema::create('addresses', function (Blueprint $table) {
        $table->Increments('id');
        $table->integer('user_id')->unsigned()->nullable();
        $table->string('name');

        $table->foreign('user_id')->references('id')->on('user'); // this line is the forgein key.
    });

用户模型中的关系无效。您应该更正此项以处理关系

2.App/User.php

public function address(){
    return $this->hasOne('App\Address', 'foreign_key');
}
public function address(){
    return $this->hasOne('App\Address', 'user_id','id');
}
另一种将您的地址分配给用户的方法—替代方法和提示:

$address = new Addresses(['name'=>'1234 Houston av NY NY 234', 'user_id',$user->id]);
$adresss->save();
我认为您还应该将关系添加到数据库迁移中。在你的代码中你没有

4.地址表

Schema::create('addresses', function (Blueprint $table) {
        $table->Increments('id');
        $table->integer('user_id')->unsigned()->nullable();
        $table->string('name');
        //$table->timestamps();
    });
Schema::create('addresses', function (Blueprint $table) {
        $table->Increments('id');
        $table->integer('user_id')->unsigned()->nullable();
        $table->string('name');

        $table->foreign('user_id')->references('id')->on('user'); // this line is the forgein key.
    });

在这里,我添加了将数据逐步插入数据库的代码

创建用于创建post表的迁移

php artisan make:migration create_post_table --create=post
在迁移中添加标题和内容列

public function up()
    {
        Schema::create('post', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->longText('content');
            $table->timestamps();
        });
    }

在视图目录中创建帖子文件夹 在post文件夹中,创建刀片文件create.blade.php

<form method="POST" action="{{ route('post.store') }}">
    @csrf

   Title:  <input type="text" name="title" placeholder="Title" required>

   <br>

    Content: <textarea name="content" placeholder="Content" required></textarea>

    <button type="submit">Save</button>

</form>
控制器中的代码

php artisan make:controller PostController --resource
use App\Post;

    public function create()
    {
        return view('post.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required',
            'content' => 'required',
        ]);

        $post = $request->all();

        Post::create($post);

        return redirect('/post');
    }

在路由文件web.php中

//routes for post
Route::resource('post', 'PostController');
打开邮寄表格

http://example.test/post/create 

输入标题和内容,然后单击保存按钮

在这里,我添加代码,以便逐步将数据插入数据库

创建用于创建post表的迁移

php artisan make:migration create_post_table --create=post
在迁移中添加标题和内容列

public function up()
    {
        Schema::create('post', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->longText('content');
            $table->timestamps();
        });
    }

在视图目录中创建帖子文件夹 在post文件夹中,创建刀片文件create.blade.php

<form method="POST" action="{{ route('post.store') }}">
    @csrf

   Title:  <input type="text" name="title" placeholder="Title" required>

   <br>

    Content: <textarea name="content" placeholder="Content" required></textarea>

    <button type="submit">Save</button>

</form>
控制器中的代码

php artisan make:controller PostController --resource
use App\Post;

    public function create()
    {
        return view('post.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required',
            'content' => 'required',
        ]);

        $post = $request->all();

        Post::create($post);

        return redirect('/post');
    }

在路由文件web.php中

//routes for post
Route::resource('post', 'PostController');
打开邮寄表格

http://example.test/post/create 

输入标题和内容,然后单击保存按钮

用此代码替换此地址方法返回$this->hasOne'App\address',user\u id';检查清单:1。确保.env配置了正确的数据库凭据2。php artisan key:generate[key is generated]3。mysql正在运行4。php artisan serve运行新地址['name'=>'1234 Houston av NY 234'];用以下代码替换此地址方法返回$this->hasOne'App\address',user_id';检查清单:1。确保.env配置了正确的数据库凭据2。php artisan key:generate[key is generated]3。mysql正在运行4。php artisan serve运行新地址['name'=>'1234 Houston av NY 234'];对不起,它仍然不起作用,而且根据你的建议,像这样学习CRUD有什么用处吗?您认为如何跳过或继续此学习。请在“用户”表中进行检查,是否存在ID为1的用户我已更改上述代码以逐步插入包含完整详细信息的数据step@YoheiUmezu我已经完成了将文章标题和内容插入数据库的上述代码早上好。非常感谢你!我要试试那些!对不起,它仍然不起作用,而且根据你的建议,像这样学习CRUD有什么用处吗?您认为如何跳过或继续此学习。请在“用户”表中进行检查,是否存在ID为1的用户我已更改上述代码以逐步插入包含完整详细信息的数据step@YoheiUmezu我已经完成了将文章标题和内容插入数据库的上述代码早上好。非常感谢你!我要试试那些!谢谢你的回答,但还是不行。谢谢你的回答,但还是不行。