Can someone please tell me how can I achieve something in a Laravel Model?
One of the columns contains json_encoded data:
["1", "7", "13", "18"]
I have a model for this specific table and when I'm trying to get the data from different tables based on those ids, it won't work as variable is always null:
use App\Models\VehiculeType;
...
class Freight extends Model
{
...
public function selected_vt() {
return VehiculeType::whereIn('id', json_decode($this->vehicules))
->get();
}
}
Then in the controller:
$freights = Freight::with('selected_vt')
->where('user_id', Auth::user()->id)
->get();
And I'm getting this error:
Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
I am sure I'm making some fundamental error trying to do this in the completely wrong way.
Unfortunately you can't use with with collection, and selected_vt must return relation instance rather than a collection.
Try eloquent-json-relations package, it can helps you with your situation:
https://github.com/staudenmeir/eloquent-json-relations
Here is an example depending your situation
After installing package
in Freight Modle
class Freight extends Model
{
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
protected $casts = [
'vehicules' => 'json'
];
public function vehicules()
{
return $this->belongsToJson(VehiculeType::class, 'vehicules');
}
}
If you want the reversed relation
in VehiculeType Model
class VehiculeType extends Model
{
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
public function freights()
{
return $this->hasManyJson(Freight::class, 'vehicules');
}
}
Then in the controller:
$freights = Freight::with('vehicules')
->where('user_id', Auth::user()->id)
->get();
Related
I Want To Use Relation Methods In Scopes But It Gives an Error.
Error:
Call to undefined method Illuminate\Database\Eloquent\Builder::members()
Controller:
$members = $book->MembersLoanedCurrentBook()->paginate(8);
Scope:
public function scopeMembersLoanedCurrentBook(Builder $query): Builder
{
return $query->members()->orderBy('return_date')->where('book_member.returned',false);
}
Assuming your models are something akin to User hasMany BookMember and Book has an attribute called returned, you can use Laravel's with` query scope:
Users::with(['member_books', function ($q) => {
$q->returned
})->get();
#geertjanknapen was right that this is a possible duplicate. You can achieve the same result using the methods from this question.
What you are doing is defining a scope and in that scope querying a relationship for a specific property or value.
public function scopeMembersLoanedCurrentBook(Builder $query): Builder
{
return $query->members()
->orderBy('return_date')
->whereHas(['book', function ($q) => {
$q->returned == false;
});
});
}
Without knowing the model structure and relationships, it's hard to write out an exact solution, but something along these lines should work.
You can't work with relations in scope, because your work with Builder $query.
public function scopeMembersLoanedCurrentBook(Builder $query): Builder
{
return $query->orderBy('return_date')
->where('returned',false);
}
And
$members = $book->members()->MembersLoanedCurrentBook()->paginate(8);
I created an Eloquent Model :
class VehicleDetails extends Model
{
//
protected $table = 'v_vehicle_details';
protected $primaryKey = 'model_id';
public function tariffs()
{
return $this->hasMany('App\Tariffs', 'vehicle_model_id', 'model_id');
}
}
The table structure for the same is v_vehicle_details is
v_vehicle_details
The table structure for tariffs is
t_tariffs
The Model is being called in controller like :
public function booking_view(){
$vehicle_details = new VehicleDetails();
return $vehicle_details->find(5)->tariffs();
}
What I need is to get vehicle details with all tariffs, But when I try that it throws an error Illuminate\Database\Eloquent\Relations\HasMany could not be converted to string. Can somebody please help, I am new to laravel.
These are not actual tables, But views.
change to
return VehicleDetails::with('tariffs')->find(5);
This is because calling the method ->tarrifs() will return a relationship object in this case HasMany this means you still have to perform the query on this object to get the results.
If you use it as a property ->tarifs without () it will perform the query.
This is the same as tarifs()->get() just a shortcut.
Change your function to:
public function bookingView(){
$vehicle = VehicleDetails::with('tariffs')->find(5);
return view('your.view', compact('vehicle'));
}
The with() will eager load the tariffs relation.
You can acess your tariffs in your view like this:
{{$vehicle->tariffs->anyAttributeYouWantToAccess}}
I've got Tag and Attendee Eloquent models, they are in many-to-many relation. Pivot table has also two more attributes – value_int and value_string. My Attendee model looks like this:
class Attendee extends Model
{
public $timestamps = false;
protected $fillable = [
'event_id'
];
public function tags() {
return $this->belongsToMany('App\Models\Tag', 'attendee_tag', 'attendee_id', 'tag_id')
->withPivot(['value_string', 'value_int']);
}
public function scoreTagValue($tag_id) {
return $this->tags->where('tag_id', '=', $tag_id)->first();
}
}
What I want is to obtain pivot values based on Attendee model and variable tag_id, so I've written scoreTagValue function, but it always returns null and I don't know why :( I'm calling it this way:
$attendee->scoreTagValue($tag_id). Thanks for your help :)
You need to access the relation, not the property:
public function scoreTagValue($tag_id) {
return $this->tags()->where('tag_id', '=', $tag_id)->first();
}
Also, according to the docs, withPivot() does not take an array, so:
->withPivot('value_string', 'value_int');
I am using Laravel 5.2 and have a problem.
My code is;
$sts = STSMember::find($member_id)->join('rating', 's_t_s_members.member_id', '=', 'rating.member_id');
But I get the following error
Object of class Illuminate\Database\Eloquent\Builder could not be converted to string'.
How to get member_id from two table?
find returns a model instance, you need either:
STSMember::where("id","=",$member_id)->join('rating', 's_t_s_members.member_id', '=', 'rating.member_id')->get();
Or ideally:
class STSMember extends Model {
//Other model code
public function rating() {
return $this->belongsTo(Rating::class);
}
}
Then you can do:
STMember::with("rating")->find($member_id);
Check https://laravel.com/docs/5.4/eloquent-relationships
Suppose I have a Course model like this :
class Course extends Model
{
public $primaryKey = 'course_id';
protected $appends = ['teacher_name'];
public function getTeacherNameAttribute ()
{
$this->attributes['teacher_name'] = $this->teacher()->first()->full_name;
}
public function teacher ()
{
return $this->belongsTo('App\User', 'teacher', 'user_id');
}
}
And in the other hand there is a User model like this :
class User extends Authenticatable
{
public $primaryKey = 'user_id';
protected $appends = ['full_name'];
public function getFullNameAttribute ()
{
return $this->name . ' ' . $this->family;
}
public function course ()
{
return $this->hasMany('App\Course', 'teacher', 'user_id');
}
}
As you can see there is a hasMany relationship between those.
There is an full_name accessor in User model.
Now I want to add a teacher_name accessor to Course model that uses it's teacher relations and gets full_name of teacher and appends to Course always.
In fact I want whenever call a Course model, it's related teacher name included like other properties.
But every time , when call a Course model , I got this error :
exception 'ErrorException' with message 'Trying to get property of non-object' in D:\wamp\www\lms-api\app\Course.php:166
That refers to this line of Course model :
$this->attributes['teacher_name'] = $this->teacher()->first()->full_name;
I do not know how can I solve that and what is problem exactly.
Yikes some interesting answers here.
FYI to those coming after me- getFooAttribute() should return the data, and not modify the internal attributes array.
If you set a new value in the attributes array (that doesnt exist in this model's db schema) and then attempt to save the model, you'll hit a query exception.
It's worth reading up the laravel docs on attribute accessors/mutators for more info.
Furthermore, if you need to access a related object from within the model (like in an accessor) you ought to call $related = $this->getRelation('foo'); - note that if the relation isnt loaded (e.g., if you didnt fetch this object/collection with eager loaded relations) then $this->getRelation() could return null, but crucially if it is loaded, it won't run the same query(ies) to fetch the data again. So couple that with if (!$this->relationLoaded('foo')) { $this->loadRelation('foo'); }. You can then interact with the related object/collection as normal.
$this->attributes['teacher_name'] = $this->teacher()->first()->full_name;
Should be
$this->attributes['teacher_name'] = $this->teacher->full_name;
First thing is that you want to reference the relationship, so loose the brackets (), and because the relationship is belongsTo, you will have one user / teacher returned. So you don't need the first().
We haven't seen your fields but probably you will have to change:
return $this->belongsTo('App\User', 'teacher', 'user_id');
to
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
where foreign_key and other_key are the primary keys that you need to make the join on.
Check this link from the documentation for reference:
https://laravel.com/docs/5.4/eloquent-relationships#one-to-many-inverse
the right way to do this is:
COURSE
public function setTeacherNameAttribute ()
{
$this->attributes['teacher_name'] = $this->teacher->full_name;
}
100% working for me.
I have one to one relationship between Order and Shipment. I have to add the accessor of shipments table column from orders table.
function getOrderNoAttribute()
{
$appendText = "OR100";
if($this->orderShipment()->first()) {
$appendText = $this->orderShipment()->first()->is_shipping === 1 ? "ORE100" : "OR100";
}
return $appendText . $this->attributes['id'];
}
This error is only object data to array use or array data to object data use.
example::
$var->feild insted of $var[feild]
$var[feild] insted of $var->feild
You should use return for accessors . something like this :
public function getTeacherNameAttribute ()
{
return $this->teacher()->first()->full_name ?? '';
}
maybe a course hasn't teacher.