I want to hide some data which selected from database but reinitialize from some method in Controller not defined in its Models.
function ddd(){
return Client::select($this->_client)->with([
'Contact'=>function($s){
//$this->setHidden('use_id');
//$s->setHidden('use_id');
$s->select($this->_contact);
},
'Employer'=>function($s){$s->select($this->_employers);},
])->get();
}
You requirement is not very clear. However. I am assuming that you have 3 models Client hasOne Contact and belongsTo Employer.
In order to hide the use_id property of the Client model you can define a hidden property in your model
class Client extends Model
{
//will hide the `use_id` from the model's array and json representation.
protected $hidden = ['use_id'];
//Relations
/**
* Get the Contact which the given Client has.
* #return \Illuminate\Database\Eloquent\Relations\HasOne
* will return a App\Contact model
*/
public function contact()
{
return $this->hasOne('App\Contact');
}
/**
* Get the Employee to which the given Client belongs.
* #return \Illuminate\Database\Eloquent\Relations\BelongsTo
* will return a App\Employer model
*/
public function employer()
{
return $this->belongsTo('App\Employer');
}
}
Then in probably your ClientsController in some action ddd
public function ddd()
{
return Client::with('contact')->with('employer')->get();
}
Related
I have custom model User and I want push custom attribute to model after fetching data. How I can do it?
I know what before fetching we can add custom attibute like this:
class User extends Model
{
protected $appends = ['callable'];
public function getCallableAttribute()
{
$callable = $this->getMeta('phone') ? true : false;
return $callable;
}
}
But how do it after fetching data, for example after:
$user = User::find(1);
And now how append custom attribute to fetched User object data?
You can define a Laravel Accessor for that
E.g.
class User extends Model
{
/**
* Get the user's first name.
*
* #param string $value
* #return string
*/
public function getFirstNameAttribute($value)
{
return ucfirst($value);
}
/**
* Get the user's full name.
*
* #return string
*/
public function getFullNameAttribute()
{
return "{$this->first_name} {$this->last_name}";
}
}
Then you can access Accessors like this:
$user = App\User::find(1);
$firstName = $user->first_name; // In studly Case
$firstName = $user->full_name; // In studly Case
You can read more about Laravel Accessors here: https://laravel.com/docs/5.8/eloquent-mutators#defining-an-accessor
I'm using Laravel Nova partition to show an amount of items per category.
public function calculate(Request $request) {
return $this->count($request, Item::class, 'category_id');
}
This works fine, but displays on the screen the category_id of course.
I would rather want to show the category_name.
The model is built as follows:
class Item extends Model
{
public function category()
{
return $this->belongsTo('App\Category');
}
}
and
class Category extends Model
{
public function item()
{
return $this->hasMany('App\Item');
}
}
Also, I have in the Nova Category resource defined the following:
public static $title = 'category_name';
How can I show the category name instead of the category id?
From the documentation
Often, the column values that divide your partition metrics into
groups will be simple keys, and not something that is "human
friendly". Or, if you are displaying a partition metric grouped by a
column that is a boolean, Nova will display your group labels as "0"
and "1". For this reason, Nova allows you to provide a Closure that
formats the label into something more readable:
/**
* Calculate the value of the metric.
*
* #param \Illuminate\Http\Request $request
* #return mixed
*/
public function calculate(Request $request)
{
return $this->count($request, User::class, 'stripe_plan')
->label(function ($value) {
switch ($value) {
case null:
return 'None';
default:
return ucfirst($value);
}
});
}
So, in your case, it would be something like this:
/**
* Calculate the value of the metric.
*
* #param \Illuminate\Http\Request $request
* #return mixed
*/
public function calculate(Request $request)
{
return $this->count($request, Item::class, 'category_id');
->label(function ($value) {
// Get your category name here
// For example: return \App\Category::find($value)->name;
});
}
i want to implement a sidebar based on user with most posts and comments in laravel 5.2 app,
i want to show the top 5 users with most posts and comment.
Exemple Like this:
User Name: "Mohcin", Post:30, answer:20
Thanks in advance
User model:
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
public function questions(){
return $this->hasMany('\App\Question');
}
public function answers(){
return $this->hasMany('\App\Answer');
}
Question model: (posts model)
public function User(){
return $this->belongsTo('\App\User');
}
public function answers(){
return $this->hasMany('\App\Answer');
}
public static function your_questions(){
return static::where ('user_id','=',Auth::user()->id)->paginate(50);
}
public static function unsolved(){
return static::where ('solved','=',0)->orderby('id','DESC')->latest()->paginate(50);
}
public static function unsolvedbar(){
return static::where ('solved','=',0)->orderby('id','DESC')->latest()->take(3)->get();;
}
public function category()
{
return $this->belongsTo('App\Category');
}
public function tags()
{
return $this->belongsToMany('App\Tag');
}
Steps:
Open app/Providers/AppServiceProvider.php
In this file you have an empty (probably) boot method.
Inside this method, you can create a view composer to pass data for a specific view.
Like this:
//Assuming you want to pass the var to the view: sidebar.blade.php inside resources/views/layout
view()->composer('layout.sidebar', function($view){
$topUsers = User::topUsers(); //Create a query or scope to do this...
//After populate topUsers, you have to pass it to the view using:
$view->with('topUsers', $topUsers);
});
In your view: `resources/views/layout/sidebar.blade.php, you can access the users with a simple $topUsers like this:
foreach($topUsers as $user) {
var_dump($user);
}
I've got a model and the model its self could be linked to multiple other databases but only one at a time.
Instead of having a eloquent method for all the possible databases; it could have one that will use a variable from the self instance to choose the database and return just that.
It will save alot of work, as returning each one and testing to see if there are any results is cumbersome.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Feature extends Model
{
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'companies';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name',
];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = [
'db_name',
'enabled',
];
/**
* Uses the its own database name to determine which input to return.
*/
public function inputs() {
// if this->hidden->db_name == 'input type 1'
// return $this->HasMany(InputType1::class);
.... and so on
} // end function inputs
}
This is definitely a strange behaviour but I think you can achieve what you are looking for like so :
//in your model
public function inputs()
{
switch ($this->attributes['db_name']) {
case : 'input type 1':
return $this->hasMany(InputType1::class);
case : //some other database name
return //another relation
}
}
Expanding on shempognon answer, what I actually got to work was
switch($this->db_name) {
case 'Input_Timesheet':
return $this->hasMany(Input_type1::class);
}
I am having an issue with laravel not seeing my tags() method for attaching new tags on a new entry. I keep getting Call to a member function on a non-object when I try to run the method and attach tags to my Tile model. All methods are returning their relations. I followed the same order as the documentation says eloquent.
Controller
$tile = \Tiles\Tile::find($tile_id);
$tile->tags()->attach($tag_array);
Model
<?php namespace Tiles;
use Illuminate\Database\Eloquent\Model;
class Tile extends Model {
/**
* The Tile table
* #var string
*/
protected $table = 'tiles';
/**
* Pivot table for tags
* #var string
*/
protected $pivot = 'tag_tile';
/**
* Get the tags associated with the given tile
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags() {
return $this->belongsToMany('Tiles\Tag', $this->pivot, 'tile_id', 'tag_id')->withTimestamps();
}
}
Try it
Model
public function tags() {
return $this->belongsToMany('Tiles\Tag', $this->pivot, 'tag_id', 'tile_id')->withTimestamps();
}
Thanks for all your help. I figured out the solution. I created a method in my model and pushed each to an array and fed it to the attach method. It works now.