画像を保存するストレージへのリンクを作成
projecに移動して、
php artisan storage:link
プロジェクトに移動してプロフィール画像を保存するディレクトリの作成
mkdir public/storage/profile
コントローラの作成
php artisan make:controller PeopleController
モデルの作成
mkdir ./app/Models php artisan make:model models/Person
/project/app/Http/Controllers/PersonController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests\PersonRequest; use App\Models\Person; class PersonController extends Controller { public function user_list(){ $people = new Person(); $users = $people->select(); return view('users.list', ['users' => $users]); } public function user_add(){ return view('users.add'); } public function user_create(Request $request){ $people = new Person(); unset($request['_token']); $people->create($request); return redirect('/'); } }
/project/app/Models/Person.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; use Illuminate\Http\Request; class Person extends Model { protected $table = 'people'; protected $guarded = array('id'); public function select(){ $users = DB::table('people') ->join('category' , 'people.category_id', '=', 'category.id' ) ->join('prefectures', 'people.prefectures_id', '=', 'prefectures.id' ) ->join('generation', 'people.generation_id', '=', 'generation.id' ) ->select('people.*', 'category.category_name', 'prefectures.prefectures_name', 'generation.generation_name') ->orderBy('people.updated_at', 'DESC') ->paginate(3); return $users; } public function create($request){ $param = [ "people_name" => $request->name, "generation_id" => $request->generation, "category_id" => $request->category, "prefectures_id" => $request->prefectures, "password" => $request->password, "email" => $request->email, 'created_at' => now(), 'updated_at' => now(), ]; if(isset($request->image)) { //画像を保存 $param["image"] = $request->file('image')->getClientOriginalName(); $request->file('image')->storeAs('profile', $param['image'], 'public'); }else{ $param["image"] = ""; } //DBに保存 DB::insert('INSERT INTO people (people_name, generation_id, category_id, prefectures_id, comment, image, password, email, created_at, updated_at) VALUES(:people_name, :generation_id, :category_id, :prefectures_id, :comment, :image, :password, :email, :created_at, :updated_at)', $param); } }
フォーム作成用のパッケージをインストールする
$ vi /var/www/vhosts/example.net/httpdocs/project/composer.json (略) "require": { "php": "^7.1.3", "fideloper/proxy": "^4.0", "laravel/framework": "5.7.*", "laravel/tinker": "^1.0", "laravelcollective/html": "~5.0" ←追加 }, (略)
project]$ composer update
project/resources/views/users/add.blade.php
(略) <table class="table table-striped table-hover"> <thead> </thead> {{Form::open(['action' => 'PersonController@user_create', 'files' => true])}} {{ csrf_field() }} <tr><th>画像</th><td>{{ Form::file('image') }}</td></tr> <tr><th></th><td>{{Form::submit()}}</td></tr> {{Form::close()}} </tbody> </table> (略)
project/routes/web.php
Route::get('/add', 'PersonController@user_add'); Route::post('/add', 'PersonController@user_create');
これで画像を表示させることが出来る
<img class="card-img-top" src="/storage/profile/{{ $user->image }}" alt="">
http://ホスト名/add