How to get Laravel set table to 'stick' for collection - php

We have a Laravel model and sometimes we want to get data from it's default table, but other times from a different table. It all works from a data perspective, but when the collection is returned from the alternate table, the 'table' attribute still references the original table (even though the data is from the other one as expected).
If we dd() getTable(), it is correct:
$model = COUNTRY;
$table = "countires";
$org_id = 1;
$org_collection = $model->setTable('merge_' . $table)->get()
->where('organization_id', $org_id)->keyBy('id');
dd($model->getTable()); // *** THIS SHOWS THE 2nd TABLE AS EXPECTED
dd() ouput:
"merge_countries"
But with the exact same code, if we dd() the result the table attribute in the collection is still the 1st one ('orgs') and not 'merge_orgs' as expected even though the data is correctly coming from the 'merge_orgs' table:
$model = COUNTRY;
$table = "countires";
$org_id = 1;
$org_collection = $model->setTable('merge_' . $table)->get()
->where('organization_id', $org_id)->keyBy('id');
dd($org_collection); // *** THIS SHOWS THE 1st TABLE EVEN THOUGH THE getTable() ABOVE DOES NOT
dd() output:
Collection {#297
#items: array:1 [
3 => Country {#295
#connection: "common"
#table: "countries" <---- WHY IS THIS NOT 'merge_countries'???
#hidden: array:2 [
0 => "created_at"
1 => "updated_at"
]
#appends: array:1 [
0 => "level"
]
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [
"id" => 3
"organization_id" => 1
"region_id" => 2
"name" => "Southpark"
"active" => 1
"created_at" => "2018-06-21 14:05:36"
"updated_at" => "2018-06-21 13:25:27"
]
#original: array:7 [
"id" => 3
"organization_id" => 1
"region_id" => 2
"name" => "Southpark"
"active" => 1
"created_at" => "2018-06-21 14:05:36"
"updated_at" => "2018-06-21 13:25:27"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
}
]
}
Any idea how to address this? We're not sure what else to even try at this point. Laravel if 5.6 if that matters.

OK, there is a fix reference and more detail at github.com/laravel/framework/issues/26058. In the interim you can also override the model newInstance method in the trait to work around it:
public function newInstance($attributes = [], $exists = false)
{
// Overridden in order to allow for late table binding.
$model = parent::newInstance($attributes, $exists);
$model->setTable($this->table);
return $model;
}

Related

Laravel 5.8: Parameter passed to the event listener is getting null

I have created an Event called UserWalletNewTransaction.php and added this to it:
public $transaction;
public function __construct($transaction) {
$this->$transaction = $transaction;
}
And also registered it at EventServiceProivder.php:
use App\Listeners\UserWalletNotification;
protected $listen = [
UserWalletNewTransaction::class => [
UserWalletNotification::class,
],
Now in order to fire this event at the Controller, I coded this:
$newTransaction = UserWalletTransaction::create(['user_id' => $user_id, 'wallet_id' => $wallet_id, 'creator_id' => $creator_id, 'amount' => $amount_add_value, 'description' => $trans_desc]);
event(new UserWalletNewTransaction($newTransaction));
Then at the Listener, UserWalletNotification.php, I tried:
public function handle(UserWalletNewTransaction $event) {
dd($event->transaction);
}
But it returns null somehow.
However if I dd($event) instead, I get this:
So what is going wrong here? I can properly insert new record at user_wallet_transactions and the variable $newTransaction should contain the transaction information but it's not.
Here is also the Model UserWalletTransaction.php:
protected $fillable = ['user_id','wallet_id','amount','description','creator_id'];
protected $table = 'user_wallet_transactions';
I would really appreciate any idea or suggestion from you guys about this...
Thanks in advance.
UPDATE #1:
If I do dd($newTransaction) at the Controller before event(), I get this:
UPDATE #2:
Here is the full code of event, UserWalletNewTransaction.php:
class UserWalletNewTransaction
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public $transaction;
public function __construct($transaction)
{
$this->$transaction = $transaction;
}
}
UPDATE #3:
Here is the full result of dd($event) at the Listener:
App\Events\UserWalletNewTransaction {#2533 ▼
+newTransaction: null
+socket: null
+"{"user_id":"373","wallet_id":"2","creator_id":2,"amount":"-60","description":null,"updated_at":"2021-07-18 13:33:59","created_at":"2021-07-18 13:33:59","id":61}": App\UserWalletTransaction {#2535 ▼
#fillable: array:5 [▼
0 => "user_id"
1 => "wallet_id"
2 => "amount"
3 => "description"
4 => "creator_id"
]
#table: "user_wallet_transactions"
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: true
#attributes: array:8 [▼
"user_id" => "373"
"wallet_id" => "2"
"creator_id" => 2
"amount" => "-60"
"description" => null
"updated_at" => "2021-07-18 13:33:59"
"created_at" => "2021-07-18 13:33:59"
"id" => 61
]
#original: array:8 [▼
"user_id" => "373"
"wallet_id" => "2"
"creator_id" => 2
"amount" => "-60"
"description" => null
"updated_at" => "2021-07-18 13:33:59"
"created_at" => "2021-07-18 13:33:59"
"id" => 61
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▼
0 => "*"
]
}
}
I just figured out the problem... You are doing $this->$transaction when it should be $this->transaction.
I also had this problem, in case it is the case: remove the underscores in the properties of the event. ($_order has to be $order)

Laravel ManyToMany withPivot Trying to get property 'started_at' of non-object

I have a simeple ManyToMany relation between 2 models when I try to access a field in my pivot table I get this error:
Trying to get property 'started_at' of non-object
I have followed the instructions on the laravel documentation correctly.
My Models:
Instance:
public function steps(): BelongsToMany
{
return $this->belongsToMany(Step::class)
->using(InstanceStep::class)
->withTimestamps();
}
Step:
public function instances(): BelongsToMany
{
return $this->belongsToMany(Instance::class)
->using(InstanceStep::class)
->withPivot([
'started_at',
'finished_at',
'failed_at',
'output',
])
->withTimestamps();
}
How I try to access 'started_at' of a step:
dd($step->pivot->started_at);
EDIT
private function finishStep(Step $step)
{
dd($step->pivot->started_at);
}
/** #var Step $step */
foreach ($this->workflow->steps as $step) {
StepJob::dispatch($step, $instance);
}
dd($step)
Step {#420 ▼
#table: "workflow_steps"
#fillable: array:6 [▶]
#casts: array:3 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => "ca646f59-b215-41b0-afe6-ea9e6174f4f5"
"workflow_id" => "d786bd13-4111-4199-94d6-c52fef33b78b"
"category" => "transport"
"properties" => "props"
"entity_type" => "App\TransportWorkflowStep"
"entity_id" => "4d32e11c-6453-4f48-9419-7c5cbd647128"
"order" => 1
"created_at" => "2019-07-04 11:19:41"
"updated_at" => "2019-07-04 11:41:16"
"deleted_at" => null
]
#original: array:10 [▶]
#changes: []
#dates: array:1 [▶]
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
#forceDeleting: false
}
Attach step to instance
// set started_at
$workflowInstance->workflowsteps()->attach(
$step,
[
'started_at' => Carbon::now(),
]
);
// do something
// set finished_at when done
dd($step->pivot->started_at);
I did the dd($step->pivot->started_at); to see if i am working with the correct pivot row
You've not specified the relationship you're after, you may access the intermediate table using the pivot attribute on the models:
Try the following code:
private function finishStep(Step $step)
{
foreach ($step->instances as $instance) {
var_dump($instance->pivot->started_at);
}
die();
}
It depends on how declare $step, it gets collection and you have to insert it in foreach loop.
private function finishStep(Step $step)
{
dd($step->pivot->started_at);
}
this doesn't declare $step, you have to declare it first like
$step=Step::where(...)->get();
and then you can dd($step);
in short - object doesn't exist yet

How do I check if a row of a table was insert in laravel eloquent?

Before query all the information from DB I want to check if a row of a table was insert or not and if there are no any new insert I will not use query method.But I'm much understand about how to check up if it was inserted or not in Laravel5 with Eloquent library.
I've research some tutorial I found as below sql sentence but I don't how to used it in Eloquent and this sql is work as my expected or not.
On checkingIfNotificationUpdated method I want to checking for true and false when a row was insert into that tables only but as this sentence it will query all data
public function checkingIfNotificationUpdated(){
// SELECT * FROM INFORMATION_SCHEMA.'.$this->table.' WHERE DATE_SUB(NOW(), INTERVAL 1 HOUR) < UPDATE_TIME
RETURN self::select('*')->where(DB::raw('DATE_SUB(NOW(), INTERVAL 1 HOUR)'),'<','UPADTE_TIME')->get();
}
Here is my query method within checking Notification table is insert or not
public function getNotification($user_id, $gId)
{
if($this->checkingIfNotificationInserted() == true){
$this->_data = self::select('*')->join('users', 'users.id', '=', 'n_user_id')
->where('n_group_code_id','=', $gId)
->get();
if (count($this->_data)) {
return $this->_data;
} else {
return false;
}
}else{
// to go with old data.
}
}
Here is data my respond data when Used dd()
Collection {#619 ▼
#items: array:1 [▼
0 => Notification {#620 ▼
#table: "notification"
+timestamps: true
-_data: false
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:11 [▼
"n_id" => 269
"n_user_id" => 69
"n_group_code_id" => 11
"n_source_id" => 231
"n_activity_type" => "Issue Till"
"n_create_times" => "2016-04-06 09:04:40"
"n_description" => "Issue Till"
"n_status" => 0
"url" => "teller/trans_issues"
"updated_at" => "2016-04-06 09:45:40"
"created_at" => "2016-04-06 09:45:40"
]
#original: array:11 [▼
"n_id" => 269
"n_user_id" => 69
"n_group_code_id" => 11
"n_source_id" => 231
"n_activity_type" => "Issue Till"
"n_create_times" => "2016-04-06 09:04:40"
"n_description" => "Issue Till"
"n_status" => 0
"url" => "teller/trans_issues"
"updated_at" => "2016-04-06 09:45:40"
"created_at" => "2016-04-06 09:45:40"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▼
0 => "*"
]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
]
}
i think you can check with your input fields when you inserted:
Here,
in Controller :
$input = $request->all();
//check input value if exists or not in db...
$b_exists = ModelClass::where('title','=',$input['title'])->exists();
if($b_exists){
//show message: alteady exists
}
else{
//
....new data inserted
}
If you are using Eloquent there is no need to query back to the DB.
Assume you do an insert like this:
$myModel = ModelClass::create($params);
or this:
$myModel = new ModelClass($params);
$myModel->save();
You can then do this check:
if (! empty($myModel->id)) {
// Model has been successfully inserted
}

Laravel 5 can not access atributes from model

I got a slight issue,for some reason I'm unable to access my attributes from a relationship model.
I keep getting the same error, 'Undefined property: Illuminate\Database\Eloquent\Collection::$season'
I have been staring at this all day and I couldn't find a solution for it.
<?php namespace App\Http\Controllers;
use App\Show;
use App\Episode;
class TestController extends Controller {
public function test(){
$shows = Show::where('active',1)->get();
foreach ($shows as $show) {
if($show->active == 1 && $show->airday == date('N'))
{
echo 'test';
$episode = $show->episodes()->orderBy('id', 'desc')->take(1)->get();
\Debugbar::info($episode);
//this is the line where he gives me
echo('New episodes avaible, Season '.$episode->season.' Episode '.$episode->episode.' From '.$show->name);
//end error
}
}
}
}
?>
Although if I do a print out of the $episode variable I can clearly see the properties I wish to access.
Illuminate\Database\Eloquent\Collection {#150
#items: array:1 [
0 => App\Episode {#241
#table: "episodes"
+timestamps: true
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:6 [
"id" => "6"
"show_id" => "6"
"season" => "3"
"episode" => "15"
"created_at" => "2016-02-07 11:45:53"
"updated_at" => "2016-02-07 11:45:53"
]
#original: array:6 [
"id" => "6"
"show_id" => "6"
"season" => "3"
"episode" => "15"
"created_at" => "2016-02-07 11:45:53"
"updated_at" => "2016-02-07 11:45:53"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
At the moment I'm clueless whats wrong with the code.
I hope someone can point me in the right direction.
When you call ->take(1)->get() you're telling Eloquent (the builder, really) to give you back a Collection object with 1 item in it.
So when you're trying to access your $season property, you're actually trying to access it on a Collection object and not the Model that you think.
You can do a couple of things:
You can replace your ->take(1)->get() call with ->first(),
or you can treat your current $episode variable as a collection, and retrieve the actual episode you care about by accessing $episode[0],
or you can call first on your current $episode variable ($episode = $episode->first()) to get the first model object within it.

How to get an item from a collection in Laravel 5?

I'm new to Laravel and I cannot figure out how to get the value of an item from within the returned collection. Here is my code:
$aircraft = Aircraft::join('pams_shared.shared_aircraft', 'aircraft.shared_aircraft_id', '=', 'pams_shared.shared_aircraft.shared_aircraft_id')
->select('aircraft.*', 'shared_aircraft.*')
->where('aircraft.owner_id',Auth::user()->owner_id)
->where('registration',$reg)
->get();
I want to get the value from aircraft.aircraft_id but that's where I get stuck. I've tried:
$id = $aircraft->aircraft_id;
I've tried:
$id = $aircraft->aircraft.aircraft_id;
I've also tried:
$id = array_get($aircraft, 'aircraft_id');
But I'm getting null as a value. When I perform either a var_dump($aircraft) or dd($aircraft) I'm able to confirm that the data is there.
In my view I am able to access the value by looping through, but I need this data in my controller to perform a second query on another table.
Hope that makes sense. Cheers.
EDIT
When running dd($aircraft); I get this:
Collection {#239 ▼
#items: array:1 [▼
0 => Aircraft {#240 ▼
#fillable: array:8 [▶]
#table: "aircraft"
#primaryKey: "aircraft_id"
#connection: null
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:16 [▼
"aircraft_id" => 5
"owner_id" => 2
"shared_aircraft_id" => 67443
"year" => 2012
"serial_number" => "C127RG3"
"registration" => "C-SMTH"
"created_by" => 2
"modified_by" => 2
"created_at" => "0000-00-00 00:00:00"
"updated_at" => "0000-00-00 00:00:00"
"aircraft_code" => "2072438"
"aircraft_mfr" => "CESSNA"
"aircraft_model" => "172RG"
"aircraft_type" => 4
"aircraft_eng" => 1
"aircraft_cat" => 1
]
#original: array:16 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
]
}
When you use get() method on a query builder it would return an array no matter how many record it gets. So you can access it by pointing it by index. By the way do not forget to check for emptiness to overcome possible errors.
$id = 0;
$aircraft = Aircraft::join('pams_shared.shared_aircraft', 'aircraft.shared_aircraft_id', '=', 'pams_shared.shared_aircraft.shared_aircraft_id')
->select('aircraft.*', 'shared_aircraft.*')
->where('aircraft.owner_id',Auth::user()->owner_id)
->where('registration',$reg)
->get();
// now you can access properties
if(count($aircraft) > 0) $id = $aircraft[0]->aircraft_id;
If you execute the query with get() you will always get a collection as result. Even if you just need one model. The simplest way to change that is to use first():
$aircraft = Aircraft::join('pams_shared.shared_aircraft', 'aircraft.shared_aircraft_id', '=', 'pams_shared.shared_aircraft.shared_aircraft_id')
->select('aircraft.*', 'shared_aircraft.*')
->where('aircraft.owner_id',Auth::user()->owner_id)
->where('registration',$reg)
->first();
$id = $aircraft->aircraft_id;

Categories