前提・実現したいこと
laravel5.6にて、applyボタンを押した際、companiesテーブルのidをcompany_idとしてまたusersテーブルのidをuser_idとして、company_userという中間テーブルの中に情報を飛ばしたい。どのページにどのコードを記載すれば、中間テーブルに情報をinsert出来るのかがわかりません。
発生している問題・エラーメッセージ
if ($request->method() == 'OPTIONS') { return (new Route('OPTIONS', $request->path(), function () use ($methods) { return new Response('', 200, ['Allow' => implode(',', $methods)]); }))->bind($request); } $this->methodNotAllowed($methods); } /** * Throw a method not allowed HTTP exception. * * @param array $others * @return void * * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException */ protected function methodNotAllowed(array $others) { throw new MethodNotAllowedHttpException($others); } /** * Get routes from the collection by method. * * @param string|null $method * @return array */ public function get($method = null) { return is_null($method) ? $this->getRoutes() : Arr::get($this->routes, $method, []); } /** * Determine if the route collection contains a given named route. * * @param string $name * @return bool */ public function hasNamedRoute($name) Arguments
エラーメッセージ
""
該当のソースコード
↓コレがcompanies.showページのForm部分です。
{!!Form::open(['url'=>['/companies/{{$company->id}}'], 'method'=>'POST'])!!} {{ csrf_field() }} {{Form::hidden('company_id', '$company->id')}} {{Form::hidden('user_id', 'Auth::user()->id')}} {{Form::submit('apply',['class'=>'btn btn-danger'])}} {!! Form::close() !!}
試したこと
《companiesコントローラーページのstore部分に》
public function store(Request $request) { $company=Company::find($request->input('company_id')); $user=User::where('user_id', Auth::user()->id)->get(); if($user && $company){ $company->users()->attach($user->id); } }
を記載してみた。
《companyモデルの中↓》
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Company extends Model { protected $fillable= array('country_id', 'title', 'description'); public function country(){ return $this->belongsTo('App\Country'); } public function user() { return $this->belongsToMany('App\User')->using('App\Company_User'); } }
《userモデルページの中↓》
<?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 company (){ return $this->belongsToMany('App\Company')->using('App\Company_User'); } }
また、$php artisan make:migration create_company_user_table
で中間テーブルを作成し、migrationファイルの中も
public function up() { Schema::create('company_user', function (Blueprint $table){ $table->increments('id'); $table->integer('company_id')->unsigned(); $table->foreign('company_id')->references('id')->on('companies'); $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users'); $table->timestamps(); }); }
のように記載した後に、$php artisan migrate を行い正常にmigrateは出来ているのですが、
試しで$php artisan tinkerを使用して情報を中間テーブルにinsertしようとするとテーブルが見つからないというようなエラーが出ます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/31 16:07
退会済みユーザー
2018/08/31 16:22
2018/08/31 16:31
退会済みユーザー
2018/08/31 17:04 編集
退会済みユーザー
2018/08/31 17:02
2018/08/31 17:36
退会済みユーザー
2018/08/31 17:43 編集
2018/08/31 19:50 編集
2018/08/31 20:58
退会済みユーザー
2018/09/01 04:34
2018/09/01 11:48
退会済みユーザー
2018/09/01 11:53
2018/09/02 08:41
退会済みユーザー
2018/09/02 09:03 編集
2018/09/02 09:58
退会済みユーザー
2018/09/02 09:59
2018/09/02 11:01
退会済みユーザー
2018/09/02 12:40
2018/09/02 12:51
退会済みユーザー
2018/09/02 13:04
2018/09/02 13:51
退会済みユーザー
2018/09/02 13:53
2018/09/02 13:56
退会済みユーザー
2018/09/02 13:58
2018/09/02 14:01
退会済みユーザー
2018/09/02 14:07
2018/09/02 14:12
退会済みユーザー
2018/09/02 14:31
退会済みユーザー
2018/09/02 14:34
2018/09/02 14:48
退会済みユーザー
2018/09/02 14:52
退会済みユーザー
2018/09/02 14:57 編集
退会済みユーザー
2018/09/02 14:58
2018/09/02 15:03
2018/09/02 15:04
2018/09/02 15:05
退会済みユーザー
2018/09/02 15:05
2018/09/02 15:10
退会済みユーザー
2018/09/02 15:17
2018/09/02 15:44
退会済みユーザー
2018/09/02 15:50
2018/09/02 16:30
2018/09/02 16:31
退会済みユーザー
2018/09/03 00:56
2018/09/03 03:45
退会済みユーザー
2018/09/03 05:02 編集
2018/09/03 05:55
退会済みユーザー
2018/09/03 06:00