laravel项目,存储对象而不是字符串

时间:2016-11-15 10:14:01

标签: php

这是我用来将URL添加到数据库中的控制器。主要思想是发送HTTP请求并根据响应将其保存到数据库中。当添加URL时,必须将其保存到数据库中。

class Notification extends Model
{
    protected $fillable = ['id','website_url','email','slack_channel','check_frequency','alert_frequency','speed_frequency','active'];

    public function statuses(){
        return $this->belongsToMany('App\Status')->withPivot('values')->withTimestamps();
    }

    public function status($s_type)
    {
        $o_health_status = Status::where('name', 'health')->first();
        $o_speed_status = Status::where('name', 'speed')->first();

        if ($s_type === 'health'){

            $o_response = $this->statuses()->where('status_id', $o_health_status->id)
            ->select('values')->orderBy('created_at','ASC')->first();

            if($o_response === null){
                return 'unsigned';
            }
            $o_response = $o_response->values;
            return $o_response;         
        }else{

            $o_response = $this->statuses()->where('status_id', $o_speed_status->id)
            ->select('values')->orderBy('created_at', 'desc')->first();
            if($o_response === null){
                return 'unsigned';
            }

            $o_response = $o_response->values;
            return $o_response;
        }
    }

我的模型中的商店方法更像是

    else {
        // store
        $notification = new Notification();
        $statusType = Status::where('name', 'health')->first();
        $statusTypeSpeed = Status::where('name', 'speed')->first();
        // trial

        $notification->website_url       = $request->get('website_url');
        $notification->email             = $request->get('email');
        $notification->slack_channel     = $request->get('slack_channel');
        $notification->check_frequency   = $request->get('check_frequency');
        $notification->alert_frequency   = $request->get('alert_frequency');
        $notification->speed_frequency   = $request->get('speed_frequency');

        $notification->active = ($request->has('active') && $request->get('active') === 'on') ? 0 : 1;

        $notification->save();
        $notification->statuses()->attach($statusType,['values'=>$statusTypeSpeed]);

        // redirect
        Session::flash('message', 'Successfully created notification!');
        return redirect()->route('dashboard');
        }
}

我原本希望在我的数据库的第二列和第三列中存储unsinged,但是第二列是存储一个对象

    else {
        // store
        $notification = new Notification();
        $statusType = Status::where('name', 'health')->first();
        $statusTypeSpeed = Status::where('name', 'speed')->first();

        // trial
        $notification->website_url       = $request->get('website_url');
        $notification->email             = $request->get('email');
        $notification->slack_channel     = $request- >get('slack_channel');
        $notification->check_frequency   = $request->get('check_frequency');
        $notification->alert_frequency   = $request->get('alert_frequency');
        $notification->speed_frequency   = $request->get('speed_frequency');
        $notification->active = ($request->has('active') && $request->get('active') === 'on') ? 0 : 1;
        $notification->save();
        $notification->statuses()->attach($statusType,['values'=>$statusTypeSpeed]);

        // redirect
        Session::flash('message', 'Successfully created notification!');
        return redirect()->route('dashboard');
    }
}

我期待我的数据库的第一列和第二列上的unsigned。而不是在我存储对象的第一个组合。

1栏

{"id":1,"name":"health"} 2016-11-14 13:07:18    

2栏

unsigned-2016-11-14 13:07:18

0 个答案:

没有答案