開発環境
Laravel 5.8.26
困っている事
私は現在、LaravelでEC-CUBE4のSQLを使用し、以下の様なリレーションを作り、dtb_productテーブルと一対一のdtb_product_imageテーブルを取得しようとしています。
dtb_customerテーブルからdtb_productテーブルへの多対多リレーションは実装できたのですが、dtb_product_imageテーブルを取得する事ができません。
試した事
belongsToManyメソッドにhasOneメソッドを組み合わせてコードを作成したのですが、下記のエラーが表示されました。
Customer.php
PHP
1<?php 2 3namespace App; 4 5use Laravel\Passport\HasApiTokens; 6use Illuminate\Notifications\Notifiable; 7use Illuminate\Foundation\Auth\User as Authenticatable; 8 9class Customer extends Authenticatable 10{ 11 use HasApiTokens, Notifiable; 12 13 /** 14 * The table associated with the model. 15 * 16 * @var string 17 */ 18 protected $table = 'dtb_customer'; 19 20 protected $with = ['ticket']; 21 22 public function ticket() 23 { 24 $ticket = $this->belongsToMany( 25 'App\Product', 26 'dtb_ticket', 27 'customer_id', 28 'ticket_id' 29 ); 30 $ticket_all = $ticket->hasone('App\Product_Image', 'product_id'); 31 return $ticket_all; 32 } 33 34 /** 35 * The attributes that are mass assignable. 36 * 37 * @var array 38 */ 39 protected $fillable = [ 40 'name', 'email', 'password', 41 ]; 42 43 /** 44 * The attributes that should be hidden for arrays. 45 * 46 * @var array 47 */ 48 protected $hidden = [ 49 'password', 'remember_token', 50 ]; 51 52 /** 53 * The attributes that should be cast to native types. 54 * 55 * @var array 56 */ 57 protected $casts = [ 58 'email_verified_at' => 'datetime', 59 ]; 60}
エラー
error
1Illuminate\Database\Eloquent\RelationNotFoundException: Call to undefined relationship [ticket] on model [App\Customer]. in file /var/www/html/siro/vendor/laravel/framework/src/Illuminate/Database/Eloquent/RelationNotFoundException.php on line 34
dtb_product_imageテーブルのfile_nameカラムを取得するには何処を修正すればいいのでしょうか。
追記
エンティティに以下のコードを追加しましたが、取得する事はできませんでした。
お手数ですが問題点を教えて頂けないでしょうか。
Product.php
php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Product extends Model 8{ 9 protected $table = 'dtb_product'; 10 11 public function image() 12 { 13 return $this->hasone('App\Product_Image', 'product_id', 'id'); 14 } 15}
Product_Image.php
php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Product_Image extends Model 8{ 9 protected $table = 'dtb_product_image'; 10}
AuthController.php
php
1<?php 2 3namespace App\Http\Controllers\Auth; 4use App\User; 5use Carbon\Carbon; 6use Illuminate\Http\Request; 7use App\Http\Controllers\Controller; 8use Illuminate\Support\Facades\Auth; 9class AuthController extends Controller 10{ 11 public function login(Request $request) { 12 $request->validate([ 13 'email' => 'required|string|email', 14 'password' => 'required|string', 15 //'remember_me' => 'boolean' 16 ]); 17 $credentials = request(['email', 'password']); 18 if(!Auth::attempt($credentials)) 19 return response()->json([ 20 'message' => 'Unauthorized' 21 ], 401); 22 $user = $request->user(); 23 $tokenResult = $user->createToken('Personal Access Token'); 24 $token = $tokenResult->token; 25 if ($request->remember_me) 26 $token->expires_at = Carbon::now()->addWeeks(1); 27 $token->save(); 28 return response()->json([ 29 'access_token' => $tokenResult->accessToken, 30 'token_type' => 'Bearer', 31 'expires_at' => Carbon::parse( 32 $tokenResult->token->expires_at 33 )->toDateTimeString() 34 ]); 35 } 36 37 public function logout(Request $request) 38 { 39 $request->user()->token()->revoke(); 40 return response()->json([ 41 'message' => 'Successfully logged out' 42 ]); 43 } 44 45 /** 46 * Get the authenticated User 47 * 48 * @return [json] user object 49 */ 50 public function user(Request $request) 51 { 52 return response()->json($request->user()); 53 } 54}

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/27 04:17
退会済みユーザー
2019/08/27 04:29
2019/08/27 05:18
退会済みユーザー
2019/08/27 05:28
退会済みユーザー
2019/08/27 05:29
2019/08/27 10:21
2019/08/27 20:08