【やりたいこと】
'/edit'ページからurlを入力する➡入力内容を取得する
【問題】
updateメソッドでdd($inputs)するとURLがない!!
【質問】
どの部分を編集するとurlが受け取れるのでしょうか?
array:4 [▼ "_token" => "0dySbPSMGAfFtBE9jXVnlH8xZENw4z7zUWXxlF10" "user_id" => "1" "content" => "11/17/19:46" "image" => "classroom.jpg" ]
【コード】
web.php
php
1<?php 2 3Auth::routes(); 4 5Route::get('/', 'HomeController@index')->name('home'); 6Route::get('/home', 'HomeController@index')->name('home'); 7Route::get('/create', 'HomeController@create')->name('create'); 8Route::get('/memo', 'HomeController@memo')->name('memo'); 9Route::post('/store', 'HomeController@store')->name('store'); 10Route::get('/edit/{id}', 'HomeController@edit')->name('edit'); 11Route::post('/update/{id}', 'HomeController@update')->name('update'); 12Route::get('/content/{id}', 'HomeController@content')->name('content'); 13Route::post('/delete/{id}', 'HomeController@delete')->name('delete'); 14
HomeController.php
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use \App\Memo; 7use \App\User; 8 9 10class HomeController extends Controller 11{ 12 /** 13 * Create a new controller instance. 14 * 15 * @return void 16 */ 17 public function __construct() 18 { 19 $this->middleware('auth'); 20 } 21 22 /** 23 * Show the application dashboard. 24 * 25 * @return \Illuminate\Contracts\Support\Renderable 26 */ 27 public function index() 28 { 29 //↓でログインしているユーザの情報を渡す 30 $user = \Auth::user(); 31 //ここでメモの中身をログインしているアカウントのIDと一致させないといけない 32 $memos = Memo::where('user_id',$user['id'])->where('status',1)->get(); 33 //dd($memos); 34 return view('home',compact('user','memos')); 35 } 36 37 public function memo() 38 { 39 $user = \Auth::user(); 40 41 return view('memo',compact('user')); 42 } 43 44 public function store(Request $request) 45 { 46 $data = $request->all(); 47 //dd($data); 48 $image = $request->file('image'); 49 50 if($request->hasFile('image')){ 51 $path = \Storage::put('/public', $image); 52 $path = explode('/', $path); 53 }else{ 54 $path = null; 55 } 56 // POSTされたデータをDB(memosテーブル)に挿入 57 // MEMOモデルにDBへ保存する命令を出す 58 $memo_id = Memo::insertGetId( 59 ['content' => $data['content'], 60 'user_id' => $data['user_id'], 61 'image' => $path[1], 62 'status' => 1]); 63 64 //'url' => $data['url'], 65 66 // リダイレクト処理 67 return redirect()->route('home'); 68 } 69 70 public function edit($id){ 71 $user = \Auth::user(); 72 $memo = Memo::where('status','1')->where('id',$id)->where('user_id',$user['id'])->first(); 73 74 $memos = Memo::where('user_id',$user['id'])->where('status',1)->orderBy('updated_at','DESC')->get(); 75 76 return view ('edit',compact('memo','user','memos')); 77 } 78 79 public function update(Request $request , $id){ 80 $inputs = $request->all(); 81 dd($inputs); 82 /* 83 Memo::where('id',$id)->update(['content' => $inputs['content'], 84 'image' => $inputs['image'], 85 'url' => $inputs['url'] 86 ]); 87 */ 88 return redirect()->route('home'); 89 } 90 91 public function content($id){ 92 $user = \Auth::user(); 93 $memo = Memo::where('status','1')->where('id',$id)->where('user_id',$user['id'])->first(); 94 95 $memos = Memo::where('user_id',$user['id'])->where('status',1)->orderBy('updated_at','DESC')->get(); 96 97 return view ('content',compact('memo','user','memos')); 98 } 99 100 public function delete(Request $request , $id){ 101 $inputs = $request->all(); 102 //dd($inputs); 103 Memo::where('id',$id)->update([ 'status' => 2 ]); 104 105 return redirect()->route('home')->with('success','メモの削除が完了しました'); 106 } 107 108 109} 110
edit.php
php
1@extends('layouts.app') 2 3@section('content') 4<div class="row justify-content-center ml-0 mr-0 h-100"> 5 <div class="card w-100"> 6 <div class="card-header">メモ編集</div> 7 <form method = 'POST' action = "/delete/{{ $memo['id'] }}" id = 'delete-form'> 8 @csrf 9 <button><i id = 'delete-button' class = "fas fa-trash"></i></button> 10 </form> 11 <div class="card-body"> 12 <form method='POST' action="{{route('update',['id' => $memo['id']] ) }}"> 13 @csrf 14 <input type='hidden' name='user_id' value="{{ $user['id'] }}"> 15 <div class="form-group"> 16 <textarea name='content' class="form-control"rows="10"> 17 {{ $memo['content'] }} 18 </textarea> 19 20 <div class="form-group"> 21 <form method='POST' action="{{route('update',['id' => $memo['id']] ) }}" enctype="multipart/form-data"> 22 <label for="image">画像のupload</label> 23 <input type="file" class="form-control-file" name='image' id="image"> 24 </div> 25 26 <!--URLをとってくる--> 27 <div class="mb-3"> 28 <form method='POST' action="{{route('update',['id' => $memo['id']] ) }}"> 29 <label for="disabledTextInput" class="form-label">URLの入力してください</label> 30 <input type="text" id="disabledTextInput" class="form-control" placeholder="URL"> 31 </div> 32 33 </div> 34 <div class="form-group"> 35 </div> 36 <button type='submit' class="btn btn-primary btn-lg">更新する</button> 37 38 </form> 39 </div> 40 </div> 41</div> 42@endsection 43 44
回答1件
あなたの回答
tips
プレビュー