問題は、連携したuser_idが Integrity constraint violation: 1048 Column 'user_id' cannot be nullのエラーが出ます。
laravel5.4を使っています。
登録ボタンを押すと下記のエラーが出ます、どうすれば解決できますか?
(2/2) QueryException SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into `jobs` (`job_id`, `user_id`, `updated_at`, `created_at`) values (2121212, , 2017-08-08 21:32:21, 2017-08-08 21:32:21))
userテーブル:
id | name |
Jobテーブル:
id | user_id | job_id
説明:
user_idはuserテーブルとjobテーブルを連携する為です
job_idはuser毎に複数のJob_idを登録したいです、使いやすい為、jobテーブルに分離しました。
UserModel:
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; public function jobs(){ return $this->hasMany('App\Job'); } }
jobModel:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Job extends Model { protected $fillable = [ 'user_id', 'job_id' ]; public function user(){ return $this->belongsTo('App\User'); } }
jobscreateRequest:
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class JobsCreateRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'user_id' =>'required', 'job_id' => 'required', ]; } }
JobController:
<?php namespace App\Http\Controllers; use Illuminate\Support\Facades\Auth; use App\Http\Requests\JobsCreateRequest; use Illuminate\Http\Request; use App\User; use App\Job; class UserJobsController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $users = User::latest()->get(); $query = Job::query(); $jobs = $query->orderBy('created_at','desc')->paginate(5); return view('admin.jobs.index', compact('jobs','users')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $users = User::pluck('name','user_id'); return view('admin.jobs.index',compact('users')); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $user= Auth::user(); Job::create([ 'name' => request('name'), 'job_id' => request('job_id'), 'user_id' => auth()->id() ]); return redirect('/admin/jobs/index'); } }
index.blade.php
<div class="col-sm-8"> {!! Form::open(['method'=>'POST', 'action'=> 'JobsController@store']) !!} {{ csrf_field() }} <div class="form-group"> {!! Form::label('user_id','名前') !!} {!! Form::select('user_id[]', $users->pluck('name', 'id')->toArray(), null, ['class' => 'form-control']) !!} </div> <div class="form-group"> {!! Form::label('job_id','Job_ID') !!} {!! Form::text('job_id',null,['class'=>'form-control',]) !!} </div> <div class="form-group"> {!! Form::submit('登録', ['class'=>'btn btn-info']) !!} </div> @include('includes.form_error') {!! Form::close() !!} </div>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/08/08 15:35
退会済みユーザー
2017/08/08 15:37
退会済みユーザー
2017/08/08 15:38
2017/08/08 15:56
退会済みユーザー
2017/08/08 16:01
2017/08/08 16:03
退会済みユーザー
2017/08/08 16:07
退会済みユーザー
2017/08/09 04:48
2017/08/09 05:16
退会済みユーザー
2017/08/09 05:23
2017/08/09 05:41
退会済みユーザー
2017/08/09 05:57