$item = DB::select('select * from people where id =:id',$param);
↓
$item = DB::select('select * from people where id =:id',$param)->get();
これはどうですか?
Like!
laravelを学習しているのですがクエリー文字が取得できません
laravelのバージョンは6.20.44です
クエリー文字?id=を指定する。そしてidのレコードを更新したい感じなんですが
まずクエリー文字が取得できないみたいです
初学者なのでわからないことが多いですがよろしくお願いします。
@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');
}
$item = DB::select('select * from people where id =:id',$param);
↓
$item = DB::select('select * from people where id =:id',$param)->get();
これはどうですか?
@kikiki445
QuestionerUndefined offset: 0
で調べてみてください
多分エラーメッセージの解釈が間違っちゃっている気がします
また、各処理(=戻り値の変数)ごとにログに出力してみて、どこの行までは自分の想定している内容で動作しているか確認しながら進めたほうがいいかと思います
以下参考までにLaravelのログ出力の方法です
https://qiita.com/ucan-lab/items/99f9f4989a894de1aea0
私はファサードでログ出力するをよく使っていました
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テーブル内に指定したデータがあるか確認してみてください。
@kikiki445
Questioner