class Admin {
public function user()
{
return $this->morphOne('App\Models\User', 'humanable');
}
public function master()
{
return $this->hasOne('App\Models\Master');
}
}
class Master {
public function admin()
{
return $this->hasOne('App\Models\Admin');
}
}
class User {
public function humanable()
{
return $this->morphTo();
}
public function images()
{
return $this->hasOne('\App\Models\Image');
}
}
class Image {
public function user()
{
return $this->belongsTo('\App\Models\User');
}
}
Now if I dump this:
return \App\Models\Admin::where('id',1)->with(array('user.images','master'))->first();
I get the perfect result one master, one user and one image record.
But if I do this
return $user = \App\Models\User::where('id',1)->with(array('humanable','humanable.master'))->first();
I only get one Admin record, the query get * from masters doesn't even run.
Any idea what I'm doing wrong, I'm sure this is possible.
If I remember correctly Laravel has lots of pitfall. You can try to use the protected $with var in Admin model instead of query builder with function.
class Admin {
protected $with = ['master'];
public function user() {
return $this->morphOne('App\Models\User', 'humanable');
}
public function master() {
return $this->hasOne('App\Models\Master');
}
}
In query builder, only need to include humanable. Now you should see master inside the humanable object.
return $user = \App\Models\User::where('id',1)->with('humanable')->first();
Hope this help.
Related
i am trying to connect 4 tables together:
StepAnimation Model:
public function steps()
{
return $this->hasManyThrough('App\Models\Step','App\Models\WorkSequence');
}
public function step()
{
return $this->belongsTo('App\Models\Step');
}
Step Model:
public function workSequence()
{
return $this->belongsTo('App\Models\WorkSequence');
}
public function stepAnimation()
{
return $this->hasMany('App\Models\StepAnimation');
}
WorkSequence Model:
public function categoryWorkSequence()
{
return $this->hasMany('App\Models\CategoryWorkSequence');
}
public function step()
{
return $this->hasMany('App\Models\Step');
}
CategoryWorkSequence Model:
public function workSequence()
{
return $this->belongsTo('App\Models\WorkSequence');
}
I am currently selecting all the work sequences that have a step animation:
$this->stepAnimation = New StepAnimation();
$workSequence = $this->stepAnimation::with('step.workSequence')->get();
But now id like to attach all the categories to the worksequence, how do i need to extend the select for that to happen?
Then you can continue selecting the category relationship in your workSequence as long as it is available on your model. Like this.
$this->stepAnimation = New StepAnimation();
$workSequence = $this->stepAnimation::with('step.workSequence.categoryWorkSequence')->get();
I am not able to paginate in laravel in this situation
return $this->hasMany('App\News','category_id')->orderBy('id','desc')->paginate(20);
but says error. in controller I have also tried
$byCategories=Category::findOrFail($id)->paginate(20);`
it also says error.
help me.
My Model is
class Category extends Model
{
public function news()
{
return $this->hasMany('App\News','category_id')->orderBy('id','desc');
}
public function newsMany()
{
return $this->belongsToMany('App\News')->paginate(20);
}
public function user()
{
return $this->belongsTo('App\User');
}
public function getRouteKeyName()
{
return 'slug';
}
}
another model one is
class News extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
public function category()
{
return $this->belongsTo('App\Category');
}
}
my controller code is
public function byCategory($id)
{
$byCategories=Category::findOrFail($id);
return view('back-end/news/byCategory', compact('byCategories'));
}
Thank you so much.
Pagination is not done in the Model
it is to be done in the controller, For example remove (->paginate(20);) from here
public function newsMany()
{
return $this->belongsToMany('App\News')->paginate(20);
}
keep it only as
public function newsMany()
{
return $this->belongsToMany('App\News');
}
and call the pagination in controller when returning the view
$news=Category::findOrFail($id)->newsMany()->paginate(20);
return view('view name', compact('news'));
I try to filter SurveyQuestionnaire which have Answer = 'poor' and their Questionnaire have step = 'rating'.
I've tried to look through the documentation of Eloquent and I've found nothing help.
This is my models.
class Questionnaire extends Model {
...
public function surveysQuestionnaires() {
return $this->hasMany(SurveyQuestionnaire::class, 'question_id');
}
public function answers() {
return $this->hasMany(Answer::class, 'question_id');
}
public function questionnaires() {
return $this->hasMany(QuestionnaireTranslation::class, 'question_id' );
}
}
class SurveyQuestionnaire extends Model {
public function survey() {
return $this->belongsTo(Survey::class ,'survey_id');
}
public function questionnaires() {
return $this->belongsTo(Questionnaire::class, 'question_id');
}
public function answer() {
return $this->belongsTo(Answer::class, 'answer_id');
}
}
Well, the hasMany method returns query builder, so you can simply add some conditions:
public function surveysQuestionnaires() {
return $this->hasMany(SurveyQuestionnaire::class, 'question_id')->where('Answer', 'poor');
}
Also, you can read this link Constraining Eager Loads and add your conditions manually after taking an instance of your model:
$items = App\Questionnaire::with(['surveysQuestionnaires' => function ($query) {
$query->where('Answer', 'poor');
}])->get();
I'm working with a large json that returns several data from the database and I need to return an integer model that does not have any kind of relationship, I just need to return all the records that LampModels model with this great json. But Laravel always returns me Illegal offset type.
Controller
public function showAllUdiJson()
{
$allLamps = LampModels::all();
return Ilumination::with('street')
->with('neighborhood')
->with('iluminationinfo')
->with('economyplan')
->with('lamp')
->with('reactor')
->with('aluminumcable')
->with('steelconduit')
->with('alllamps', $allLamps)
->with('ticket')->get();
}
LampModels
<?php
class LampModels extends \Eloquent {
protected $fillable = [];
protected $table = 'lampmodel';
}
Illumination
<?php
class Ilumination extends \Eloquent {
protected $fillable = [];
protected $table = 'ilumination';
public function street()
{
return $this->belongsTo('street');
}
public function neighborhood()
{
return $this->hasOne('neighborhood', 'id');
}
public function iluminationinfo()
{
return $this->hasOne('iluminationinfo');
}
public function ticket()
{
return $this->hasMany('ticket');
}
public function economyplan()
{
return $this->hasOne('economyplan', 'id' ,'street_id');
}
public function lamp()
{
return $this->hasOne('lamp', 'id');
}
public function reactor()
{
return $this->hasOne('reactor', 'id');
}
public function aluminumcable()
{
return $this->hasOne('aluminumcable', 'id');
}
public function steelconduit()
{
return $this->hasOne('steelconduit', 'id');
}
}
See the error
Your error report is quite bad, but seems that your Ilumination model doesnt have an alllamps method.
You should attacth LampModels to your Ilumination model with a relationship, insted of doing what you doing, cause is a wrong approach.
I think you access somewhere ticket method which was created in Illumination Model that offset error encountered..
public function ticket()
{
return $this->hasMany('ticket');
}
if you want to access illumination->ticket, you must use this method with loop.
foreach(illumination->tickets as ticket) {
$field1 = ticket->field1;
}
If your still facing any issue than share your error log page here..
For some reason, I cannot chain model objects. I'm trying to eager load 'Location' for an 'Order' and would prefer the logic to be contained in the models themselves. But past one chain, it does not work.
class Order extends Eloquent {
protected $table = 'orders';
public function customer() {
return $this->belongsTo('Customer');
public function location() {
return $this->customer()->location(); // this does not work
}
}
class Customer extends Eloquent {
protected $table = 'customers';
public function user() {
return $this->belongsTo('User');
}
public function orders() {
return $this->hasMany('Order');
}
public function location() {
return $this->user()->location();
// return $this->user(); // WORKS!!
}
}
class User extends Eloquent {
protected $table = 'users';
public function locations() {
return $this->hasMany('Location');
}
public function location() {
return $this->locations()->first();
}
}
I eventually want to do this:
class ChefController extends BaseController {
public function get_orders() {
$chef = $this->get_user_chef(); // this already works
return $chef->orders()->with('location')->get(); // does not work
}
}
Try to reference relation (user table) by adding user_id as second argument, like this:
public function user() {
return $this->belongsTo('User',"user_id");
}
Maybe you called that id field different, but you know what I mean.