Q&A
前提
PHP Laravelを使用。
フォロー一覧ページを作っているところです。フォローしたユーザーをprofile/myfollowに表示し、そこに表示されたユーザーをクリックするとそのユーザーのプロフィールに飛べるようにしたいです。ProfileController.phpで全フォローを$followingsと定義し、profile/myfollowでforeachで各フォローユーザーごとに表示しようと思います。しかしfollowingsの定義で配列に誤りがあるのか以下のエラーメッセージが出ます。
実現したいこと
エラーメッセージが表示される原因を教えてください。a valid array offset type
とはどこのことを指しているのでしょうか。
発生している問題・エラーメッセージ
array_key_exists(): Argument #1 ($key) must be a valid array offset type
該当のソースコード
follow.php
1<?php 2 3namespace App\Models; 4 5use Illuminate\Database\Eloquent\Factories\HasFactory; 6use Illuminate\Database\Eloquent\Model; 7 8 9class Follower extends Model 10{ 11 use HasFactory; 12 13 protected $primaryKey = [ 14 'following_id', 15 'followed_id' 16 ]; 17 protected $fillable = [ 18 'following_id', 19 'followed_id' 20 ]; 21 public $timestamps = false; 22 public $incrementing = false; 23}
User.php
1<?php 2 3namespace App\Models; 4 5// use Illuminate\Contracts\Auth\MustVerifyEmail; 6use Illuminate\Database\Eloquent\Factories\HasFactory; 7use Illuminate\Foundation\Auth\User as Authenticatable; 8use Illuminate\Notifications\Notifiable; 9use Laravel\Sanctum\HasApiTokens; 10 11class User extends Authenticatable 12{ 13 use HasApiTokens, HasFactory, Notifiable; 14 15 /** 16 * The attributes that are mass assignable. 17 * 18 * @var array<int, string> 19 */ 20 protected $fillable = [ 21 'name', 22 'avatar', 23 'email', 24 'password', 25 'message', 26 'url' 27 ]; 28 29 /** 30 * The attributes that should be hidden for serialization. 31 * 32 * @var array<int, string> 33 */ 34 protected $hidden = [ 35 'password', 36 'remember_token', 37 ]; 38 39 /** 40 * The attributes that should be cast. 41 * 42 * @var array<string, string> 43 */ 44 protected $casts = [ 45 'email_verified_at' => 'datetime', 46 ]; 47 48 public function posts() { 49 return $this->hasMany(Posts::class); 50 } 51 public function comments() { 52 return $this->hasMany(Commentart::class); 53 } 54 public function roles() { 55 return $this->belongsToMany(Role::class); 56 } 57 public function nices() { 58 return $this->hasMany(Nice::class); 59 } 60 public function followers() 61 { 62 return $this->belongsToMany(User::class, 'followers', 'followed_id', 'following_id'); 63 } 64 public function follows() 65 { 66 return $this->belongsToMany(User::class, 'followers', 'following_id', 'followed_id'); 67 } 68}
<?php namespace App\Http\Controllers; use App\Models\User; use App\Models\Role; use App\Models\Follower; use Illuminate\Http\Request; use Illuminate\Support\Facades\Hash; use Illuminate\Validation\Rule; use Illuminate\Support\Facades\Storage; use Illuminate\Pagination\Paginator; class ProfileController extends Controller { public function followings(User $user) { $follower = auth()->user(); $followings = Follower::where('following_id', $follower->id)->get(); return view('profile.myfollow', compact('followings')); } }
試したこと
取り敢えず試しにwhereではなく、findとしてみたら
str_contains(): Argument #1 ($haystack) must be of type string, array given
となりました。全く関係なかったみたいです。
補足情報(FW/ツールのバージョンなど)
migrations\Y_m_d_t_create_followers_table.php
1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7return new class extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('followers', function (Blueprint $table) { 17 $table->unsignedInteger('following_id')->comment('フォローしているユーザID'); 18 $table->unsignedInteger('followed_id')->comment('フォローされているユーザID'); 19 20 $table->index('following_id'); 21 $table->index('followed_id'); 22 23 $table->unique([ 24 'following_id', 25 'followed_id' 26 ]); 27 }); 28 } 29 30 /** 31 * Reverse the migrations. 32 * 33 * @return void 34 */ 35 public function down() 36 { 37 Schema::dropIfExists('followers'); 38 } 39};
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/11/08 06:36
2022/11/08 06:38
2022/11/08 06:49
2022/11/08 06:55 編集
2022/11/08 10:32
2022/11/08 23:51