Laravel access value from others object - laravel

I have 3 tables :-
available_offers
- id
- date
- time
- from
- to
- user_id
requested_offer
- id
- user_id
- available_offersID
user
- id
- name
- username
- password
- email
Here my routes_bookedController
class routes_bookedController extends Controller
{
public function create()
{
$user = available_offers::with('user')->get();
$booked = requested_routes::with('available_offers', 'user')
->where('user_id', Auth::id())
->get();
return $booked;
return view ('routes_booked.show', compact('booked'));
}
}
Here the result from show.blade.php from routes_bookedController
[
{
"id": 3,
"user_id": 4,
"available_offersID": 3,
"created_at": null,
"updated_at": null,
"available_offers": {
"id": 3,
"date": "2017-08-08",
"time": "14:11:30",
"from": "Sabah",
"to": "Sarawak",
"user_id": 2,
"isBooked": 1,
"created_at": null,
"updated_at": null
},
"user": {
"id": 4,
"name": "admin",
"username": "user1",
"email": "user1#example.com",
"created_at": "2017-08-08 06:15:49",
"updated_at": "2017-08-08 06:15:49"
}
}
]
Is that possible to grab name from available_offers->user->name ?
This is my model :-
requested_routes
class requested_routes extends Model
{
public function available_offers()
{
return $this->belongsTo(available_offers::class, 'available_offersID');
}
public function user()
{
return $this->belongsTo(User::class);
}
}
available_offers
class available_offers extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function requested_routes()
{
return $this->hasMany(requested_routes::class);
}
}
User
public function available_offers()
{
return $this->hasMany(available_offers::class);
}
public function requested_routes()
{
return $this->hasMany(requested_routes::class);
}

Eager load sub model
$booked = requested_routes::with('available_offers', 'available_offers.user', 'user')
->where('user_id', Auth::id())
->get();
Ensure you add available_offers.user as one of the with clauses.

Related

How to filter collection in laravel?

I return the ExportTemplate model :
$exportTemplates = ExportTemplate::where('user_id', Auth::id())->get();
the result is something like:
[
{
"id": 45,
"name": "hallo",
"user_id": 1,
"deleted_at": null,
"created_at": "2020-02-27 14:12:50",
"updated_at": "2020-02-27 14:12:50",
"ExportTemplateColumns": [
{
"id": 398,
"export_template_id": 45,
"searchable_column_id": 7,
"created_at": "2020-02-27 14:12:50",
"updated_at": "2020-02-27 14:12:50",
"SearchableColumn": {
"id": 7,
"name": "bic",
"type": "string",
"searchable_table_id": 1,*** I need only results with searchable_table_id ==1
"created_at": "2020-02-06 09:11:24",
"updated_at": "2020-02-06 09:11:30",
"label": "BIC"
}
}
]
},
{
"id": 46,
"name": "fol",
(...)
I want to filter this result through Collection Filter to return only :
ExportTemplateColumns->SearchableColumn->searchable_table_id == 1
Models :
class ExportTemplate extends Model
{
protected $appends = ["ExportTemplateColumns"];
public function getExportTemplateColumnsAttribute()
{
return ExportTemplatesColumns::where('export_template_id',$this->id)->get();
}
public function user()
{
return $this->belongsTo(User::class,'id');
}
public function exportTemplatesColumns()
{
return $this->hasMany(ExportTemplatesColumns::class,'export_template_id');
}
}
class ExportTemplatesColumns extends Model
{
protected $appends = ["SearchableColumn"];
public function getSearchableColumnAttribute()
{
return SearchableColumn::find($this->searchable_column_id);
}
public function exportTemplate()
{
return $this->belongsTo(ExportTemplate::class,'id');
}
public function searchableColumn()
{
return $this->belongsTo(SearchableColumn::class,'id');
}
}
try this.
$exportTemplates = ExportTemplate::where('user_id', Auth::id())->get();
$filtered = $exportTemplates->filter(function($d){
return collect($d->ExportTemplateColumns)->filter(function($columns){
return $columns->SearchableColumn->searchable_table_id === 1;
})->count();
});
$filtered->all();

Laravel eloquent get related model as column_name => value

I have a Product table related with Image table, one to many relationship.
How do I get Image records not as array? Instead, return only 1 Image record as column_name: value
This code:
$products = $request->user()->products()->orderBy('id', 'desc')->with(['images' => function($query) {
$query->where('featured', 1)->limit(1);
}])->get();
Is returning data like this :
{
"id": 13,
"name": "Shoes",
"price": "3.00",
"stock": 5,
"pivot": {
"user_id": 7,
"product_id": 13
},
"images": [
{
"id": 5,
"file": "5da9d9b493403.png",
"featured" 1,
"pivot": {
"product_id": 13,
"image_id": 5
}
}
]
}
}
How to make it return like this?
{
"id": 13,
"name": "Shoes",
"price": "3.00",
"stock": 5,
"pivot": {
"user_id": 7,
"product_id": 13
},
"images": "5da9d9b493403.png"
}
}
You can append a custom accessor that gets the first image from the relationship
In the Product model
protected $appends = ['images'];
public function getImagesAttribute()
{
return $this->images()->where('featured', 1)->first()->file;
}
public function images()
{
return $this->hasMany('App\Image');
}
Then just return the query without eager loading
$products = $request->user()->products()->orderBy('id', 'desc')->get();
Hope this helps
In the Product model
protected $appends = ['images'];
public function images()
{
return $this->hasMany('App\Image')->select(['name'])->first();
}
select the name only and return first relation

laravel eloquent with in with select return null

I use laravel eloquent with relations, I have a problem about it.
these are my eloquent models;
class Post extends BaseModel
{
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
}
class User extends BaseModel
{
public function userInfo()
{
return $this->hasOne(UserInfo::class,'user_id','id');
}
}
class UserInfo extends BaseModel
{
}
when I use the following code, the user_info is null.
$posts = Post::with(['user' => function ($query) {
$query->with(['userInfo' => function ($query) {
$query->select(['nickname','avatar']);
}]);
}])->paginate(10)
->toArray();
when i request it , the result is
{
"id": 10,
"community_id": 1,
"title": "biaoti",
"desc": null,
"content": "abc",
"user_id": 1,
"user": {
"id": 1,
"mobile_phone": "13800000003",
"user_info": null
}
}
But the following code is normall.
$posts = Post::with(['user' => function ($query) {
$query->with(['userInfo' => function ($query) {
// $query->select(['nickname','avatar']);
}]);
}])->paginate(10)
->toArray();
when i request it ,the result is :
{
"id": 10,
"community_id": 1,
"title": "biaoti",
"desc": null,
"content": "abc",
"user_id": 1,
"user": {
"id": 1,
"mobile_phone": "13800000003",
"user_info": {
"id": 1,
"user_id": 1,
"nickname": "devkang",
"avatar": "",
"birthday": "1989-01-30",
"sex": 1,
"identity": 1,
"region_id": 1
}
}
}
can you help me? I don't know how to use it, I want to select some fields from user_info.
This is happening because you exclude user_id foreign key when you're using select method. To fix it add foreign key to the list:
$query->select(['nickname', 'avatar', 'user_id']);

Laravel's Eloquent pivot object

I have defined relation in my model:
public function positions() {
return $this->belongsToMany('Position', 'users_positions')->withPivot('season');
}
Is it possible to display objects with pivot in cleaner way?
For Example:
"positions": [
{
"id": 1,
"name": "A",
"pivot": {
"user_id": 1,
"position_id": 1,
"season": 2014
}
}
],
I would like to get:
"positions": [
{
"id": 1,
"name": "A",
"season": 2014
}
],
Use accessor:
public function getSeasonAttribute()
{
return ($this->pivot) ? $this->pivot->season : null;
}
Then you can access it just like other properties:
$position = $someModel->positions->first();
$position->season;
if you also need it in the toArray/toJson output, then use on you model:
protected $appends = ['season'];

laravel one to many return null on one side

i have a one to many relation that return value on one side and null in the other side !!
class Particular extends Eloquent
public function calls()
{
return $this->hasMany('Call');
}
this relation ships return values
[
{
"id": 1,
"date": "2014-10-16",
"time": "12:12:12",
"quote_id": 1,
"repairman_id": 17,
"particular_id": 2,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00"
},
{
"id": 2,
"date": "2014-10-21",
"time": "12:12:12",
"quote_id": 2,
"repairman_id": 30,
"particular_id": 2,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00"
}
]
but when i try in the other side
class Call extends Eloquent
public function particularCall() {
return $this->belongsTo('Particular');
}
it return nothing ???
You should try with:
Route::get('test', function() {
$Call = Call::find(2);
return $Call->particularCall();
});
or
Route::get('test', function() {
$Call = Call::with('particularCall')->find(2);
return $Call->particularCall;
});

Resources