LoginSignup
kikiki445
@kikiki445

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

laravel クエリー文字が取得できない

laravel クエリー文字が取得できない!

laravelを学習しているのですがクエリー文字が取得できません

laravelのバージョンは6.20.44です

DB: peopleテーブルの内容

スクリーンショット (689).png

エラー

スクリーンショット (690).png

クエリー文字?id=を指定する。そしてidのレコードを更新したい感じなんですが
まずクエリー文字が取得できないみたいです
初学者なのでわからないことが多いですがよろしくお願いします。

edit.blade.php

@extends('layouts.helloapp')

@section('title','Add')

@section('menubar')
    @parent
    更新ページ
@endsection

@section('content')
<form action="/hello/edit" method="post">
    <table>
        @csrf
        <input type="hidden" name="id" value="{{$form->id}}">
        <tr><th>name: </th><td><input type="text" name="name" value="{{$form->name}}"></td></tr>
        <tr><th>mail: </th><td><input type="text" name="mail" value="{{$form->mail}}"></td></tr>
        <tr><th>age: </th><td><input type="text" name="age" value="{{$form->age}}"></td></tr>
        <tr><th></th><td><input type="submit" value="send" ></td></tr>
    </table>
    </form>
@endsection
@section('footer')
copyright 2022 outp
@endsection

コントローラーのソースコードの一部

    public function edit(Request $request){
        $param = ['id' => $request->id];
        $item = DB::select('select * from people where id =:id',$param);
        return view('hello.edit',['form' => $item[0]]);

    }
    public function update(Request $request){
        $param = [
            'id' => $request->id,
            'name' => $request->name,
            'mail' => $request->mail,
            'age' => $request->age,
        ];
        DB::update('update people set name =:name, mail=:mail, age = :age where id=:id',$param);
        return redirect('/hello');
    }
0

3Answer

$item = DB::select('select * from people where id =:id',$param);

$item = DB::select('select * from people where id =:id',$param)->get();

これはどうですか?

1

Comments

  1. @kikiki445

    Questioner
    できなかったです..
  2. そうですか。。 (´・ω・`)
    他の方のコメントの内容みると多分

    ```
    <input type="hidden" name="id" value="{{$form->id}}">
    ```

    $formにidがセットされてない気がします。
    色々デバッグして解決することを期待してます!

Undefined offset: 0で調べてみてください
多分エラーメッセージの解釈が間違っちゃっている気がします

また、各処理(=戻り値の変数)ごとにログに出力してみて、どこの行までは自分の想定している内容で動作しているか確認しながら進めたほうがいいかと思います

以下参考までにLaravelのログ出力の方法です
https://qiita.com/ucan-lab/items/99f9f4989a894de1aea0
私はファサードでログ出力するをよく使っていました

1

edit メソッドの頭で$requestの中身を確認してちゃんと入ってるかデバックをしてみてください。

    public function edit(Request $request){
         
        dd($request->all());    
    
        $param = ['id' => $request->id];
        $item = DB::select('select * from people where id =:id',$param);
        return view('hello.edit',['form' => $item[0]]);

    }

もしidが取れてるのであれば、ここまでは来てます。

peopleテーブル内にid=3のデータが存在してなくて取れてないかもしれないので、peopleテーブル内に指定したデータがあるか確認してみてください。

0

Comments

  1. @kikiki445

    Questioner
    id取れてないです....

Your answer might help someone💌