Reputation: 107
I am studying an eloquent relationship and I want to create a post for specific user. I want to add User_Id
in create form
Below are my code snippets:
@extends('layouts.app')
@section('content')
<div class="container">
<form method="post" action="{{route('articles.store')}}">
@csrf
<input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
<div class="form-group">
<label>Başlık</label>
<input class="form-control" name="title" placeholder="Başlık">
</div>
@if($errors->has('title'))
<span class="text-danger">
{{$errors->first('title')}}
</span>
@endif
<div class="form-group">
<label>Post İçeriği</label>
<textarea class="form-control" name="body" placeholder="İçerik" rows="3"></textarea>
</div>
@if($errors->has('body'))
<span class="text-danger">
{{$errors->first('body')}}
</span>
@endif
<button type="submit" class="btn btn-primary">Kaydet</button>
</form>
</div>
@endsection
and my article model is like this;
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $fillable=[
'title','body','user_id'
];
public function user()
{
return $this->belongsTo('App\User');
}
}
And here is my controller;
public function store(ArticleRequest $request)
{
Auth::user();
$article = new Article();
$article -> title=$request->get('title');
$article -> body=$request->get('body');
$article->save();
return redirect()->route('articles.index');
}
But at the end, I am getting below error:
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a
default value (SQL: insert into `articles` (`title`, `body`, `updated_at`,
`created_at`) values (Merhaba, Bu post id değeri 1 olan user tarafından
yazıldı., 2018-05-03 06:55:45, 2018-05-03 06:55:45))
Upvotes: 0
Views: 2975
Reputation: 9060
You can send the user id along with others post data like others answers did. But since you studying how eloquent relationship works, then why not using it to save the article model.
Create article relationship inside User
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
.........
.........
// Since user can have many articles, then i assumed you need
// hasMany relationship here, otherwise please change accordingly.
public function articles()
{
return $this->hasMany(Article::class);
}
}
And in your controller(it will automatically add foreign key to Article
model):
public function store(ArticleRequest $request)
{
$user = Auth::user();
$user->articles()->create($request->only(
'title',
'body'
));
return redirect()->route('articles.index');
}
And lastly, remove user_id
input field from form(no longer needed as the article will saved by relationship of authenticated User
model):
<!-- remove this -->
<input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
Upvotes: 1
Reputation: 126
You can take current logged in user ID from Auth user in store procedure.
public function store(ArticleRequest $request)
{
Auth::user();
$article = new Article();
$article -> user_id=Auth::user()->id;
$article -> title=$request->get('title');
$article -> body=$request->get('body');
$article->save();
return redirect()->route('articles.index');
}
Upvotes: 1
Reputation: 1786
You are getting user in
$user = Auth::user();
use this id
$article -> user_id = $user->id;
public function store(ArticleRequest $request)
{
$user = Auth::user();
$article = new Article();
$article -> title=$request->get('title');
$article -> body=$request->get('body');
$article -> user_id = $user->id;
$article->save();
return redirect()->route('articles.index');
}
Or
else you can get user_id in post request from form(you can use hidden field for user_id in your form for this) and just pass it
$article -> user_id=$request->get('user_id');
Upvotes: 0
Reputation: 1373
public function store(ArticleRequest $request)
{
Auth::user();
$article = new Article();
$article -> title=$request->get('title');
$article -> body=$request->get('body');
$article ->user_id=$request->get('user_id'); // add this line
$article->save();
return redirect()->route('articles.index');
}
Upvotes: 1