Larabel5でWebアプリを作っているのですが、お気に入り登録 の機能を実装したいと思っています。
ユーザー認証でログインした状態で、表示されている各アイテムにあるお気に入りボタンを押すと、お気に入りに登録され、それをお気に入り一覧としてユーザーページで確認できるようにしたいです。
リーレーションだったりをいろいろ調べたのですが、自分がやろうとしていることの場合はどうプログラムを書いていいか分かりませんでした。
具体的な方法などを教えてくださるとありがたいです。
- DBの操作はEloquentで行っています。
- ユーザー用のUserのUserクラスとアイテム用のItemクラスがあります。
php
1//Userクラス 2<?php 3 4namespace App; 5 6use Illuminate\Notifications\Notifiable; 7use Illuminate\Contracts\Auth\MustVerifyEmail; 8use Illuminate\Foundation\Auth\User as Authenticatable; 9 10class User extends Authenticatable 11{ 12 use Notifiable; 13 14 /** 15 * The attributes that are mass assignable. 16 * 17 * @var array 18 */ 19 protected $fillable = [ 20 'name', 'email', 'password', 21 ]; 22 23 /** 24 * The attributes that should be hidden for arrays. 25 * 26 * @var array 27 */ 28 protected $hidden = [ 29 'password', 'remember_token', 30 ]; 31} 32
PHP
1//Itemクラス 2<?php 3 4namespace App; 5 6use Illuminate\Database\Eloquent\Model; 7 8class Item extends Model 9{ 10 protected $guarded = array('id'); 11 12 protected $table = 'items'; 13 14} 15
- それぞれのテーブルです
PHP
1//usersテーブル 2 3 public function up() 4 { 5 Schema::create('users', function (Blueprint $table) { 6 $table->increments('id'); 7 $table->string('name'); 8 $table->string('email')->unique(); 9 $table->timestamp('email_verified_at')->nullable(); 10 $table->string('password'); 11 $table->rememberToken(); 12 $table->timestamps(); 13 }); 14 } 15
PHP
1//itemsテーブル 2 public function up() 3 { 4 Schema::create('items', function (Blueprint $table) { 5 $table->increments('id'); 6 $table->string('name'); 7 $table->string('manufacturer'); 8 $table->integer('itemtype'); 9 $table->string('material'); 10 $table->integer('weight'); 11 $table->integer('model_year'); 12 $table->string('link'); 13 $table->timestamps(); 14 }); 15 }
- アイテムをお気に入り一覧に表示させるときは、naem,manufacturer,weightだけを表示させたいと思っています
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。