stuck on collection array
I am getting the specfic array. but can't loop throught in blade
this is my controller:
public function appliedJob($id)
{
$apllied = AplliedJob::with('user','job')->find($id);
return view('dashboardviews.page.apllied-job',compact('apllied'));
}
this my model:
class AplliedJob extends Model
{
use Notifiable;
protected $fillable = [
'name', 'email', 'message','aplied_cv',
];
protected $table='appllied_jobs';
//protected $primaryKey = 'user_id';
protected $primaryKey = 'user_id';
public function user()
{
return $this->belongsTo('App\User','user_id');
}
public function job()
{
return $this->belongsTo('App\Job','job_id');
}
}
this the array I am getting I want to access relation# [job] but it
throws an error
AplliedJob {#257 ▼
#fillable: array:4 [▶]
#table: "appllied_jobs"
#primaryKey: "user_id"
#connection: "mysql"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▶]
#original: array:9 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:2 [▼
"user" => User {#263 ▼
#fillable: array:4 [▶]
#table: "users"
#hidden: array:2 [▶]
#casts: array:1 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▶]
#original: array:9 [▶]
#changes: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
}
"job" => Job {#261 ▼
#fillable: array:4 [▶]
#table: "jobs"
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => 1
"user_id" => 2
"jb_name" => "web developer"
"jb_type" => "software"
"jb_salary" => "12000"
"jb_exp" => "12"
"jb_description" => "djghnbjkguyfgykgvkvbuykgbkub g uiygjghpiu p;"
"jb_loc" => "lahore"
"created_at" => "2019-05-15 01:18:46"
"updated_at" => "2019-05-15 01:18:46"
]
#original: array:10 [▶]
#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 tried this but it gives an exception
#foreach($apllied as $aplil)
{{$aplil->job->jb_name}}
#endforeach
this the error I am getting and can't figure out what am I doing wrong
ErrorException (E_ERROR)
Trying to get property 'job' of non-object (View:
D:\xampp\htdocs\locojobs\resources\views\dashboardviews\page\apllied-
job.blade.php)
find() return one model so you need to do this:
{{$apllied ->job->jb_name}} //Remove the loop
or use get() instead of find()
What i get from this code, each AplliedJob instance has one Job. So as #Anas Bakro says, you need the remove #foreach from your code. If you want to build different architecture please write down your tables.
Try to use get() or paginate() instead of find(), because the find() returns single value.
Try the below code,
public function appliedJob($id)
{
$apllied = AplliedJob::with('user')->with('job')->get();
return view('dashboardviews.page.apllied-job',compact('apllied'));
}
Related
I have an array like this
"fio" => "John Dou"
"city" => "1"
"birthday" => "2022-10-21"
"email" => "test#gmail.ru"
"phones" => array:2 [▼
0 => array:2 [▼
"id" => "7"
"number" => "911"
]
1 => array:2 [▼
"id" => "10"
"number" => "112"
]
]
The check for uniqueness works, but when you try to update the current record, it swears for duplication
public function rules()
{
return [
'phones.*.number' => 'required|string|max:12|unique:'.Phone::class.',number,phones.*.id';
]
}
Maybe I made a mistake somewhere?
I need to access at the email value; it become from a relation hasOnce(),
this is dd($this->order_details)
Illuminate\Database\Eloquent\Collection {#2322 ▼ #items: array:1 [▼
0 => App\Order {#2327 ▼
#hidden: array:1 [▶]
#connection: "mysql"
#table: "orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:12 [▶]
#original: array:12 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: array:1 [▶]
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"user" => App\User {#2356 ▼
#dispatchesEvents: array:1 [▶]
#fillable: array:3 [▶]
#hidden: array:9 [▶]
#casts: array:1 [▶]
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:20 [▼
"id" => 36
"email" => "email#gmail.com"
"password" => "$2y$10$f.u7MO6ZZ4GBf1uaabiF9OXerh0SR7JzUV9M5PGI1IG1xFATkeTSG"
"name" => "NAME"
"surname" => "SURNAME"
"phone" => "+39331199222"
"confirm_code" => 111111
"confirm_code_date" => "2020-06-01 18:17:42"
"confirm_tries" => 0
"city_id" => 5662
"stripe_customer_id" => "cus_HPVN1YJE0wjHtm"
"status" => "ACTIVE"
"is_admin" => 1
"price_list_id" => 1
"wallet" => 99852.6
"last_login_at" => "2020-06-05 11:49:32"
"remember_token" => null
"created_at" => "2020-06-01 18:17:42"
"updated_at" => "2020-06-08 16:33:12"
"deleted_at" => null
when i try to get it with: $this->order_details->user->email
i get back:
Property [user] does not exist on this collection instance.
I've no idea which is my error and what is wrong to get it as well,
with $this->order_details->user[0]->email
the result is the same
You are trying to access an object which is not present in the collection.
$this->order_details is a Collection
You need to access the first order, before you can access the user:
$firstOrder = $this->order_details->first();
$user = $firstOrder->user;
$email = $user->email;
// Or:
$email = $this->order_details->first()->user->email;
Of course you can also iterate over the orders in the collection, but for now I assumed you only need the first order.
In one of the controller function
$readings = Reading::orderBy('reading_date_time', 'DESC')->get();
dd($readings);
This gives
Illuminate\Database\Eloquent\Collection {#1883
#items: array:160 [
0 => App\Reading {#1722
#fillable: array:6 [ …6]
#connection: "mysql"
#table: "readings"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:8 [ …8]
#original: array:8 [ …8]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [ …1]
}...
How can I access the actual values of readings table. And use them for operations like array_column().
This function already give you the result, and the result is collection.
You can call the attribute by single object in collection:
$readings->first()->reading_date_time;
and you can use method-pluck to get the attribute values from collection, it just like array_column:
$readings->pluck('reading_date_time');
If you use array_column, you can use method-toArray to change collection to array:
array_column($readings->toArray(), 'reading_date_time')
You may use toArray() helper
The toArray method converts the collection into a plain PHP array. If
the collection's values are Eloquent models, the models will also be
converted to arrays
$readings = Reading::orderBy('reading_date_time', 'DESC')->get()->toArray();
/*
[
['column1' => 'value1', 'column2' => 'value2', ...],
['column1' => 'value1', 'column2' => 'value2', ...],
['column1' => 'value1', 'column2' => 'value2', ...],
...
]
*/
Then you may use array_column() on $readings array
$pluckedValues = array_column($readings, 'chosen_column');
print_r($pluckedValues);
In my controller I have
$all = \App\Product::where('campaign_id', $product->campaign_id)->get();
When I DD out in the template
{{ dd($all)}}
I get a collection object
Collection {#340 ▼
#items: array:1 [▼
0 => Product {#341 ▼
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▶]
#original: array:10 [▶]
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
]
}
How do I see the basic array returned?
I know this question is old, but can't believe is not answered.
You need to convert the eloquent model to an array, to display it
{{ dd($all->toArray()) }}
More info here: https://laravel.com/docs/5.6/eloquent-serialization
I'm getting this error message bag from a RESTfull API on a Laravel client, I want to show the errors to the user, but I'm having trouble to show the errors inside the bag when they are an array.
MessageBag {#251 ▼
#messages: array:6 [▼
"gender" => array:1 [▶]
"first_name" => array:1 [▼
0 => "The first name must be at least 2 characters."
]
"last_name" => array:1 [▶]
"user_id" => array:1 [▶]
0 => array:4 [▼
"user_id" => array:1 [▶]
"address2" => array:1 [▼
0 => "The address2 must be at least 3 characters."
]
"zip" => array:1 [▶]
"phone" => array:1 [▶]
]
1 => array:4 [▼
"user_id" => array:1 [▶]
"address2" => array:1 [▼
0 => "The address2 must be at least 3 characters."
]
"zip" => array:1 [▶]
"phone" => array:1 [▶]
]
]
#format: ":message"
}
I know the normal way to show errors for example to print the gender errors I can do this one:
{!! $errors->first('gender', '<label class="control-label"><i class="glyphicon glyphicon-menu"></i> :message</label>') !!}
Or to check if 'gender' error exists:
{!! $errors->has('gender') ? 'has-error' : '' !!}
But how I do to check if the array 0 exists and show the errors?
Thank you :)
Currently on Laravel 5.1 printing MessageBags with Arrays is not posible, my solution was to instance a different MessageBag for the secondary errors instead of nest it on the main MessageBag.
Another posible solution was to nest it like:
MessageBag {#251 ▼
#messages: array:6 [▼
"gender" => array:1 [▶]
"first_name" => array:1 [▼
0 => "The first name must be at least 2 characters."
]
"last_name" => array:1 [▶]
"user_id.0.errors" => array:1[▼
0 => "True"
]
"user_id.0.error_name" => array:1[▼
0 => "The first name must be at least 2 characters"
]
"user_id.1.errors" => array:1[▼
0 => "True"
]
"user_id.1.error_name" => array:1[▼
0 => "The first name must be at least 2 characters"
]
]
#format: ":message"
}
But is not a clean way to do it, as I said, create a secondary MessageBag was more clean and easy :)