简体   繁体   English

model 如何与 laravel 中的 controller 一起工作?

[英]how model work with controller in laravel?

I created this code in a controller but it does not work.我在 controller 中创建了此代码,但它不起作用。 My view is register member with username and password.我的观点是使用用户名和密码注册会员。 I want to add it on mysql by model not query builder.我想通过 model 而不是查询生成器将它添加到 mysql 上。 I am using laravel framework我正在使用 laravel 框架

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\myModel;
// use DB;

class myController extends Controller
{
    //
    public function register()
    {
        return view('register');
    }
    public function getRegister(Request $request)
    {
        $username = $request->username;
        $password = md5($request->password);

        $mem           = new App\myModel;
        $mem->username = $username;
        $mem->password = $password;
        $mem->save();
        echo 'done!';
    }
}

If you put use App\myModel;如果你把use App\myModel; at the top, you only have to do $mem = new myModel;在顶部,您只需执行$mem = new myModel; in your code.在你的代码中。

But I would advice against registering users in this way.但我建议不要以这种方式注册用户。 Laravel has a great package to handle anything related to logins, registrations, etc: https://laravel.com/docs/6.x/authentication Laravel 有一个很棒的 package 来处理与登录、注册等相关的任何事情: https://laravel.com/docs/6.x/auth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM