Skip to main content
Communityユーザーがトップに表示した
Communityユーザーがトップに表示した
Communityユーザーがトップに表示した
Communityユーザーがトップに表示した
Communityユーザーがトップに表示した
Communityユーザーがトップに表示した
タイトルを編集
リンク
supa
  • 5,399
  • 4
  • 18
  • 41

laravel sql エラー: SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value

本文に 20 文字追加
ソース リンク
hinaloe
  • 4,751
  • 1
  • 13
  • 25
belongsTo(User::class);}}TasksController
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    protected $fillable = ['content', 'status', 'user_id'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
tasks()->orderBy('created_at', 'desc')>paginate(10);$data = ['user' => $user,'tasks' => $tasks,];return view('tasks.index', ['tasks' => $tasks,]);} else{return view('welcome');}}/*** Show the form for creating a new resource.** @return \Illuminate\Http\Response*/public function create(){$task = new Task;return view('tasks.create', ['task' => $task,]);}/*** Store a newly created resource in storage.** @param \Illuminate\Http\Request $request* @return \Illuminate\Http\Response*/public function store(Request $request){$this->validate($request, ['status' => 'required|max:10','content' => 'required|max:191',]);$task = new Task;$task->status = $request->status;$task->content = $request->content;$task->save();return redirect('/');}/*** Display the specified resource.** @param int $id* @return \Illuminate\Http\Response*/public function show($id){$task = Task::find($id);return view('tasks.show', ['task' => $task,]);}/*** Show the form for editing the specified resource.** @param int $id* @return \Illuminate\Http\Response*/public function edit($id){$task = Task::find($id);return view('tasks.edit', ['task' => $task,]);}/*** Update the specified resource in storage.** @param \Illuminate\Http\Request $request* @param int $id* @return \Illuminate\Http\Response*/public function update(Request $request, $id){$this->validate($request, ['status' => 'required|max:10','content' => 'required|max:191',]);$task = Task::find($id);$task->status = $request->status;$task->content = $request->content;$task->save();return redirect('/');}/*** Remove the specified resource from storage.** @param int $id* @return \Illuminate\Http\Response*/public function destroy($id){$task = Task::find($id);$task->delete();return redirect('/');}

TasksController

}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Task;

class TasksController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = [];
        if (\Auth::check()) {
            $user = \Auth::user();
            $tasks = $user->tasks()->orderBy('created_at', 'desc')>paginate(10);
            
            $data = [
                'user' => $user,
                'tasks' => $tasks,
            ];
            
            return view('tasks.index', [
                'tasks' => $tasks,
            ]);
            
        } else {
            return view('welcome');    
        }
        
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $task = new Task;

        return view('tasks.create', [
            'task' => $task,
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $this->validate($request, [
            'status' => 'required|max:10',
            'content' => 'required|max:191',
        ]);

        $task = new Task;
        $task->status = $request->status;
        $task->content = $request->content;
        $task->save();

        return redirect('/');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $task = Task::find($id);

        return view('tasks.show', [
            'task' => $task,
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $task = Task::find($id);

        return view('tasks.edit', [
            'task' => $task,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $this->validate($request, [
            'status' => 'required|max:10',
            'content' => 'required|max:191',
        ]);

        $task = Task::find($id);
        $task->status = $request->status;
        $task->content = $request->content;
        $task->save();

        return redirect('/');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $task = Task::find($id);
        $task->delete();

        return redirect('/');
    }
}
belongsTo(User::class);}}TasksControllertasks()->orderBy('created_at', 'desc')>paginate(10);$data = ['user' => $user,'tasks' => $tasks,];return view('tasks.index', ['tasks' => $tasks,]);} else{return view('welcome');}}/*** Show the form for creating a new resource.** @return \Illuminate\Http\Response*/public function create(){$task = new Task;return view('tasks.create', ['task' => $task,]);}/*** Store a newly created resource in storage.** @param \Illuminate\Http\Request $request* @return \Illuminate\Http\Response*/public function store(Request $request){$this->validate($request, ['status' => 'required|max:10','content' => 'required|max:191',]);$task = new Task;$task->status = $request->status;$task->content = $request->content;$task->save();return redirect('/');}/*** Display the specified resource.** @param int $id* @return \Illuminate\Http\Response*/public function show($id){$task = Task::find($id);return view('tasks.show', ['task' => $task,]);}/*** Show the form for editing the specified resource.** @param int $id* @return \Illuminate\Http\Response*/public function edit($id){$task = Task::find($id);return view('tasks.edit', ['task' => $task,]);}/*** Update the specified resource in storage.** @param \Illuminate\Http\Request $request* @param int $id* @return \Illuminate\Http\Response*/public function update(Request $request, $id){$this->validate($request, ['status' => 'required|max:10','content' => 'required|max:191',]);$task = Task::find($id);$task->status = $request->status;$task->content = $request->content;$task->save();return redirect('/');}/*** Remove the specified resource from storage.** @param int $id* @return \Illuminate\Http\Response*/public function destroy($id){$task = Task::find($id);$task->delete();return redirect('/');}}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    protected $fillable = ['content', 'status', 'user_id'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

TasksController

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Task;

class TasksController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = [];
        if (\Auth::check()) {
            $user = \Auth::user();
            $tasks = $user->tasks()->orderBy('created_at', 'desc')>paginate(10);
            
            $data = [
                'user' => $user,
                'tasks' => $tasks,
            ];
            
            return view('tasks.index', [
                'tasks' => $tasks,
            ]);
            
        } else {
            return view('welcome');    
        }
        
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $task = new Task;

        return view('tasks.create', [
            'task' => $task,
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $this->validate($request, [
            'status' => 'required|max:10',
            'content' => 'required|max:191',
        ]);

        $task = new Task;
        $task->status = $request->status;
        $task->content = $request->content;
        $task->save();

        return redirect('/');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $task = Task::find($id);

        return view('tasks.show', [
            'task' => $task,
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $task = Task::find($id);

        return view('tasks.edit', [
            'task' => $task,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $this->validate($request, [
            'status' => 'required|max:10',
            'content' => 'required|max:191',
        ]);

        $task = Task::find($id);
        $task->status = $request->status;
        $task->content = $request->content;
        $task->save();

        return redirect('/');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $task = Task::find($id);
        $task->delete();

        return redirect('/');
    }
}
本文から 1 文字削除
ソース リンク
user37276
  • 11
  • 1
  • 2
  • 4

現在laravelでタスクリストを作ってます。
user_idカラムを追加しましたが、下記エラーが出ます。
考えられる原因を教えてもらいたいです。
ソースコードはどのソースコードを載せればいいのかわからないです。
よろしくお願いします。

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `tasks` (`status`, `content`, `updated_at`, `created_at`)

Task.php

belongsTo(User::class); } } TasksController tasks()->orderBy('created_at', 'desc')->paginate(10); $data = [ 'user' => $user, 'tasks' => $tasks, ]; return view('tasks.index', [ 'tasks' => $tasks, ]); } else { return view('welcome'); } } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $task = new Task; return view('tasks.create', [ 'task' => $task, ]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, [ 'status' => 'required|max:10', 'content' => 'required|max:191', ]); $task = new Task; $task->status = $request->status; $task->content = $request->content; $task->save(); return redirect('/'); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $task = Task::find($id); return view('tasks.show', [ 'task' => $task, ]); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $task = Task::find($id); return view('tasks.edit', [ 'task' => $task, ]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $this->validate($request, [ 'status' => 'required|max:10', 'content' => 'required|max:191', ]); $task = Task::find($id); $task->status = $request->status; $task->content = $request->content; $task->save(); return redirect('/'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $task = Task::find($id); $task->delete(); return redirect('/'); } }

現在laravelでタスクリストを作ってます。
user_idカラムを追加しましたが、下記エラーが出ます。
考えられる原因を教えてもらいたいです。
ソースコードはどのソースコードを載せればいいのかわからないです。
よろしくお願いします。

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `tasks` (`status`, `content`, `updated_at`, `created_at`)

Task.php

belongsTo(User::class); } } TasksController tasks()->orderBy('created_at', 'desc')->paginate(10); $data = [ 'user' => $user, 'tasks' => $tasks, ]; return view('tasks.index', [ 'tasks' => $tasks, ]); } else { return view('welcome'); } } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $task = new Task; return view('tasks.create', [ 'task' => $task, ]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, [ 'status' => 'required|max:10', 'content' => 'required|max:191', ]); $task = new Task; $task->status = $request->status; $task->content = $request->content; $task->save(); return redirect('/'); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $task = Task::find($id); return view('tasks.show', [ 'task' => $task, ]); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $task = Task::find($id); return view('tasks.edit', [ 'task' => $task, ]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $this->validate($request, [ 'status' => 'required|max:10', 'content' => 'required|max:191', ]); $task = Task::find($id); $task->status = $request->status; $task->content = $request->content; $task->save(); return redirect('/'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $task = Task::find($id); $task->delete(); return redirect('/'); } }

現在laravelでタスクリストを作ってます。
user_idカラムを追加しましたが、下記エラーが出ます。
考えられる原因を教えてもらいたいです。
ソースコードはどのソースコードを載せればいいのかわからないです。
よろしくお願いします。

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `tasks` (`status`, `content`, `updated_at`, `created_at`)

Task.php

belongsTo(User::class); } } TasksController tasks()->orderBy('created_at', 'desc')>paginate(10); $data = [ 'user' => $user, 'tasks' => $tasks, ]; return view('tasks.index', [ 'tasks' => $tasks, ]); } else { return view('welcome'); } } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $task = new Task; return view('tasks.create', [ 'task' => $task, ]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, [ 'status' => 'required|max:10', 'content' => 'required|max:191', ]); $task = new Task; $task->status = $request->status; $task->content = $request->content; $task->save(); return redirect('/'); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $task = Task::find($id); return view('tasks.show', [ 'task' => $task, ]); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $task = Task::find($id); return view('tasks.edit', [ 'task' => $task, ]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $this->validate($request, [ 'status' => 'required|max:10', 'content' => 'required|max:191', ]); $task = Task::find($id); $task->status = $request->status; $task->content = $request->content; $task->save(); return redirect('/'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $task = Task::find($id); $task->delete(); return redirect('/'); } }
本文に 3368 文字追加
ソース リンク
user37276
  • 11
  • 1
  • 2
  • 4
読み込んでいます
本文に 4 文字追加
ソース リンク
hinaloe
  • 4,751
  • 1
  • 13
  • 25
読み込んでいます
ソース リンク
user37276
  • 11
  • 1
  • 2
  • 4
読み込んでいます