sclswin

1065 经验值

框架自带的会员注册方法,我改了一下,让第二条语句出错。

protected function create(array $data)
    {

        //DB::transaction(function () use ($data) {
        
            DB::beginTransaction();
            try {
                DB::table('users')->insert([
                    'name' => $data['name'],
                    'email' => $data['email'],
                    'password' => bcrypt($data['password']),
                ]);
                DB::table('user_info')->insert([
                    'uid' => 22,
                ]);
            }catch(\Exception $e){
                DB::rollback();
                throw $e;
            }
        //});
    }

我改成这样子了 但是事务没有回滚,直接抛了异常,提示字段类型不符。uid我设的字符串类型
users表记录还是插入了。什么原因呢
有指定模型主键 但是没有做表字段约束

找到原因了。当我没问过。。。

可能我的这种做法不对,我试过setter ,还是不行,

刚看了,和我的完全不是一回事吧?难道你觉得是一样的么?你视频中是赋值,我是要给传参,这能一样么

我就是试了一下服务容器。
laravelacademy.org/post/6699.html

服务容器就是这个样子的,我就是变了一下,不知道我这样写是不是根本就不对,始终没找到怎么给构造函数传参,所以就问一下,直接new 我也知道可以实现。

Container make的解析对像不是类名或接口名么
用法是在控制器的构造函数里依懒注入userModel

public function __construct(userModel $userModel){
    $this->userModel = $userModel;
}

要传的数组是个变量。

我的那个代码太多了就不贴了,我简化一下

abstract class  Model implements CriteriaInterface{
public $model;
public function __construct(Container $container,Collection $collection)
   {
       $this->container = $container;
       $this->makeModel();
   }
abstract function model();
public function makeModel() {
       $model = $this->container->make($this->model());
return $this->model = $model
}
}

class userModel extends Model{
public function model(){
return 'App\User';
}
}

流程就这样的
现在问题来了
AppUser模型的构造函数需要传入一个数组。
要怎么传?

看了一下源码 第二个参数是个自增的 bool值 。。。

public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->string('id',18);
            $table->primary('id');
            $table->integer('a',10)->nullable()->comment("aaaaaa");
            $table->string('b',20)->nullable()->comment("bbbbbb");
            $table->integer('c')->default(0)->comment("aaaaa");
        });
    }

今天发现一个问题数据写入不了表,这张表是上次建的,结果我打开表发现结构不对。我又测试了一下,结果报错

[Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table `tests` add primary key `tests_id_primary`(`id`))


[PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined

没看懂 为什么会报这个错。

  @if ($paginator->hasPages())
    @php
    $page_min = max(1, $paginator->currentPage()-5);
    $page_max = min($paginator->lastPage(), $paginator->currentPage()+5);
    @endphp
<div class="pages">
    @if($paginator->currentPage() > $page_min)
        @php
        $page_i = $paginator->currentPage()-1;
        @endphp
        <a href="  $paginator->previousPageUrl()  " class="page_pre ">« 上一页</a>
         @if($page_min > 1)
             @php
                $page_i = 1;
             @endphp
            <a href=" $paginator->url(1) ">1...</a>
         @endif
            @for($page_i = $page_min; $page_i < $paginator->currentPage(); $page_i++)
                <a href=" $paginator->url($page_i) "> $page_i </a>
            @endfor
    @endif
        <strong> $paginator->currentPage() </strong>

    @if($paginator->currentPage() < $page_max)
            @for($page_i = $paginator->currentPage()+1; $page_i <= $page_max; $page_i++)
                <a href=" $paginator->url($page_i) "> $page_i </a>
            @endfor
             @if ($page_max < $paginator->lastPage())
                 @php
                    $page_i = $paginator->lastPage();
                 @endphp
                    <a href=" $paginator->url($page_i) ">... $paginator->lastPage() </a>
            @endif
        @php
                $page_i = $paginator->currentPage()+1;
        @endphp
        <a href="  $paginator->nextPageUrl()  " class="page_next">下一页 »</a>
    @endif

</div>
@endif

写了一个分页 看起来总感觉好复杂 看起好累 求简化

找到一个问题,没有主键 使用了软删除后不能使用 delete()
public function delete()

{
    if (is_null($this->getKeyName())) {
        throw new Exception('No primary key defined on model.');
    }

如果一个模型没有主键会有哪些影响,除了find() 和关系表 其他的有什么影响么
模型 primaryKey 与 keyType 是不是就可以不用理会还是要设置 成NULL ,
我试过一下好像没什么影响。具体的我没找到哪儿有资料说这种情况