I have an MorphTo relationship. can't work function inside the morphWith
Error:
`<!--
TypeError: mb_strpos(): Argument #1 ($haystack) must be of type string, Closure given in file C:\wamp64\www\projects\archer-review-web\vendor\laravel\framework\src\Illuminate\Support\Str.php on line 197
0 C:\wamp64\www\projects\archer-review-web\vendor\laravel\framework\src\Illuminate\Support\Str.php(197): mb_strpos(Object(Closure), ':')
1 C:\wamp64\www\projects\archer-review-web\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(1364): Illuminate\Support\Str::contains(Object(Closure), ':')
2 C:\wamp64\www\projects\archer-review-web\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(1298): Illuminate\Database\Eloquent\Builder->parseWithRelations(Array)
3 C:\wamp64\www\projects\archer-review-web\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations\MorphTo.php(141): Illuminate\Database\Eloquent\Builder->with(Array)`
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class AnnouncementSession extends Model
{
use HasFactory;
public $timestamps = false;
protected $fillable = ['announcement_id', 'session_id', 'session_type'];
public function session(): MorphTo
{
return $this->morphTo();
}
}
Announcement::where('is_enabled', true)
->withCount('announcementProducts')
->with([
'announcementSession.session' => function ($morphTo) {
$morphTo->morphWith([
TutorSchedule::class => function ($tutorSchedule) {
$tutorSchedule->select('id', 'tutor_id')->with(['tutor:id,first_name,last_name,profile_image,qualification']);
},
]);
}
])->get()
But below code is working fine.
Announcement::where('is_enabled', true)
->withCount('announcementProducts')
->with([
'announcementSession.session' => function ($morphTo) {
$morphTo->morphWith([
TutorSchedule::class => ['tutor:id,first_name,last_name,profile_image,qualification'],
]);
}
])->get()
Top comments (0)