【问题标题】:laravel 4 preg_replace(): Parameter mismatchlaravel 4 preg_replace():参数不匹配
【发布时间】:2014-04-28 12:11:34
【问题描述】:

我试图将 2 个不同表的值存储在一个数组中,然后在视图中显示值。我有 2 张桌子“汽车”和“班级”。表汽车有一个名为“类”的字段,其中包含类表的 id,表类有字段“类”和“id”。我正在做以下查询:

public function edit($id) {
    $data['values'] = DB::select('select * from cars where id = ?', array($id));
    $class = DB::select('select class from cars where id = ?', array($id));  
    $data['class']= DB::select('select class from classes where id = ?', array($class)); 
    $data['classes'] = DB::table('classes')->orderBy('class', 'asc')->distinct()->lists('class', 'id');
    return View::make('pages.edit', $data);
}

在视野中:

<div class="form-group">
    {{ Form::label('class', 'Class', array('class'=>'control-label col-lg-4')) }}
    <div class="col-lg-8">
        {{ Form::select('class', $classes ,$class->class,array('class' => 'form-control') ) }} 
    </div>
</div>

在 $class 中,我想保存该特定汽车的班级 ID。有没有其他方法可以在没有内部连接的情况下做到这一点?它显示以下错误:

preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

【问题讨论】:

    标签: php mysql laravel


    【解决方案1】:
    // this returns array(stdClass('class'=>'classValue'))
    $data['class']= DB::select('select class from classes where id = ?', array($class)); 
    
    // so simply do this:
    $classArray= DB::select('select class from classes where id = ?', array($class)); 
    $data['class']= $classArray[0];
    

    【讨论】:

    • {{ Form::select('class', $classes, $class, array('class' => 'form-control') ) }}
    • 第 3 行 - 有未定义的 $class 变量,它不是视图。在 Form::select 中使用 $class->class 因为它仍然是对象属性
    • 对不起,这是另一个错误。现在它显示了这个未定义的变量:class (View: C:\wamp\www\national\app\views\pages\edit.blade.php)
    • 现在在这里 $classArray = DB::select('select class from classes where id = ?', array($class)); “未定义变量:类”
    • laravel.io/bin/VqEMn and :preg_replace():参数不匹配,pattern是字符串,replacement是数组
    猜你喜欢
    • 2016-05-11
    • 1970-01-01
    • 2023-03-29
    • 2014-10-21
    • 2016-01-25
    • 2013-01-06
    • 2017-03-26
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多