I want match data from "jobTable" based on job id then i got 5 data within this 5 data i have userId i want to show data from "userTable" based on userID. Then i will catch it on variable and i want to show it on my view then i will show it using loop.
If i do this below code it shows all data from my userTable not show based on matched JobID base UserId Details.
public function applyUser($jobId){
$applyUsers=ApplyInfo::where('job_id', $jobId)->get();
//5 rows found, with in this 5 rows everybody has userId I want to show data from user table based on this found userId and pass value a variable then i will loop it on view page.
foreach ($applyUsers as $applyUser){
$applyUsersDtials=Employee::find($applyUser->user_id)->get();
}
return view('front.user.apply-user-details', ['applyUsersDtials'=>$applyUsersDtials]);
}
//when i do that that time show all data based on my user table not show based on my JobId
First of all in your code $applyUsersDtials is a variable it should be an array. Next when you need to find a single row you just need to use find(). Try this-
public function applyUser($jobId){
$applyUsers = ApplyInfo::where('job_id', $jobId)->get();
$applyUsersDtials = [];
foreach ($applyUsers as $applyUser){
$applyUsersDtials[] = Employee::find($applyUser->user_id);
}
return view('front.user.apply-user-details',
['applyUsersDtials' => $applyUsersDtials]);
}
Related
I want to get the next id that will be created but not yet in laravel.
Is this code correct?
$tableStatus = DB::select("show table status from database_name where Name = 'table_name'");
if (empty($tableStatus)) {
throw new \Exception("Table not found");
}
// Get first table result, get its next auto incrementing value
echo $tableStatus[0]->Auto_increment;
Yes, the code is correct.
You can achive this in several other ways. For example, You can query the particular database table to extract last created id by ordering the rows based on created_at column or id column itself and finally increment the value by 1 to get next auto-increment value.
I am trying to fetch the multi same name to single name also sum associated QTY values also.Show PART-ID as it's
example
I want to show like this
my controller
public function productserach()
{
$searchitem = Item_list::get();
return view('products.product-serach',compact('searchitem'));
}
DB::table('your_table')->select(DB::raw('sum(qty) as qty,name,part_id'))->groupBy('name','part_id')->get()
So basically use raw select to sum qty you want and group by fields that you want to have unique. Also you can use laravel eloquent like above but change it a bit because it is incorrect the output data will be not same as you want.
you can do try this code
use DB;
$sums = Model::select(DB::raw('sum(qty) as total_quantity'),'name')->groupBy('name')->get();
I am using Laravel 5.8. I have this function in my api controller
$bill=new Bill();
$bill->msisdn =$msisdn;
$bill->trans_id =$transReference;
$bill->ext_id =$ref_id;
$bill->amount=$game_check->amount;
$bill->answer=$useroption;
$bill->game_code=$useresponse;
$bill->is_complete_transactions =1;
$bill->billing_channel="Unity-7799";
$bill->save();
I have another table user_response, the model class is User_Response. It has these fields:
id, msisdn, answer, answer_code
I want to get the last record from $bill=new Bill() where $bill->msisdn is equal to msisdn in User_Response. Then update answer in User_Response with $bill->answer.
Please how do I achieve this?
So save() method always return the recently save data in database.
if you do $bill->save()
so the last inserted record can be achive by the same object you trigger save() from.
For eg i need last inserted id from the database after insertion.
so
$bill = new Bill();
$bill->myFileds = $request->myfields;
$bill->save();
$lastInsertedId = $bill->id;
as you can see from above code. the last inserted record is $bill. you can use it for User_Repsonse
User_Response::where('msisdn',$bill->msisdn)->update(['answer'=>$bill->answer]);
Here you go
i've been building a basic laravel CRUD system, with that i've made a basic create funciton but i want to make it so that if a record already exists in the database and u create the same record again that the Integer u put in the create form add's up by the already existing record example: u have a stock system with a product that has the ammount of 50, and u create the same product with the ammount of 40, and that the 40 add's up with the 50 so that in the database it will say 90. im not entirely sure how to do so.
This is the store function i've made for my application:
public function store(Request $request)
{
// Aantal = ammount (int)
//Producten_Id = foreign_key from the Producten database table
//Locaties_Id = foreign_key from the locaties database table
Voorraad::Create($request->only(['aantal', 'Producten_Id', 'Locaties_Id']));
//var_dump($request);
return redirect(Route('voorraad.index'));
}
if u need more information just let me know and i'll add it to the question
Assuming that you set up default value for this field in database structure, its as simple as:
$voorraad = Voorraad::firstOrCreate($request->only(['Producten_Id', 'Locaties_Id']);
$voorraad->increment('aantal', $request->aantal);
If you want to set default amount for new products in the controller do this instead:
$voorraad = Voorraad::firstOrCreate($request->only(['Producten_Id', 'Locaties_Id']);
if (! $voorraad->aantal) {
$voorraad->aantal = 123; // default
}
else {
$voorraad->aantal += $request->aantal;
}
$voorraad->save();
Generally speaking firstOrCreate method fetches from database record with passed attributes if it exists already or otherwise creates it. See in Laravel docs.
If you have any questions just ask in comments, I'll be happy to explain.
I am having 2 tables, users and profiledetails
I am able to run Join query and access the data and send to view.
But when I am manipulation the field 'dob' (date format) in profiledetails table. No Success, Please check the code below ,
webpagesConroller:
$users = DB::table('users')
->join('profiledetails', 'users.id', '=', 'profiledetails.user_id')
->select('users.*', 'profiledetails.dob')
->get();
$age = $users->dob->diffInYears(Carbon::now());
return view('webpages.index',compact('users'))->with($age);
View:
<li class="cate_head">Age : {{ $age}}</li>
Error:
Trying to get property of non-object
I have model Profiledetails added the mutators as below,
public function getAge(){
return $this->dob->diffInYears(Carbon::now());
}
public function getDOB(){
return $this->dob->format('d-m-Y');
}
Can I not use this method on another controller for Ex- webpagesController, If yes How.
Since you are using a raw query, the data returned is not an object but an array with the data.
Also you did not limit the query, meaning it could return multiple rows.
You'll probably need to get the data from the $users as:
//with the possibily of multiple rows. Index is the row which has to be used
$users[index]['dob']
//when you are sure it only returns one row
$users[0]['dob'] // or $users['dob'] not sure as of the moment
You want to check the differences with Carbon.
So you will probably need to make a Carbon object of the dob result and check it against the Carbon::now.
$age = Carbon::createFromFormat('Y-m-d', $users[0]['dob'])->diffForHumans();
// diffForHumans() uses the local time
Note that this only gets the age for one row (the index or the first because of [0]).
If you want to do it for every row use a for or foreach where you set the $users->age for every $user. But as these are not object/models but raw data, this will not work.
Hope this helps your question a bit