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

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');

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!!!!!

How to display owner of a post in laravel admin list display?

public function index()
{
$query = Activity::orderBy('id','DESC')->with('provinces');
if(!Auth::user()->hasRole('Administer')){
$query=$query->where('province_id', Auth::user()->id);
}
$activities = $query->latest()->get();
return view('activity.index',compact('activities'));
}
How to display owner of a post in laravel admin list display?
My User Model
class User extends Authenticatable implements HasMedia
{
public function activities()
{
return $this->hasMany(Activity::class);
}
}
My Activity Model:
class Activity extends Model
{
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
public function provinces()
{
return $this->belongsToMany(Province::class);
}
}
My province Model
class Province extends Model
{
protected $fillable = [ 'title', 'post_id'];
public function activities()
{
return $this->belongsToMany(Activity::class);
}
public function user()
{
return $this->belongsToMany(User::class);
}
}
I want to show the content to the user by province.
And also the admin can see all the content.
please help me i am new in laravel
Based on your comment, you should change your province conditions to:
if (!Auth::user()->hasRole('Administer')) {
$query = $query->where('province_id', Auth::user()->province_id);
}
You have defined that each activity belongs to an user. So can access owner of each activity like this:
$activity->user->name;
And if your activity belongs to a province, you should define your relation in this way:
class Activity extends Model
{
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
public function province()
{
return $this->belongsTo(Province::class);
}
}
Also for your Province model:
class Province extends Model
{
protected $fillable = [ 'title', 'post_id'];
public function activities()
{
return $this->hasMany(Activity::class);
}
public function users()
{
return $this->hasMany(User::class);
}
}
And this is best practice to use eager loading for fetching related records. It will look like this one:
$query = Activity::with(['user', 'province'])->orderBy('id','DESC');
Also another hint: try to use better variable names. You can use $activities instead of $query.

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 'boot' function is not firing

I have the following in an eloquent model
class Bucket extends \Eloquent {
protected $fillable = ['name'];
protected $appends = ['slug'];
public function __construct(){
$this->key = substr(str_shuffle(MD5(microtime())), 0, 24);
}
public static function boot(){
dd('check');
}
public function users(){
return $this->belongsToMany('User');
}
public function getSlugAttribute(){
return slugify($this->name);
}
}
However I'm able to read and update the model with no problem. I was under the impression boot was supposed to be called every time a model was instantiated, is that wrong?
Here's one of the controllers I use to view all the buckets
public function index()
{
return Auth::user()->buckets;
}
In your constructor, try calling
parent::__construct();

How can I select linked objects from multiple parent objects in Laravel Eloquent

I would like to do something like this.
Location::where('city', '=', 'Chicago')->chef();
With these relationships:
class Location extends Eloquent {
protected $table = 'locations';
public function chef() {
return $this->belongsTo('Chef');
}
}
class Chef extends Eloquent {
protected $table = 'chefs';
public function location() {
return $this->hasMany('Location');
}
}
This should work:
class Location extends Eloquent {
protected $table = 'locations';
public function chefs() {
return $this->belongsTo('Chef');
}
public function getAllChefsByCity($city)
{
$this->with('chefs')->where('city', $city)->get();
}
}
Then in your code:
$array = $location->getAllChefsByCity('Chicago');

Resources