I have a tricky situation here.
I have an Edit form where I can fill the existing values when I'm editing an index. (I pass a Model Object in the controller). It works for every input field, but I need to fill a certain Multiple Select field which I don't know how to do it. I tried passing this specific value in the object, either as array (which contains the IDs of each selectable value) which crashes the view or as a json string using json_encode. I read an article here where the person accomplish this but it's done with a many-to-many relationship table, but the value I need to fill doesn't come from the table.
Resulting Object using dd()
Contact {#408 ▼
+table: "contacts"
#dates: array:1 [▼
0 => "deleted_at"
]
+fillable: array:5 [▼
0 => "nombre"
1 => "apellido"
2 => "cargo"
3 => "empresa_id"
4 => "groups"
]
#casts: array:5 [▼
"nombre" => "string"
"apellido" => "string"
"cargo" => "string"
"empresa_id" => "integer"
"groups" => "array"
]
#connection: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:9 [▼
"id" => 4
"nombre" => "redacted"
"apellido" => "redacted"
"cargo" => "redacted"
"empresa_id" => 2
"created_at" => "2017-03-09 20:30:19"
"updated_at" => "2017-11-29 17:30:55"
"deleted_at" => null
"groups" => "["2","3","4"]"
]
#original: array:9 [▼
"id" => 4
"nombre" => "redacted"
"apellido" => "redacted"
"cargo" => "redacted"
"empresa_id" => 2
"created_at" => "2017-03-09 20:30:19"
"updated_at" => "2017-11-29 17:30:55"
"deleted_at" => null
"groups" => "["2","3","4"]"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▼
0 => "*"
]
#dateFormat: null
#touches: []
#observables: []
#with: []
+exists: true
+wasRecentlyCreated: false
#forceDeleting: false
Multiple Select Form.
{!! Form::label('groups', 'Grupos:') !!} </br>
{!! Form::select('groups', $groups, null, ['data-actions-box' => 'true', 'multiple'=>'multiple']) !!}
If more information is needed I will edit the question.
Fixed it by transforming the array elements to int before passing it to json_encode.
Related
Here is my Session and i would like to fetch the value from Sample_table to get the original array value how can i do that ?
{{ dd(Session::get('sample')[0]) }}
Sample_table{#266 ▼
#fillable: array:6 [▶]
#table: "Sample_table"
#primaryKey: "id"
+timestamps: true
#connection: "mysql"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:20 [▶]
#original: array:20 [▼
"id" => "9"
"firstName" => "test"
"lastName" => "asdf"
"middleName" => "test"
"contactNo" => "2147483647"
"emailAddress" => "test#gmail.com"
"residenceAddress" => "afsf"
"province_id" => "6"
"city_id" => "3"
"barangay_id" => "16982"
"copyMailing" => null
"mailingAddress" => null
"mailing_province_id" => "6"
"mailing_city_id" => "3"
"mailing_barangay_id" => "16982"
"status" => "0"
"userIdentity" => "0"
"global_id" => "0"
"updated_at" => "0000-00-00 00:00:00"
"created_at" => "0000-00-00 00:00:00"
]
You can use ->getRawOriginal():
Session::get('sample')[0]->getRawOriginal();
Today I'm facing some very weird behavior in Laravel while fetching records from the table. I'm having a table which has One to Many Relation with itself. A Menu item can have multiple Submenu. Below is a brief structure:
Table name: site_navigation
id => Primary Key, Auto Increment
parent_id => Index, Defaults Null
menu_name => Varchar
Model => 'Sitemenu_model.php'
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Sitemenu_model extends Model
{
protected $table = 'site_navigation';
public function getParent()
{
return $this->belongsTo('App\Models\backend\Sitemenu_model', 'parent_id');
}
public function getChildren()
{
return $this->hasMany('App\Models\backend\Sitemenu_model', 'parent_id');
}
}
Controller => 'Pagescontent.php'
use App\Models\backend\Sitemenu_model;
class Pagescontent extends Controller
{
$collSiteMenu = Sitemenu_model::with('getChildren')
->orderBy('display_order', 'asc')
->get();
return view('backend.pages.pagescontent.create', ['collSiteMenu'=>$collSiteMenu]);
}
View => 'create.blade.php'
#foreach($collSiteMenu as $menu)
#php
$arrMainMenu[$menu->id] = $menu->menu_name;
#endphp
#endforeach
The '$arrMainMenu' is only having the last record of the collection. When i tried to print $menu->id, it prints blank. Even the child record doesn't print its menu id. Even if i use below code, still no output. Which means 'id' attribute is not set.
#foreach($collSiteMenu as $menu)
#isset($menu->id)
{{ 'menu id is set to '. $menu->id }}
#endisset
#endforeach
However, in controller, when i dd($collSiteMenu) it prints below:
Collection {#380 ▼
#items: array:12 [▼
0 => Sitemenu_model {#393 ▼
#table: "site_navigation"
#fillable: array:10 [▶]
+id: null
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:13 [▼
"id" => 1
"menu_name" => "About Us"
"parent_id" => null
"url_slug" => "about-us"
"canonical_url" => null
"show_in_nav" => "show_both"
"meta_title" => "About Us"
"meta_keywords" => null
"meta_description" => "this is test description"
"is_active" => "yes"
"display_order" => 1
"created_at" => "2020-09-02 12:39:20"
"updated_at" => "2020-09-04 16:21:51"
]
#original: array:13 [▼
"id" => 1
"menu_name" => "About Us"
"parent_id" => null
"url_slug" => "about-us"
"canonical_url" => null
"show_in_nav" => "show_both"
"meta_title" => "About Us"
"meta_keywords" => null
"meta_description" => "this is test description"
"is_active" => "yes"
"display_order" => 1
"created_at" => "2020-09-02 12:39:20"
"updated_at" => "2020-09-04 16:21:51"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"getChildren" => Collection {#411 ▼
#items: array:2 [▼
0 => Sitemenu_model {#415 ▼
#table: "site_navigation"
#fillable: array:10 [▶]
+id: null
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:13 [▶]
#original: array:13 [▼
"id" => 7
"menu_name" => "Who We Are"
"parent_id" => 1
"url_slug" => "who-we-are"
"canonical_url" => null
"show_in_nav" => null
"meta_title" => "Who We Are"
"meta_keywords" => null
"meta_description" => null
"is_active" => "yes"
"display_order" => 1
"created_at" => "2020-09-02 14:14:03"
"updated_at" => "2020-09-02 14:14:03"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => Sitemenu_model {#416 ▼
#table: "site_navigation"
#fillable: array:10 [▶]
+id: null
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:13 [▶]
#original: array:13 [▼
"id" => 8
"menu_name" => "Our Cause"
"parent_id" => 1
"url_slug" => "our-cause"
"canonical_url" => null
"show_in_nav" => null
"meta_title" => "Our Cause"
"meta_keywords" => null
"meta_description" => null
"is_active" => "yes"
"display_order" => 2
"created_at" => "2020-09-02 14:15:27"
"updated_at" => "2020-09-02 14:15:27"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
I have searched on Google but couldn't find a solution to this. Can any one guide me as where I'm going wrong.
Much Regards,
Javed
In my model, i have the following fillable array:
protected $fillable = [
'first_name',
'last_name',
'email',
'street',
'house_number',
'province',
'phone',
'country',
'date_of_birth',
'postcode',
'account_id',
'user_id'
];
user_id is nullable and account_id is not.
When i try to create a new entity, user_id stays null in my database.
This is my code for saving the entity:
#in controller
$this->dispatch(new Job($user = $request->user(), $client = new Client(),
$request->only($client->getFillable())
));
#in job class
$client = $this->client->create(array_merge([
'user_id ' => $this->user->id,
'account_id' => $this->user->account->id
],$this->variables));
This is all of the data going into the create method:
"user_id " => 3
"account_id" => 3
"first_name" => "patrick"
"last_name" => "vd pols"
"email" => "patrickvanderpolssad22dxsxs#live.nl"
"street" => "Dijkgraaf 2"
"house_number" => "asdasd"
"province" => "Drenthe"
"country" => "asdasdsad"
"date_of_birth" => "14-12-2019"
"postcode" => "3155GA"
Both user with ID 3 and account with ID 3 exist in my database.
After saving the entity, the user_id is null:
App\Client {#384 ▼
#fillable: array:12 [▶]
#connection: "mysql"
#table: "clients"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: true
#attributes: array:13 [▼
"account_id" => 3
"first_name" => "patrick"
"last_name" => "vd pols"
"email" => "patrickvanderpolssad22dxsxs#live.nl"
"street" => "Dijkgraaf 2"
"house_number" => "asdasd"
"province" => "Drenthe"
"country" => "asdasdsad"
"date_of_birth" => Carbon\Carbon #1576281600 {#385 ▶}
"postcode" => "3155GA"
"updated_at" => "2019-12-14 00:10:03"
"created_at" => "2019-12-14 00:10:03"
"id" => 8
]
#original: array:13 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
What am i not seeing?
Thanks!
Edit:
I am an idiot.
There was an extra space after user_id:
'user_id ' => $this->user->id
Solved.
You did find the catch!
But since you want me to write the answer, I gladly do it.
You had an extra space on your key 'user_id ' => $this->user->id
So I am looking for a matching business using eloquent. I get a matching result and now I want to use specific field, assign it to variable so I can retrieve user twitter.
Here's my code:
$business = Business::with('addresses')
->find($id);
$variable = $business->business_twitter;
$test = Twitter::getUserTimeline(['screen_name' => $variable, 'count' => 20, 'format' => 'json']);
dd($variable);
However, dd($variable) is apparently empty. How can this be tackled?
//edit
dd($business);
Business {#232 ▼
#table: "businesses"
+timestamps: true
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:14 [▼
"id" => 97
"name" => "telephone"
"type" => "4"
"email" => "p.wojtas26#gmail.com"
"logo" => null
"twitter_business" => "live_oldham"
"facebook_business" => "liveoldham"
"instagram_business" => "live_oldham"
"image" => null
"gallery_id" => null
"user_id" => 2
"api_key" => null
"created_at" => "2017-06-20 10:10:38"
"updated_at" => "2017-06-20 10:10:38"
You are doing mistake while fetching key. You should fetch twitter_business key from your $business collection.
$variable = $business->twitter_business;
This is related to another question I asked previously that wasn't answered successfully.
I think I have narrowed it down to make the question more precise.
I have a parent/child BelongsTo relation in Laravel and it is returning the following (which is the result of a {{dd($task->asset->parent_asset())}}
BelongsTo {#657 ▼
#foreignKey: "parent_asset_id"
#otherKey: "id"
#relation: "parent_asset"
#query: Builder {#663 ▶}
#parent: Asset {#664 ▼
#dates: array:1 [▶]
#fillable: array:19 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:24 [▼
"id" => 4
"agency_id" => 1
"name" => "Bedrock Park"
"description" => ""
"serial_number" => ""
"asset_category_id" => 5
"activity_center_id" => 1
"original_cost" => 0.0
"in_service_date" => "1920-01-01"
"expected_lifespan" => 0
"status" => 1
"notes" => ""
"created_at" => "2015-09-14 17:59:47"
"updated_at" => "2016-10-31 20:01:29"
"square_feet" => 0
"gps_longitude" => "0.000000"
"gps_latitude" => "0.000000"
"parent_asset_id" => 0
"supervisor_id" => 0
"model_number" => 0
"is_location" => 1
"asset_sub_category_id" => 0
"asset_group_id" => 2
"address" => ""
]
#original: array:24 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
#related: Asset {#682 ▶}
}
I can see that the "name" attribute is there and I am trying to access that attribute of "name" in the following way but I receive an error "Trying to get property of non-object".
$task->asset->parent_asset->name
How should I be accessing the "name" attribute in the collection?
Model relations:
This is the relation in the asset model:
public function parent_asset()
{
return $this->belongsTo('\pmms\Asset', 'parent_asset_id');
}
and the relation in the task model
public function asset()
{
return $this->belongsTo('\pmms\Asset');
}
results of dd($task) on query $task = Task::with('asset.parent_asset')->first();
Task {#738 ▼
#dates: array:2 [▶]
#fillable: array:20 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:23 [▼
"id" => 71214
"agency_id" => 1
"sourceable_id" => 6
"sourceable_type" => "\pmms\Primary_task_type"
"primary_task_type_id" => 6
"activity_center_id" => 0
"asset_id" => 158
"hours" => 0.0
"labor_code_id" => 1
"user_id" => 7
"assigned_to_user_id" => 7
"do_date" => "2016-10-01"
"date_completed" => null
"status" => 0
"supervisor_notes" => ""
"staff_notes" => ""
"created_at" => "2016-07-17 21:44:22"
"updated_at" => "2016-07-17 21:44:22"
"priority_level" => 3
"crew_assignment_id" => null
"multi_asset_task_id" => null
"scheduled_maintenance_series_id" => 23
"pay_rate" => 0.0
]
#original: array:23 [▼
"id" => 71214
"agency_id" => 1
"sourceable_id" => 6
"sourceable_type" => "\pmms\Primary_task_type"
"primary_task_type_id" => 6
"activity_center_id" => 0
"asset_id" => 158
"hours" => 0.0
"labor_code_id" => 1
"user_id" => 7
"assigned_to_user_id" => 7
"do_date" => "2016-10-01"
"date_completed" => null
"status" => 0
"supervisor_notes" => ""
"staff_notes" => ""
"created_at" => "2016-07-17 21:44:22"
"updated_at" => "2016-07-17 21:44:22"
"priority_level" => 3
"crew_assignment_id" => null
"multi_asset_task_id" => null
"scheduled_maintenance_series_id" => 23
"pay_rate" => 0.0
]
#relations: array:2 [▼
"assignee" => User {#670 ▶}
"asset" => Asset {#666 ▼
#dates: array:1 [▶]
#fillable: array:19 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:24 [▶]
#original: array:24 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
]
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▼
0 => "*"
]
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
I can only assume that your relationships are working.
You are dumping the definition of relationship, not result of the query.
Try using this for query:
$task = Task::with('asset.parentAsset')->first();
And then dump like this:
dd($task->asset->parent_asset->name);
Assuming your relationships are defined correctly this should give you the result you want.
Edit:
As it turns out the relationship must be camelCased in order to use the magic property. More Laravel 4 - Can't retrieve data in a one-to-many relationship
So rename your parent_asset() method to parentAsset() and try the above queries.
I found the answer after much head shaking:
It is not this that I needed to call:
$task->asset->parent_asset->name
It is this:
$task->asset->parent_asset['name']
I am not sure why it is an array in this case but I did dd($task->asset->parent_asset) and it showed an array. Once I changed to an array in my view, it worked like I wanted.