Retrieve all elements on laravel collection - laravel

I've just started using Laravel. I'm currently creating some sort of "Instagram" app as an example. So I have 4 tables. Users, Images, Comments and Likes. I've already created the "Models" using the php artisan command make:model and included some data to the database. The thing is, after creating the relationships of the models like.
use App\Image;
class Comment extends Model
{
protected $table = 'comments';
protected $primaryKey = 'CommentId';
public function user()
{
return $this->belongsTo('App\User', 'UserId');
}
public function image()
{
return $this->belongsTo('App\Image', 'ImageId');
}
}
And doing the same thing on the Image Model
class Image extends Model
{
protected $table = 'images';
protected $primaryKey = 'ImageId';
public function comments()
{
return $this->hasMany('App\Comment', 'CommentId');
}
public function likes()
{
return $this->hasMany('App\Like', 'LikeId');
}
public function user()
{
return $this->belongsTo('App\User', 'UserId');
}
I can't get all the comments on an image, after executing:
Route::get('/', function () {
$images = Image::all();
foreach ($images as $image) {
echo "Uploaded By: ".$image->user->FirstName."<br>";
echo "<strong>Comments</strong><br>";
foreach ($image->comments as $comment)
{
echo "Comment: ".$comment->Content;
}
}
die();
return view('welcome');
});
I can only get one element on each image, even though the image has more than 1 comment.

i think the problem is in your comments relation:
public function comments()
{
return $this->hasMany('App\Comment', 'CommentId');
}
it should be:
public function comments()
{
return $this->hasMany('App\Comment', 'ImageId');
}
the second parameter in hasMany relation should be the foreign key that relate the two table in data base.
more details in:
https://laravel.com/docs/7.x/eloquent-relationships#one-to-many

Related

Has Many through relationship laravel didnot solve

I Have
Worker -> HasOne-> Document ->morphMany ->FIles
->HasOne-> Medical ->morphMany ->FIles
->HasOne-> Course ->morphMany ->FIles
I want to use hasManyTrough to get files data from worker, I defined
public function medical_detail()
{
return $this->hasOne(MedicalDetail::class);
}
Tried this :
public function document_files()
{
return $this->hasManyThrough(File::class, Document::class);
}
In worker but It expects document_id, how can I use ir for polymirhic relationship?
Also did this
public function document_files()
{
return $this->hasManyThrough(
File::class,
Document::class,
'worker_id',
'filable_id',
'id',
'id'
);
}
THese are my models
File model
protected $fillable = ['file_type', 'file_name', 'fiable_id', 'filable_type'];
public function filable()
{
return $this->morphTo();
}
Worker MOdel
public function course()
{
return $this->hasOne(Course::class);
}
public function document()
{
return $this->hasOne(Document::class);
}
Document model
protected $fillable = ['doc_name', 'worker_id'];
public function worker()
{
return $this->belongsTo(Worker::class);
}
public function files()
{
return $this->morphMany(File::class, 'filable');
}
This is one alternative I added, but gives me No query results for model [App\\Models\\Worker] 1 . SInce files table has $table->morphs('filable'); This defined so I added filable_id. These are my models: docuemnt,file

Blank object in Eloquent belongsTo()

I'm trying to display which one attribute (code) of Item. ServiceItem has Item as a foreign key. But I can't get the Item at all.
This one gives a blank object in blade template:
#foreach ($service->serviceItems as $serviceItem )
{{ json_encode($serviceItem->item()) }}
#endforeach
Here's my model declaration:
//ServiceItem model
class ServiceItem extends Model
{
use HasFactory;
public $fillable = ['service_id', 'item_id', 'values'];
public function service()
{
return $this->belongsTo(Service::class, 'foreign_key');
}
// this doesn't work
public function item()
{
return $this->belongsTo(Item::class, 'foreign_key');
}
}
// Service model
class Service extends Model
{
use HasFactory;
public $fillable = ['user_id', 'site_id', 'title', 'status', 'remarks', 'report', 'date'];
public function user()
{
return $this->belongsTo('\App\Models\User');
}
public function site()
{
return $this->belongsTo('\App\Models\Site');
}
public function serviceItems() {
return $this->hasMany('\App\Models\ServiceItem');
}
}
This is my controller:
public function index()
{
$services = Service::latest()->paginate(5);
return view('services.index', compact('services'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
Please help me to display the code attribute in Item from Service!!! Thanks a lot!
I suppose you read the Laravel doc of model relationship definition. They referenced to put foreign key as the second parameter, not the foreign_key as word but your actual foreign key that reference the parent table. you have to change the model code.
class ServiceItem extends Model
{
use HasFactory;
public $fillable = ['service_id', 'item_id', 'values'];
public function service()
{
return $this->belongsTo(Service::class, 'service_id');
}
public function item()
{
return $this->belongsTo(Item::class, 'item_id');
}
}
and then $serviceItem->item should work as expected.

Laravel eloquent multiple levels of relations

Alright so I am basically trying to retrieve all animal_registry codes based on a user ID.
Idea is that
1 user has many jobs.
Jobs are consisted of many "Jobs data".
Jobs data has many "Animal registry" entries.
These are my relations
Image relations link (click)
And these are my relations in Laravel
class User
{
public function jobs()
{
return $this->hasMany('App\Models\RegistryJobs', 'employee', 'id');
}
}
class RegistryJobs extends Model
{
protected $table = "registry_jobs";
protected function jobsData()
{
$this->hasManyThrough('App\Models\AnimalRegistry', 'App\Models\RegistryJobsData', 'id', 'animal_registry_id');
}
}
class RegistryJobsData extends Model
{
protected $table = "registry_jobs_data";
public function jobs()
{
$this->belongsTo('App\Models\RegistryJobs', 'id', 'registry_jobs_id');
}
public function animals()
{
$this->hasMany('App\AnimalRegistry', 'id', 'animal_registry_id');
}
}
class AnimalRegistry extends Model
{
protected $table = "animal_registry";
}
And now I am trying to query it from a controller in a way
$data = User::whereHas('jobs', function ($query) {
$query->where('id', 1);
})->get();
But I am unable to access the properties from the animal_registry.
Can you try like this :
public function animals(){
return $this->hasManyThrough('App\Registry_Jobs_data','App\Registry_Jobs', 'employee',
'registry_jobs_id', 'id' ,'id')->join('//Do the joining')->select();
}
Check the hasManyThrough i am not sure..

How to get pivot data depends model binding

I am using in Pivot model. In previously i assigned user_id is routekey but now i want to match model binding flats (flat_id) also. I have attached the output screenshot too.
My url is http://127.0.0.1:8000/admin/apartments/1/flats/2/flat-members/3
namespace App\pivotes;
use Illuminate\Database\Eloquent\Relations\Pivot;
use App\models\RoleUser;
use App\models\Flat;
use App\models\Association;
use App\models\Role;
use App\User;
class FlatUser extends Pivot
{
protected $table = 'flat_user';
public $timestamps = false;
public function getRouteKeyName()
{
return 'user_id';
}
public function flat()
{
return $this->belongsTo(Flat::class, 'flat_id', 'id');
}
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
public function role()
{
return $this->belongsTo(Role::class, 'role_id', 'id');
}
}
Update I tired this
In RouteServiceProvider
public function boot()
{
parent::boot();
Route::bind('flat_member', function ($value) {
dd(\App\pivotes\FlatUser::where('flat_id', request()->route()->parameter('flat'))->where('user_id', request()->route()->parameter('flat_member'))->first());
return \App\pivotes\FlatUser::where('flat_id', request()->route()->parameter('flat'))->where('user_id', request()->route()->parameter('flat_member'))->first() ?? abort(404);
});
}
for dd() I am getting correct data, but if I am returning it, it seems to be becoming empty.
I am using RouteServiceProvider.php. I check flat_id and route parameter flats whether it matches, giving particular pivot model data. In below added my codes.
public function boot()
{
parent::boot();
Route::bind('flat_member', function ($value) {
return \App\pivotes\FlatUser::where('flat_id', request()->route()->parameter('flat'))->where('user_id', request()->route()->parameter('flat_member'))->first() ?? abort(404);
});
}

Laravel scout check if relation is not empty?

namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Searchable;
class Event extends Model
{
protected $table = 'events';
public $timestamps = true;
use Searchable;
use SoftDeletes;
protected $dates = ['deleted_at'];
public function entities()
{
return $this->belongsTo('App\Entity', 'entity_id');
}
public function users()
{
return $this->belongsTo('App\User', 'id');
}
public function events()
{
return $this->belongsTo('App\DirtyEvent', 'id');
}
public function toSearchableArray()
{
$data = $this->toArray();
$data['entities'] = $this->entities->toArray();
return $data;
}
}
This is my model for Event, as you can see I am using toSearchableArray which is Laravel scout function to import 'relations' to algolia. However the problem is that sometimes it is empty. So for example
event id 1 has entity_id 1
but in another example
event id 2 has entity_id = null
How can I modify this function to check if the entities() relation is not empty before putting it into array?
if i understand u correctly this should help. if the relationship does not exist return an empty array and scout won't update the index
public function toSearchableArray()
{
if(is_null($this->entities)){
return [];
}
$this->entities
return $this->toArray();
}
please update foreign_key in relation as this
user_id as foreign_key instead of id
event_id as foreign_key instead of id
public function users()
{
return $this->belongsTo('App\User', 'user_id');
}
public function events()
{
return $this->belongsTo('App\DirtyEvent', 'event_id');
}
I think if load the relation before the toArray().
public function toSearchableArray()
{
$this->entities;
return $this->toArray();
}

Resources