I have made relation between Buku and Guest. I want to add link download ebook in guest's email. Field link_dl in table Buku.
In common\models\Buku
public function getGuest()
{
return $this->hasOne(Guest::className(), ['id_buku' => 'id_buku']);
}
In common\models\Guest
public function getBuku()
{
return $this->hasMany(Buku::className(), ['id_buku' => 'id_buku']);
}
In Controller
if($model->load(Yii::$app->request->post()))
{
$value = Yii::$app->mailer->compose()
->setFrom('myaccount#gmail.com')
->setTo($model->email)
->setSubject('Download Ebook')
->setTextBody($model->buku->link_dl) //link download ebook $model->relationName->field_name
->send();
$model->save();
But I get an error,
exception 'yii\base\ErrorException' with message 'Trying to get property of non-object'
What is the issue in here? Thankyou. It's Solved
Your relation getBuku is declared as One-to-Many, so when you access buku with $model->buku you get array of Buku models.
If it's really a relation of type One-to-Many you have to choose (with any kind of logic, or simple get the 0 index) the Buku model you need like:
->setTextBody($model->buku[0]->link_dl)
If the relation suppose to be One-to-One, you need to change the relation in Guest model and then to access link_dl as in your code:
// common\models\Guest
public function getBuku()
{
return $this->hasOne(Buku::className(), ['id_buku' => 'id_buku']);
}
// Controller
->setTextBody($model->buku->link_dl)
Related
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}}
In my web app, users will be able to save some parsed content to a table called field_results.
However, this model needs to reference two polymorphic relationships:
FieldResults.php:
// A field result morphs to either a document field or email field.
public function fieldable()
{
return $this->morphTo();
}
// A field result morphs to either a document or email.
public function typeable()
{
return $this->morphTo();
}
The fieldable refers to:
DocumentFields
EmailFields
and the typeable refers to:
Documents
Emails
In my DocumentField and EmailField models, I have defined this relationship:
// A field will have a final result.
public function result()
{
return $this->morphOne(FieldResult::class, 'fieldable');
}
And in my Document and Email models, I have defined this:
public function results()
{
return $this->morphMany(FieldRuleResult::class, 'typeable');
}
So consider below loop, where I need to save some content, that will reference a specific documentfield/emailfield and a specific document/email.
foreach ($document->fields as $field) {
$content = $field->content->text;
//Save the content to `FieldResult
$field->result()->create(['content' => $content]);
}
Above will only set the values for the fieldable morph:
SQLSTATE[HY000]: General error: 1364 : Field 'typeable_type' doesn't have a default value [...]
If I change it to save to the document instead, I get below error::
//Save the content to `FieldResult
$document->result()->create(['content' => $content]);
SQLSTATE[HY000]: General error: 1364 Field 'fieldable_type' doesn't have a default value [...]
How can I do, so my model FieldResult can have two polymorphic relationships?
You can solve this problem by replacing this line:
$field->result()->create(['content' => $content]);
By the following lines:
$field->result()->create([
'content' => $content,
'typeable_id' => $document->id,
'typeable_type' => get_class($document),
]);
I am trying to make a one-to-many relationship, but I get the following error
Undefined property: stdClass::$client (View:
C:\wamp\www\intranet\resources\views\users\list.blade.php)
The problem is that I am working with an existing database that in the tables does not have id fields, and the foreign keys would also be the typical ones like client_id
My model Client.php
class Client extends Model
{
protected $connection = 'dpnmwin';
protected $table = 'nmundfunc';
public function employee(){
return $this->hasMany('App\Employee');
}
}
My model Employee.php
class Employee extends Model
{
protected $connection = 'dpnmwin';
protected $table = 'nmtrabajador';
public function client(){
return $this->belongsTo('App\Client', 'COD_UND');
}
}
In nmtrabajador COD_UND field would be the foreign key that relates to nmundfunc.
And I try to get the data out like this: {{$user->client->CEN_DESCRI}}.
but it does not throw me the error, how can I solve it?
My Controller where I send in sight
public function index(){
$users = DB::connection('dpnmwin')->table('nmtrabajador')->where('CONDICION', '=', 'A')->get();
return view('users.list',array(
'users' => $users
));
}
You have to call basis on relations.
This code will return you data.
If you have id then you can find by id like below
$employee=Employee::find(1);
Or if you want to fetch all data then you can call all method.
Employee::all();
And then you can just get it by relation as you define in models.
$client=$employee->client->CEN_DESCRI;
Retrieving data from the instance is based on the methods which we have use.
Here in this answer, you can get that
Property [title] does not exist on this collection instance
I hope it will work.
If table doesn't have 'id' as primary key you should specify what the primary key is inside your model:
protected $primaryKey = 'your_primary_key';
Relation looks good, after that you must make sure $user is a defined instance of Employee, because your error probably means that your instance wasn't even defined, so for example if you are using list.blade.php, you need to change the return of your controller and indicate that you want to pass data to your view, for example you could do it like this:
return view('users.list', compact('user'));
Where user is an instance of Employee saved on '$user'
Update
First you should check your user is retrieved properly, you can check it by placing a dd($user)
And when you return a view you can pass information to it, a cleaner way of doing what you are trying to do is what I wrote earlier so you would end up having something like this:
public function index()
{
$users = DB::table('nmtrabajador')
->where('CONDICION', '=', 'A')
->get();
// dd($user) for debugging you are retrieving the user properly
return view('users.list', compact($users));
}
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.
When creating an entry using create() - Why does it return a relationship of pilot_id table instead of just showing the value of pilot_id?
For example, in the repository class:
public function createFlight()
$flight = App\Flight::create([
'name' => 'Flight 10'
'pilot_id' => 4
]);
return $flight;
}
In the controller:
public function show()
{
$data = $this->flight->createFlight();
return $data
}
It would return json on the screen but it is not showing the relationship table (pilot) of pilot_id.
Try adding the following to your Flight model, like so. By default you need to tell Laravel to include any relationships.
protected $with = ['pilot'];
That will make it so everytime it includes the relationship. If this is not desirable, then you will want to load the relationships when you return the flight, like so.
return $flight->load(['pilot']);
It shows pilot_id is 4 because that's what its value is. Did you create a relationship on the Flight so that Laravel knows how to retrieve the model for Pilot? It should look something like this:
public function pilot()
{
return $this->hasOne('App\Pilot');
}
When you return a model directly from the controller, it invokes the toJson() method to convert the object to a string. If you want to append the contents of a related model you can do so by adding the relationship to the $with variable on the Flight model.
protected $with = ['pilot']