How to load relationship with Eloquent? - php

I have a Link model and each link has an owner (represented by ownerId in DB, which is a foreign key of the User table).
Here is the Link model :
<?php namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Link extends Model {
protected $table = "Link";
// ...
// Relationships
public function owner()
{
return $this->belongsTo(User::class, 'ownerId', 'id');
}
}
When I use $data = Link::find($linkId)->toJson(); in my LinkController, owner is included but is null in the JSON data. I also tried $data = Link::with('owner')->find($linkId)->toJson(); and $data = Link::find($linkId)->load('owner')->toJson();
And when I use $data = Link::find($linkId)->owner->toJson();, I get the user data. Is there something missing in my code?
Is there a way to load the owner in the link object and to get it via JSON without additional requests / steps ?

I finally found the issue. The primary key in my User table was not standard (it was a string key instead of int).
I added the following lines in my User model :
public $incrementing = false;
public $keyType = 'string';
And now $data = Link::with('owner')->find($linkId); is just working fine.

Related

Laravel Policy and Show Method with View Method logical Problem

I am using Policys and want to be sure that I am prevent data to be shown of other users.
In every Table I have the column 'user_id' and check if the current logged in user with his id the same with the data and his user_id.
In this specific case I have a table of Objects and Objektverwaltung where the objekt_id is given as foreign key.
I want to use my policy to be sure that just the data for the given object was shown in objektverwaltung where the foreign key 'objekt_id' is given.
ObjektVerwaltung Controller with the show method:
public function show($objektvwId) {
$objektId = ObjektVerwaltung::with('Objekt')->find($objektvwId);
$this->authorize('view', $objektId);
$objekte = ObjektVerwaltung::where('objekt_id',$objektvwId)->get();
return view('objekte.verwaltung', compact('objekte'));
}
Policy:
public function view(User $user, ObjektVerwaltung $objektVerwaltung)
{
return $objektVerwaltung->user_id === $user->id;
}
Models:
class ObjektVerwaltung extends Model
{
use HasFactory;
protected $table = 'objekte_verwaltungens';
protected $fillable = ['user_id','objekt_id','key', 'value'];
public function Objekt() {
return $this->belongsTo(Objekt::class);
}
}
class Objekt extends Model
{
use HasFactory;
protected $table = 'objekts';
protected $fillable = ['name','strasse', 'hausnummer', 'plz', 'ort', 'user_id'];
public function Mieter() {
return $this->hasMany(Mieter::class);
}
public function Company() {
return $this->belongTo(Company::class);
}
public function Objektverwaltung() {
return $this->hasMany(ObjektVerwaltung::class);
}
}
I learned that I can easily use find() as method for the Models to validate data. But in this specific case I have to check for the objekt_id (foreign key in objektverwaltung) and not for the ID and because of that I cant use find(). But if I use where or another method I cant use my policy and always getting unauthorized.
I tried to use the with method on the model but maybe there is a better way to my problem. I strongly believe.
Thanks!
This could be solution, but I am getting always "Unauthorized" and do not get to the policy: $objekt= ObjektVerwaltung::where('objekt_id', $objektId)->get(); $this->authorize('view', $objekt);
I solved this issue. I had to use my ObjektPolicy, because I am using the objekt_id Key.

Using hasone function with constructed key

I'm new to Laravel (but not PHP).
I have two database files (which cannot be modified).
One file (salesorderlines) has two keys ordn55 and ordl55 which are an Id and a sequence number. The other file (certificate) combines these two keys with a '-' between them and the leading zeros on the sequence number.
I can construct the key to this file using the following code:
$this->ordn55.'-'. sprintf('%03s', $this->ordl55);
which works fine.
However I can't do this with a hasone() function call.
Is this possible to do with Laravel Eloquent? If not, what if the best way to do it.
Here is more detailed code for this:
SalesOrderLine model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class SalesOrderLine extends Model
{
protected $connection = 'ibmi';
public $incrementing = false;
protected $table = 'ault2f1.oep55';
protected $primaryKey = 'ordn55';
protected $appends = ['combinedkey'];
public $timestamps = false;
public function salesorder()
{
return $this->hasOne(SalesOrderHeader::class, 'ordn40', 'ordn55')->where('cono40', 'AM');
}
public function item()
{
return $this->hasOne(ItemMaster::class, 'pnum35', 'catn55')->where('cono35', 'AM');
}
public function certificate()
{
return $this->hasOne(Certificate::class, 'order_no', 'combinedkey')->where('cono70', 'AM');
}
public function getCombinedkeyAttribute()
{
return $this->ordn55.'-'. sprintf('%03s', $this->ordl55);
}
}
You can see that I have tried to achieve this using a combinedkey property that I have added to the $appends array but this doesn't seem to work.
Here is the code to run the query for this
$salesorder = SalesOrderHeader::query()
->with('salesorderlines', 'salesorderlines.item')
->with('salesorderlines', 'salesorderlines.certificate')
->join('ault1f1.slp05', 'ault1f1.slp05.cusn05', 'cusn40')
->where('cono40', 'AM')
->where('cono05', 'AM')
->where('ordn40', $id)
->first();
The item section works correctly as it doesn't need a constructed key for the foreign key like the Certificate does.
Hopefully this makes sense.
Thanks in advance for any help

Special UUID primary key for laravel eloquent

I have a users table that doesn't use an auto increment primary key, instead it uses a binary uuid primary key. I set up my custom model to interact with my table however, I'm having trouble trying to find records by using ::find() because for a case like this, this uuid needs to searched by using HEX() and UNHEX() mysql functions. How to I override this and have whatever is in ::find() to be hexed. The name of the model is Player.
So if I was to try to find a user with a uuid of 9d823b9eec224cbea10b69bec2c5adef, I cannot find them by doing:
Player::find('9d823b9eec224cbea10b69bec2c5adef') since the uuid needs to be unhexed.
I've tried Player::find("UNHEX('9d823b9eec224cbea10b69bec2c5adef')"); with no results.
Here's my model so far:
class Player extends Eloquent {
protected $table = 'players';
protected $connection = 'game';
protected $primaryKey = 'uuid';
public $incrementing = false;
public $timestamps = false;
}
The datatype for uuid is binary(16)
Update
I've got it to work by using Player::find(DB::raw("UNHEX('9d823b9eec224cbea10b69bec2c5adef')")); however this is a lot to write every time I need to look up a user.
Is there any way I can have the parameter for ::find() always run through DB::raw("UNHEX('uuid')") or be passed through the function hex2bin()?
I am 100% certain I will always be using UUID so I want to override ::find(), not create a new method.
I would try to unhex it in PHP prior to passing it to mysql:
Player::find(hex2bin('9d823b9eec224cbea10b69bec2c5adef'));
You could add this method to your Player class:
public static function findUUID($uuid) {
return self::find(hex2bin($uuid));
}
Now any where in your project you can call it like:
$result = Player::findUUID('9d823b9eec224cbea10b69bec2c5adef');
If you do not want to declare it statically you can:
public function findUUID($uuid) {
return self::find(hex2bin($uuid));
}
and then reference it in your code with:
$result = new Player;
$result->findUUID('9d823b9eec224cbea10b69bec2c5adef');
This should allow you to override the native find() behavior and not have to use findUUID() instead:
protected $primaryKey = 'uuid';
public static function find($uuid)
{
return parent::find(hex2bin($uuid));
}
If you really want it like that, you can also do Player::find('9d823b9eec224cbea10b69bec2c5adef') with this
class Player extends Eloquent {
protected $table = 'players';
protected $connection = 'game';
protected $primaryKey = 'uuid';
public $incrementing = false;
public $timestamps = false;
public static function find($uuid, $columns = array('*')) {
return self::find("UNHEX('$uuid')", $columns);
}
}
EDITED
added self, as user Elliot Fehr suggested

unable to retrieve field values from object laravel

I am trying to use eloquent relationships to retrieve some data from my DB. I am running into an issue when trying to get specific fields rather than the full object. Here is some of my code.
Controller:
public function getHosts($table = null)
{
foreach (Hosts::all() as $host)
{
echo $host->physical_machine_name;
echo $host->app->id;
}
}
models:
class Hosts extends Eloquent
{
protected $connection = 'mysql_2';
protected $table = 'hosts';
public $timestamps = false;
public function app(){
return $this->hasMany('Apps', 'os_instance_name')->orWhere('os_instance_name', $this->os_instance_name);
}
}
class Apps extends Eloquent
{
protected $connection = 'mysql_2';
protected $table = 'app_instances';
public $timestamps = false;
public function host()
{
return $this->belongsTo('Hosts','os_instance_name')->orWhere('os_instance_name', $this->os_instance_name);
}
}
I am not able to get the id of the app to display but when I remove the '->id' specifier i am able to get a json object containing all of the fields. Why is this happening?
I should also note that the $host->physical_machine_name works fine.
this is the error I receive:
Undefined property: Illuminate\Database\Eloquent\Collection::$id
I am also unable to use eager loading because I am using custom foreign keys in my relationships.
You are defining apps as a hasMany relationship. This means apps will be a collection of entities, not a single one, hence the Illuminate\Database\Eloquent\Collection instance, as Laravel uses this class for lists of entities populated by Eloquent.
Being a collection of entities you won't have any properties of the App entity on it.
Instead of echo $host->app->id; you can
foreach($host->app as $app) {
echo $app->id;
}

Add a custom attribute to a Laravel / Eloquent model on load?

I'd like to be able to add a custom attribute/property to an Laravel/Eloquent model when it is loaded, similar to how that might be achieved with RedBean's $model->open() method.
For instance, at the moment, in my controller I have:
public function index()
{
$sessions = EventSession::all();
foreach ($sessions as $i => $session) {
$sessions[$i]->available = $session->getAvailability();
}
return $sessions;
}
It would be nice to be able to omit the loop and have the 'available' attribute already set and populated.
I've tried using some of the model events described in the documentation to attach this property when the object loads, but without success so far.
Notes:
'available' is not a field in the underlying table.
$sessions is being returned as a JSON object as part of an API, and therefore calling something like $session->available() in a template isn't an option
The problem is caused by the fact that the Model's toArray() method ignores any accessors which do not directly relate to a column in the underlying table.
As Taylor Otwell mentioned here, "This is intentional and for performance reasons." However there is an easy way to achieve this:
class EventSession extends Eloquent {
protected $table = 'sessions';
protected $appends = array('availability');
public function getAvailabilityAttribute()
{
return $this->calculateAvailability();
}
}
Any attributes listed in the $appends property will automatically be included in the array or JSON form of the model, provided that you've added the appropriate accessor.
Old answer (for Laravel versions < 4.08):
The best solution that I've found is to override the toArray() method and either explicity set the attribute:
class Book extends Eloquent {
protected $table = 'books';
public function toArray()
{
$array = parent::toArray();
$array['upper'] = $this->upper;
return $array;
}
public function getUpperAttribute()
{
return strtoupper($this->title);
}
}
or, if you have lots of custom accessors, loop through them all and apply them:
class Book extends Eloquent {
protected $table = 'books';
public function toArray()
{
$array = parent::toArray();
foreach ($this->getMutatedAttributes() as $key)
{
if ( ! array_key_exists($key, $array)) {
$array[$key] = $this->{$key};
}
}
return $array;
}
public function getUpperAttribute()
{
return strtoupper($this->title);
}
}
The last thing on the Laravel Eloquent doc page is:
protected $appends = array('is_admin');
That can be used automatically to add new accessors to the model without any additional work like modifying methods like ::toArray().
Just create getFooBarAttribute(...) accessor and add the foo_bar to $appends array.
If you rename your getAvailability() method to getAvailableAttribute() your method becomes an accessor and you'll be able to read it using ->available straight on your model.
Docs: https://laravel.com/docs/5.4/eloquent-mutators#accessors-and-mutators
EDIT: Since your attribute is "virtual", it is not included by default in the JSON representation of your object.
But I found this: Custom model accessors not processed when ->toJson() called?
In order to force your attribute to be returned in the array, add it as a key to the $attributes array.
class User extends Eloquent {
protected $attributes = array(
'ZipCode' => '',
);
public function getZipCodeAttribute()
{
return ....
}
}
I didn't test it, but should be pretty trivial for you to try in your current setup.
I had something simular:
I have an attribute picture in my model, this contains the location of the file in the Storage folder.
The image must be returned base64 encoded
//Add extra attribute
protected $attributes = ['picture_data'];
//Make it available in the json response
protected $appends = ['picture_data'];
//implement the attribute
public function getPictureDataAttribute()
{
$file = Storage::get($this->picture);
$type = Storage::mimeType($this->picture);
return "data:" . $type . ";base64," . base64_encode($file);
}
Step 1: Define attributes in $appends
Step 2: Define accessor for that attributes.
Example:
<?php
...
class Movie extends Model{
protected $appends = ['cover'];
//define accessor
public function getCoverAttribute()
{
return json_decode($this->InJson)->cover;
}
you can use setAttribute function in Model to add a custom attribute
Let say you have 2 columns named first_name and last_name in your users table and you want to retrieve full name. you can achieve with the following code :
class User extends Eloquent {
public function getFullNameAttribute()
{
return $this->first_name.' '.$this->last_name;
}
}
now you can get full name as:
$user = User::find(1);
$user->full_name;
In my subscription model, I need to know the subscription is paused or not.
here is how I did it
public function getIsPausedAttribute() {
$isPaused = false;
if (!$this->is_active) {
$isPaused = true;
}
}
then in the view template,I can use
$subscription->is_paused to get the result.
The getIsPausedAttribute is the format to set a custom attribute,
and uses is_paused to get or use the attribute in your view.
in my case, creating an empty column and setting its accessor worked fine.
my accessor filling user's age from dob column. toArray() function worked too.
public function getAgeAttribute()
{
return Carbon::createFromFormat('Y-m-d', $this->attributes['dateofbirth'])->age;
}

Categories