【问题标题】:laravel syntax error unexpected '.' on attempted concatenation of string in modellaravel 语法错误意外'。'关于模型中字符串的尝试连接
【发布时间】:2023-03-19 08:36:01
【问题描述】:

我在 Laravel 工作,我有一个模型组,我有验证规则。我试图拥有一个唯一的 name_group 但仅限于给定的年份。例如,如果我将.$this->year_groups 替换为 2016,则下面的代码可以完美运行。但是,当我尝试通过连接.this->year_groups 添加要创建的组的实际年份时,出现语法错误:

Symfony\Component\Debug\Exception\FatalErrorException 语法错误,意外'.',期待')'

我看过很多例子,他们(似乎)是这样写的,我就是找不到哪里错了。我在想也许这与它在数组中有关...?

任何帮助将不胜感激!

型号:

<?php
use Illuminate\Auth\UserTrait; 
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class Group extends Eloquent implements UserInterface,RemindableInterface   
{

    use UserTrait, RemindableTrait;
    protected $table = 'groups';
    protected $primaryKey = "id_groups"; 
    protected $fillable = array('name_groups','year_groups','grados_id_grados');

    //The error is in the following $rules    
    public static $rules = array(

          'year_groups'=> 'required',
          'name_groups'=> 'required|unique:groups,name_groups,NULL, id_groups,year_groups,' . $this->year_groups,         
          'grados_id_grados' => 'required'
      );


     public function grado()
     {
     return $this->belongsTo('Grado','grados_id_grados'); 
     }

     public function students()
     {
     return $this->belongsToMany('Student','group_student','id_group','id_student')->withTimestamps();
     }
 public function teachers()
     {
     return $this->belongsToMany('Teacher','group_subject_teacher','id_group','id_teacher')->withPivot('id_subject','year_groups')->withTimestamps();
  }  
}

在控制器中,我从 store 方法调用验证:

public function store()
{
    $input = Input::all();
    $validation = Validator::make($input, Group::$rules);
    if($validation->passes()){
          $group = new Group;
          $group->name_groups = Input::get('name_groups');
          $group->year_groups = Input::get('year_groups');
          $group->grados_id_grados = Input::get('grados_id_grados');
          $group->save();
    }

 }

【问题讨论】:

  • 在您通过验证时尝试 dd($this) 以查看它是什么。阅读您的问题,我不确定您的规则是否在您的控制器、模型或请求中。

标签: php laravel


【解决方案1】:

查看您的代码,似乎 $rules 是类的变量或属性。您为属性分配值的方式是错误的,因此会引发错误。查看下面的代码并相应地安排您的代码:-

class anyClass {
 private $year_groups = "2016";     
 public $rules = [];
 public function __construct(){
    $this->rules = array(
        'year_groups'=> 'required',
        'name_groups'=> 'required|unique:groups,name_groups,NULL,    id_groups,year_groups,'.$this->year_groups,
        'grados_id_grados' => 'required'
       );

 }
}

【讨论】:

  • 是的,感谢这让我走上了正确的道路!我对其进行了一些更改以使其在我的模型和控制器中工作,并且我会将修改后的代码放在下面以防有人可能受益(不确定我应该将修改后的代码放在哪里?)我一直在自己搞混Laravel 和 Php,所以如果我正在做任何我不应该或可以做得更好的事情,请随时发表评论!
  • @maeve 太好了,如果有帮助的话
【解决方案2】:

我将模型更改为:

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class Group extends Eloquent implements UserInterface,RemindableInterface     
{

    use UserTrait, RemindableTrait;
    protected $table = 'groups';
    protected $primaryKey = "id_groups"; 
    protected $fillable = array('name_groups','year_groups','grados_id_grados');

      //This part I changed
    public static $rules = [];

    public static function _construct($year){
         $rules = array(

          'year_groups'=> 'required',
          'name_groups'=> 'required|unique:groups,name_groups,NULL, id_groups,year_groups,' . $year,
          'grados_id_grados' => 'required'   
      );
     return $rules;
    }



     public function grado()
    {
     return $this->belongsTo('Grado','grados_id_grados');
    }

    public function students()
    {
     return $this->belongsToMany('Student','group_student','id_group','id_student')->withTimestamps();
    }
    public function teachers()
   {
     return $this->belongsToMany('Teacher','group_subject_teacher','id_group','id_teacher')->withPivot('id_subject','year_groups')->withTimestamps();

   }     
}

然后在控制器中:

public function store()
{
    $input = Input::all();
    $validation = Validator::make($input,  Group::_construct(Input::get('year_groups')));

    if($validation->passes()){

          $group = new Group;
          $group->name_groups = Input::get('name_groups');
          $group->year_groups = Input::get('year_groups');
          $group->grados_id_grados = Input::get('grados_id_grados');
          $group->save();
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多