Insert value in table relationship Laravel - laravel

I want to insert some value in db, but I get an error, and I am not sure why.
Trying to get property 'lkp_relationship_correlation' of non-object**
$person = new Person();
foreach ($request->relationship_values as $item) {
$relationship = new Relationship();
$relationship->fill([
'lkp_relationship_correlation_id' => $item->lkp_relationship_correlation,
'relative_fullname' => $item->relatives_surname,
'relative_surname' => $item->relatives_full_name,
'relative_id' => 3,
]);
$relationship->person()->associate($person)->save();
};
public function person()
{
return $this->hasMany(Person::class, 'person_id');
}
public function relationship()
{
return $this->belongsTo(Relationship::class);
}
dd($item);
array:3 [ "lkp_relationship_correlation" => 11 "relatives_surname"
=> "Simona" "relatives_full_name" => "Simona Arabu" ]

Given that $item is an array, you can't do this:
$item->lkp_relationship_correlation
You can access the lkp_relationship_correlation value like:
$item['lkp_relationship_correlation']

Related

select column from an array based on id

I want to filter through this array $bank_totals and select a value of amount only.
$bank_totals = $bank_totals->bank_balances();
"id" => 1
"bank" => "KCB"
"amount" => 7622.0
]
1 => array:3 [
"id" => 2
"bank" => "I & M Bank"
"amount" => 25000.0
am getting the id from user input $data['id']; I want when the $data['id'] = 2 example the value shown is 25000
$data = request()->all();
$bank_totals = $bank_totals->bank_balances();
(to appear here)
here is my bank_balances method
class TransactionsRepository
{
public function bank_balances(){
$banks_data = Bank::all();
$banks_totals = [];
foreach ($banks_data as $bank){
$totals = (BankingTransactions::where('bank_id', $bank->id)->sum('amount')) -
((PettyCash::where('bank_id', $bank->id)->sum('amount')) + ((PayDoctor::where('bank_id', $bank->id)
->sum('total_paid'))));
array_push($banks_totals,
[
'id'=>$bank->id,
'bank'=>$bank->name,
'amount'=>$totals,
]);
}
return $banks_totals;
}
}
Replace your bank_balances method with this:
public function bank_balances($id = null) {
$banks_data = Bank::all();
$banks_totals = [];
if (is_null($id)) {
foreach ($banks_data as $bank) {
$totals = (BankingTransactions::where('bank_id', $bank->id)->sum('amount')) -
((PettyCash::where('bank_id', $bank->id)->sum('amount')) + ((PayDoctor::where('bank_id', $bank->id)
->sum('total_paid'))));
array_push(
$banks_totals,
[
'id' => $bank->id,
'bank' => $bank->name,
'amount' => $totals,
]
);
}
return $banks_totals;
} else {
return (BankingTransactions::where('bank_id', $id)->sum('amount')) -
((PettyCash::where('bank_id', $id)->sum('amount')) + ((PayDoctor::where('bank_id', $id)
->sum('total_paid'))));
}
}
And use it like this:
$bank_totals = $bank_totals->bank_balances($data['id']);

Attempt to read property post_id on int

I have a small response from db model, and i colud rebase it and response in route, but i see error.
My Controller:
class PostController extends Controller {
public function getLastRecord() {
$lastRec = Post::latest('created_at')->first();
$res = [];
$res = collect($lastRec)->map(function($record) {
return [
'id' => $record->post_id,
'rec_name' => $record->post_name,
'user' => $record->user_id,
];
})->toArray();
return $res;
}
}
I want that response will be array
['id': 1, 'rec_name': 'test_name', 'user': 1]
instead names from db
['post_id': 1, 'post_name': 'test_name', 'user_id': 1]
Error:
Attempt to read property "post_id" on int
When you collect() a single Record, then call ->map() (or other iterative methods), it loops over your Model's columns, not multiple Post records. You can solve this by wrapping $lastRec in an array, or using ->get() instead of ->first():
$lastRec = Post::latest('created_at')->first();
return collect([$lastRec])->map(function($record) {
return [
'id' => $record->post_id,
'rec_name' => $record->post_name,
'user' => $record->user_id,
];
})->toArray();
// OR
$lastRecs = Post::latest('created_at')->get();
return $lastRecs->map(function ($record) {
return [
'id' => $record->post_id,
'rec_name' => $record->post_name,
'user' => $record->user_id,
];
})->toArray();
Or, since this is a single record (using ->first() only ever returns 1 record), you don't need to collect() or map() at all:
$lastRec = Post::latest('created_at')->first();
return [
'id' => $lastRec->post_id,
'rec_name' => $lastRec->post_name,
'user' => $lastRec->user_id
];

How to save data when I delete a row or id?

My form is like this I have four row in database
May admin delete 2 rows or id.
How to save it
public function important_store(SettingRequest $request)
{
$importants = collect([]);
if ($request->importants) {
foreach ($request->helps as $help) {
$model = Important::updateOrCreate([
'title' => $help['title']
], [
'link' => $help['link']
]);
$importants->push($model);
}
}
Help::whereNotIn('id', $importants->pluck('id'))->delete();
return redirect()->back();
}

When collection->map returnes array then data in Collection raise error

In laravel 6 app I have collection defined as :
class PermissionCollection extends ResourceCollection
{
public static $wrap = 'permissions';
public function toArray($request)
{
return $this->collection->transform(function($permission){
return [
'id' => $permission->id,
'name' => $permission->name,
'is_checked' => !empty($permission->is_checked) ? $permission->is_checked : null,
'guard_name' => $permission->guard_name,
'created_at' => $permission->created_at,
];
});
}
}
I use it in a control, like :
$permissions = $user->permissions->all();
$userPermissionLabels= Permission
::get()
->map(function ($item) use($permissions) {
$is_checked= false;
foreach( $permissions as $nextPermission ) {
if($nextPermission->permission_id === $item->id) {
$is_checked= true;
break;
}
}
return [ 'id'=> $item->id, 'name'=> $item->name, 'is_checked' => $is_checked];
})
->all();
return (new PermissionCollection($userPermissionLabels));
and I got error :
Trying to get property 'id' of non-object
Looks like the reason is that collection->map returnes array of data, not objects.
If there is a way to fix it without creating new collection(using array) ?
MODIFIED :
I logged loging in my collection,
public function toArray($request)
{
return $this->collection->transform(function($permission){
\Log::info(' PermissionCollection $permission');
\Log::info($permission);
return [
'id' => $permission->id,
'name' => $permission->name,
'is_checked' => !empty($permission->is_checked) ? $permission->is_checked : null,
'guard_name' => $permission->guard_name,
'created_at' => $permission->created_at,
];
});
}
and I see in logs:
PermissionCollection $permission
array (
'id' => 1,
'name' => 'App admin',
'is_checked' => false,
)
local.ERROR: Trying to get property 'id' of non-object
The value is valid array, not null.
I mean I have already use this collenction in other part of the app, can I use it without creating a new one...
I think you get this error because you CollectionResource need to object of the Permission model, but in your case it is trying to get id from an array, after map function. Try to extend your model instead of returning an new array

yii2 sorting is not working

i want to sort the data fetched using id, i want to do it from controller but it's not working with both asc and desc,where i'm doing mistake? I hope someone can help me,thanks in advance
public function actionIndex()
{
$userid = Yii::$app->User->id;
$searchModel = new UservdoSearch();
$video= new ActiveDataProvider(
['query'=>Uservdo::find()->where('user_id=:uid',['uid'=>$userid])],**['sort' =>['defaultOrder'=>['id'=>SORT_DESC]]]**
);
return $this->render('index', [
'model' => $searchModel,'dataProvider'=>$video
]);
}
You should not use 'sort' as an array element remove ['sort'......] with 'sort'....
public function actionIndex()
{
$userid = Yii::$app->User->id;
$searchModel = new UservdoSearch();
$video= new ActiveDataProvider(
['query'=> Uservdo::find()->where('user_id=:uid',['uid'=>$userid]),
'sort' => ['defaultOrder'=>['id'=>SORT_DESC]],
);
return $this->render('index', [
'model' => $searchModel,'dataProvider'=>$video
]);
}
i got the answer bro,it should be inside query.thanks for your help.Answer is,['query'=>Uservdo::find()->where('user_id=:uid',['uid'=>$userid]),'sort' =>['defaultOrder'=>['id'=>SORT_DESC]]]

Resources