如何使此代码工作以存储在Laravel中.没有错误也没有存储 [英] How to get this code work to store in Laravel. No errors and no storage

查看:67
本文介绍了如何使此代码工作以存储在Laravel中.没有错误也没有存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将名称和IP地址存储到数据库.我通过运行php artisan migration创建了带有适当字段的表"info".

I can't store name and IP address to DB. I created a table 'info' with appropriate fields by running php artisan migrate.

模式

 Schema::create('info', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('ip');
    $table->timestamp('created_at')->nullable();
 });

信息模型

class Info extends Model
  {
   protected $fillable = ['ip', 'name']; 
  } 

也许问题出在我的HomeController中,这些变量是我得到的?

Maybe the problem is in my HomeController where I get those variables?

<?php
 namespace App\Http\Controllers;
 use Illuminate\Foundation\Auth\User as Authenticatable;
 use App\Info;
 use App\Http\Controllers\Controller;
 use Illuminate\Support\Facades\Auth;
 use Request;

class HomeController extends Controller
  {
     public function __construct()
   {
    $this->middleware('auth');
   }

public function store(Request $request) {
    Info::create(['info' => $request->input('info')]);
}


public function index()
  {
    if (Auth::check())
  {
    $name = Auth::user()->name;
    $ip = Request::ip();

  \App\Events\eventIp::dispatch($ip,$name);
  return view('home');
  }
 }
}

我在web.php中的路线

My routes in web.php

Route::post('/home','HomeController@store');

Route::get('/home', 'HomeController@index')->name('home');

});

但是它不起作用.没有错误,也没有数据库中的记录.

But it doesn't work. Gives no errors and no records in DB.

让我觉得这与我的索引功能有关.我在函数索引中得到了信息,也许函数存储区不知道我的意思.

Something make me think that it have to do with my index function. I got info in function index and maybe function store doesn't have a clue what I mean.

推荐答案

控制器动作基本上是一种通常在打开url(将它们连接到路由时)时执行的方法.

A controller action is basically a method that usually gets executed when you open an url (as you connect them to routes).

在您的示例中,您已将两条路线连接到各自的操作:

In your example you have connected two routes to their respective actions:

Route::post('/home','HomeController@store');
Route::get('/home', 'HomeController@index')->name('home');

现在,成功登录后,想象一下,您最终将在Web浏览器中进入URL为http://localhost:8000/home的页面.

Now, when you log in succesfully, imagine that you end up in the page with url http://localhost:8000/home in your web browser.

关键区别在于您用来呼叫路线的方法(您可以大致了解差异

The key difference is the method which you use to call your route (you can get an overview of the differences here), in your case you are using GET method.

生成的动作使用GET方法执行了与/home路由相关联的动作,即HomeController@index动作(或方法).

The resulting action executed it the one associated to /home route with the GET method, that is the HomeController@index action (or method).

store方法虽然在同一HomeController类中,但是除非您执行/home路由,但要执行使用POST方法,否则不会被触发.

The store method, although is in the same HomeController class, doesn't get triggered unless you execute the /home route, but with the POST method.

您可以确认是否在每种方法中都添加了调试消息:

You can confirm that if you put a debug message in each of the methods like this:

class HomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function store(Request $request)
    {
        echo 'I will not be executed';
    }

    public function index()
    {
        echo 'I have been executed';
    }
}

如果要使用GET方法访问/home路线时仅保存信息记录,则可以将保存内容放入索引方法本身,并摆脱存储方法:

If you want to simply save a info record when you visit the /home route with the GET method, you can put the save in the index method itself and get rid of the store method:

class HomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        // You can omit Auth::check() because you are using the auth middleware
        // that exactly does this job.
        Info::create([
            'name' => Auth::user()->name,
            'ip' => Request::ip(),
        ]);

        return view('home');
    }
}

请记住,通过这种方式,您将对该路由进行的每个页面视图都获得一个新的数据库记录(如果继续刷新页面,您应该会看到新记录已添加到数据库中).

Keep in mind that doing in this way you will get a new database record for each page view you make to that route (if you keep refreshing the page, you should see new records being added to database).

使用雄辩模型时,laravel将查找以复数模型名称命名的表(Info模型将尝试使用infos表).

When you use Eloquent Models, laravel will look for a table named after the pluralized model name (Info model will try to use infos table).

但是,您创建了一个名为info的表.要解决此问题,您可以重命名表并使用php artisan migrate:refresh重新运行迁移(它将删除您用于laravel应用的数据库中的所有现有数据)

However you created a table named info. To solve that you can either rename the table and rerun the migration with php artisan migrate:refresh (it will delete all the existing data in the database you are using for your laravel app)

或指定用于该laravel模型的表名:

Or specify the table name to use for that laravel model:

class Info extends Model
{
    protected $table = 'info';
    protected $fillable = ['ip', 'name'];
}

这篇关于如何使此代码工作以存储在Laravel中.没有错误也没有存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆