Query lastest Relationship, When Account was Created - laravel

I have two tables: Account and Transaction.
These tables relate to each other, by Account hasOne Transaction.
i want to show data in Transaction page, once Account was created.
My Account Model
public function transactions()
{
return $this->hasOne(\App\Transaction::class)->latest();
}
My Transaction Model
public function account()
{
return $this->belongsTo(\App\Account::class);
}
And my TransactionController
(I tried to implement code like below, but still no luck):
public function index()
{
$transactions = Transaction::with(['account' => function($query) {
$query->orderBy('id', 'desc')->first();
}]);
return response()->json(['transactions' => $transactions]);
}
Any Help? Thanks.....

QueryBuilder chains need either a get() or a first() or something of that nature at the end to execute the query, right now the variable you're passing to the view is the unexecuted query builder. Try this
$transactions = Transaction::with(['account' => function($query) {
$query->orderBy('id', 'desc')
}])->get();
Although, as TimLewis pointed out in the comments, if the relationship between transactions and accounts is one to one then there's no need to do any ordering of the account so it can just be this
$transactions = Transaction::with('account')->get();

Oooopppp i found what i want....
In my AccountController
public function store(Request $request)
{
$validateData = $request->validate([
'name' => 'required',
'code' => 'required',
]);
$account = new Account();
$account->user_id = auth()->user()->id;
$account->code = $request->code;
$account->name = $request->name;
$account->description = $request->description;
$account->balance = $request->balance;
$account->save();
$transaction = new \App\Transaction();
$transaction->credit = $request->balance;
$account->transaction()->save($transaction);
return response()->json(['created' => true]);
}
i have to save Transaction Relationship with Account.

Related

Laravel - Get array with relationship

I have an ajax call that returns an array:
$reports = Report::where('submission_id', $submissionID)
->where('status', 'pending')
->get(['description','rule']);
return [
'message' => 'Success.',
'reports' => $reports,
];
From this array, I only want to return the fields 'description' and 'rule'. However I also want to return the owner() relationship from the Report model. How could I do this? Do I have to load the relationship and do some kind of array push, or is there a more elegant solution?
You can use with() to eager load related model
$reports = Report::with('owner')
->where('submission_id', $submissionID)
->where('status', 'pending')
->get(['id','description','rule']);
Note you need to include id in get() from report model to map (owner) related model
you will have probably one to many relationship with Reports and owners table like below
Report Model
public function owner() {
return $this->belongsTo('App\Owner');
}
Owner Model
public function reports() {
return $this->hasMany('App\Report');
}
your controller code
$reports = Report::with('owner')->
where('submission_id', $submissionID)->where('status', 'pending')->get()
return [
'message' => 'Success.',
'reports' => $reports,
];
This is what I ended up going with:
$reports = Report::
with(['owner' => function($q)
{
$q->select('username', 'id');
}])
->where('submission_id', $submissionID)
->where('status', 'pending')
->select('description', 'rule','created_by')
->get();
The other answers were right, I needed to load in the ID of the user. But I had to use a function for it to work.

Get values from relationship Laravel

I have a query where I get values from 3 tables, for first 2 I use leftJoin, and is Ok, but for third one I try to get an array of objects, and I am not sure how.
In relationship table a have multiple rows for each ID from People table. HasMany type.
$q = Person::leftJoin('registers', 'people.register_id', '=', 'registers.id')
->leftJoin('relationships', 'people.id', '=', 'relationships.person_id') //if I comment this it works for first 2 tables
->find($id);
return response()->json($q);
Person
public function relationship()
{
return $this->hasMany(Relationship::class);
}
public function register()
{
return $this->belongsTo(Register::class);
}
Relationship
public function person()
{
return $this->belongsTo(Person::class, 'person_id');
}
Register
public function people(){
return $this->hasOne(Person::class);
}
UPDATE -> this works,but is kind of ugly, I think that should be a better way in Laravel
$q = Person::leftJoin('registers', 'people.register_id', '=', 'registers.id')
->find($id);
$q2 = Person::find($id)->relationship;
return response()->json([
'values' => $q,
'relationship' => $q2,
]);
You can just use with like this:
Person::with(['register', 'relationship'])->find($id);

How to manipulate a Laravel collection with related models and return a customized instance?

My model relation
return $this->hasMany('App\Models\Opportunity')->with('user');
My Attempt
$project = Project::find(1);
$$opportunities = $project->opportunities
->where('status', "confirmed");
$opportunities->each(function ($opportunity) {
return $opportunity->get('user');
});
Goal
My goal is to return the data in the following structure:
Opportunities:
Opportunity:
Status,
Amount
Currency
Name
Note that the user is a subset of the opportunity itself.
Problem
This returns a 1024 SQL error.
Ideally
It would be ideal if I can return all this information with the query itself.
Call get() method on your query to get its results first:
$oppurtunities = $project->opportunities()
->where('status', "confirmed")
->get();
You have eager loaded the user instance for each opportunity so just call $opportunity->user to return each opportunity's user:
$project = Project::find(1);
$opportunities = $project
->opportunities()
->where('status', "confirmed")
->get();
$filtered = $opportunities->map(function ($opportunity) {
return [
'status' => $opportunity->status,
'amount' => $opportunity->amount_pledged,
'currency' => $opportunity->currency,
'name' => optional($opportunity->user)->full_name
];
})->all();

How to queue the logics in controller

I have used two logic in my controller in my laravel project
public function multiStore()
{
$user = User::create([
'name'=>$request->name,
'email'=>$request->email,
'password'=>Hash::make($request->name),
]);
$post = MyPost::create([
'name'=>$request->post_name,
'body'=>$request->post_body,
]);
return redirect()->to('/admin/home);
}
Is it possible to make like if user is created successfully only then the post will be created so that I can use post created by user relationship
I have tried something like if condition but it is not working
You can try the code bellow , I assume you have a user_id field in posts table since you mentio9ned a relation ship. This is not the best way but i try to keep things simple, so I just edited the code.
Note : make sure you listed all the table fields in protected $fillable in your model before using Create()
public function multiStore()
{
$user = User::create([
'name'=>$request->name,
'email'=>$request->email,
'password'=>Hash::make($request->name),
]);
if($user){
$post = MyPost::create([
'user_id' => $user->id
'name'=>$request->post_name,
'body'=>$request->post_body,
]);
}
return redirect()->to('/admin/home);
}
Enclose your query in database transaction
https://laravel.com/docs/5.8/database#database-transactions
Either you can:
DB::transaction(function() {
Model::create();
AnotherModel::create();
});
Or you can use the following to find and catch error...
DB::beginTransaction();
// Your queries here...
// Model::create();
// AnotherModel::create();
DB::commit();

Laravel 5.3 Send database notifications

when admin adds a new appointment for a user, database notification should be created for all admins as well as the assigned user. And When viewing the notifications all the admins should see all notifications while users should see only notifications assigned for them.
public function submitAppointmentForm(Request $request){
$validator = Validator::make($request->all(), [
'respond' => 'required',
'user2_status' => 'required',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()->all()]);
}
else
{
$user = Auth::user();
$appointment = new Appointments();
$appointment->project_list_id = $request->project_id;
$appointment->respond = $request->respond;
$appointment->user2_status = $request->user2_status;
$appointment->date = $request->appointment_date;
$appointment->assigned_to = $request->assign_to;
$appointment->user2_note = $request->user2_note;
$appointment->assigned_by = $user->user_id;
$appointment->added_by = $user->user_id;
$appointment->save();
$assign_to = User::where('user_id', $request->assign_to)->first();
Notification::send($assign_to, new NewAppointmentNotification($request));
return response()->json(['success'=>'Successfully added']);
}
}
with above code notifications only added for assigned user. not for admins
how to add admins also when sending notifications
Notification::send($assign_to, new NewAppointmentNotification($request));
UPDATE :
Thanks to Dees Oomens i got it working i did a small modification as per my requirement
$assign_to = User::where('user_id', $request->assign_to)->first();
$users = User::whereHas('roles', function($q){
$q->where('name', 'admin');
})->get();
$users->push($assign_to);
Notification::send($users, new NewAppointmentNotification($request));
First you need to get all admins. You're using entrust so I'm not sure how what role name you've used, but my best guess would be:
$users = User::with(['roles' => function($query) {
$query->where('name', 'admin');
}])->where('id', '!=', $user->id)->get();
$users->push($assign_to);
Notification::send($users, new NewAppointmentNotification($request));
Now all users in the $users array will get the notification. And the $users array contains all admins (but not the current authenticated admin) and the user which is $assign_to.

Resources