laravel语法错误意外'。' 在模型中尝试连接字符串

laravel语法错误意外'。' 在模型中尝试连接字符串

问题描述:

I am working in Laravel, and I have a model Group where I have rules for validation. I am attempting to have a unique name_group but only for the given year. The code below works perfectly if I replace .$this->year_groups with 2016 for example. But when I try to add the actual year of the group to be created by concatenating .this->year_groups, I get a syntax error:

Symfony \ Component \ Debug \ Exception \ FatalErrorException syntax error, unexpected '.', expecting ')'

I have looked at many examples and they (seem) to be written this way, and I just can't find what is wrong. I am thinking perhaps it has something to do that this is in an array...?

Any help would be greatly appreciated!!

Model:

<?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();
  }  
}

In the Controller I call validation from the store method:

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();
    }

 }

Looking your code, it seems $rules is variable or property of class. The way you are assigning values to property are wrong, so it is throwing error. Look below code and arrange your code accordingly:-

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'
       );

 }
}

I changed Model to:

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();

   }     
}

Then in Controller:

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();
  }
}