如何解决Laravel中JSON数据的插入问题

时间:2019-11-08 18:21:26

标签: php json laravel

我有一个名为“ rules”的表,该表有3列,主题为 JSON 类型的列。现在在我的表单中,有两个选择列表。一个是select2,另一个是常规选择列表。因此,当要插入列时将不会保存。请帮我解决问题。 我的控制器

public function store(DepartmentRequest $request)
    {
        $department=new Department;
        $department->name=$request->name;
        $department->minimum=$request->minimum;
        $department->slug=$request->slug;
        $department->save();
        if ($request->range && $request->total) {
            $exceed=$request->range + $request->total;
            if ($exceed > 100) {
                return back();
            }
        }
        if($request->subject_id && $request->range)
        {
            $rule = new Rule();
            $arr_tojson = json_decode(json_encode($request->subject_id),TRUE);
            $rule->department_id=$department->id;
            $rule->range=$request->range;
            $rule->save();
        }
        if($request->id && $request->total)
        {   
            $rule = new Rule;
            $arr_tojson = json_decode(json_encode($request->id),TRUE);
            $rule->subject_id=$arr_tojson;
            $rule->department_id=$department->id;
            $rule->range=$request->total;
            $rule->save();
        }
        return redirect('departments');

    }

我的模特

class rule extends Model
{
    protected $fillable=['subject_id','department_id','range'];

    protected $casts=['subject_id' => 'array'];
   }

请有人帮我将其插入规则表中。

dd($ request-> subject_id)

enter image description here

dd($ request-> id) enter image description here

0 个答案:

没有答案