Laravel Eloquent, appends attribute that is same name with the relationship - laravel

<?php
class Product extends Eloquent {
protected appends = array('category');
public function category()
{
return $this->belongsTo('Models\Category',
'category_id');
}
}
How to achieve that ?

<?php
class Product extends Eloquent {
protected $with = array('category');
//protected $appends = array('category');
public function category()
{
return $this->belongsTo('Models\Category',
'category_id');
}
}
Define a $with property instead of $appends property. It is an eager load.

Related

Call to a member function where() on null Route Model Binding Laravel 7

before so sorry for my bad english. Whats mean that error? "Call to a member function where() on null". In my code need obtain all application for a each category.
Im interesting in use a Model route binding, its works well when I insert a one model, for example: Application. Example:
Route::get(applications/{application},'ApplicationController#show')->name('apps'));
But when I try use 2 model in a route, have this problem. Here my code:
Route::get('categories/{category}/applications/{application:category_id}','ApplicationController#show')->name('apps');
My models:
class Application extends Model{
protected $fillable = ['name', 'price', 'category_id', 'vote', 'image_src'];
public function users()
{
return $this->belongsToMany(User::class, 'applications_users_states', 'application_id', 'user_id')
->withPivot('state_id')
->withTimestamps();
}
public function categories()
{
return $this->belongsTo(Category::class);
}
public function logs()
{
return $this->belongsToMany(Log::class)->withTimestamps();
}
}
class Category extends Model
{
protected $fillable = ['name', 'description'];
public function applications()
{
$this->hasMany(Application::class);
}
}
My Controller:
public function show(Category $category,Application $application)
{
return $application;
}
Thanks for your time and for your knowledge!!!!!

Call to undefined relationship on model

We have the following class using $with:
class CargaHorasEmpleado extends Model
{
protected $table = "empleados_horas";
protected $with = ["tipoTarea", "proyecto", "empleado", "empleadoQueHizoLaCarga"];
public function tipoTarea()
{
return $this->belongsTo('App\TipoTarea', 'id_tipo_tarea', 'id')->withTrashed();
}
public function empleado()
{
return $this->belongsTo('App\Empleado', 'id_empleado', 'id')->withTrashed();
}
public function empleadoQueHizoLaCarga()
{
return $this->belongsTo('App\Empleado', 'id_empleado_cargo_hs', 'id')->withTrashed();
}
public function proyecto()
{
return $this->belongsTo('App\Proyecto', 'id_proyecto', 'id')->withTrashed();
}
}
This is the class TipoTarea
namespace App;
use Illuminate\Database\Eloquent\Model;
class TipoTarea extends Model
{
protected $table = 'tipos_tareas';
public $timestamps = false;
protected $fillable = [
'titulo', 'descripcion'
];
}
Thep page throws the error: "Call to undefined relationship [tipoTarea] on model [App\CargaHorasEmpleado]". That's the only relationship that's not working. The others are fine. What's wrong?
Well, isn't the relationship called "tipoTarea"? You wrote "tiposTarea"
The problem was that my class "TipoTarea" didn't use softdeletes. So the error was in using the "WithTrashed" method. The correct way is:
public function tipoTarea()
{
return $this->belongsTo('App\TipoTarea', 'id_tipo_tarea', 'id');
}

Laravel 4.2 Eloquent relationship non-object result

I want show $post->category->cat_name but I have an error Trying to get property of non-object!
Model : Category
class Category extends \Eloquent {
protected $guarded = array();
public static $rules = array();
public function posts(){
return $this->hasMany('Post');
}
}
Model Post
class Post extends \Eloquent {
protected $guarded = array();
public static $rules = array();
public function category(){
return $this->belongsTo('Category');
}
public function user(){
return $this->belongsTo('User');
}
public function comments(){
return $this->hasMany('Comment');
}
}
PostsController
public function show($id)
{
$post = Post::find($id);
return View::make('posts.show',compact('post'));
}
}
In View $post->post_name works.
$post->user->username works
$post->comments->content works
but $post->category->cat_name doesn't works
And I can do CRUD with categories.
So I think there is a probleme with my model Category but i don't understand

Laravel5.3:How to use pluck in relationship for bindign Form::select element?

This is my model:
class Positions extends Model implements Repository
{
protected $fillable = ['index_id', 'title', 'description'];
public function index()
{
return $this->belongsTo('TEST\Indices', 'index_id');
}
public function getById($id)
{
return $this->with('index')->find($id);
}
}
how to use pluck() in getById() function for listing index relationship?
You can do it like this :
return $this->with('index')->find($id)->pluck('index.indexField');

can't access morphOne relationship

I am having a problem accessing morphOne relationship
In my controller I have
$post = Post::with('content')->where('id', 1)->get();
dd($post);
this gives
This is setup of my classes:
Post.php
class Post extends Model
{
use Contentable;
Contentable.php
trait Contentable {
public function content()
{
return $this->morphOne(Content::class, 'contentable');
}
Content.php
class Content extends Model
{
protected $table = 'content';
public $timestamps = false;
}
If in controller I do this:
dd($post->content());
I get Method content does not exist.
or
dd($post->content);
I get Property [content] does not exist on this collection instance.

Resources