laravel update table returns error - laravel

I have the following issue when I try to update the laravel table I send my data via ajax everything is good. But the update returns an error.
the following function receives the data and updates the table.
public function saveCalendar(Request $request) {
$calendar = $request->calendar;
$apartment_id = $request->apartment_id;
apartments::where('Apartment_ID', $apartment_id)->update(array('calendar' => $calendar));
$confirmation = 'Календара е запазен успешно !';
return $confirmation;
}
I also tried this query:
apartments::where('Apartment_ID', $apartment_id)->update('calendar' => $calendar);
Any idea what am I doing wrong.

Error is because update function accepts array. Correct syntax is
apartments::where('Apartment_ID', $apartment_id)->update(['calendar' => $calendar]);
Also the correct syntax for fetching resquest inputs are
$calendar = $request->input('calendar');
$apartment_id = $request->input('apartment_id');

Related

How can I get only data from a response and show it into a blade template in Laravel 8?

Controller code
public function add_employee(){
$covid_statuses = Http::get("http://localhost:8000/api/v1/covid/status");
$covid_statuses = json_decode($covid_statuses['data'],200);
// $covid_statuses = json_encode($covid_statuses['data'],200);
return view('admin.hr.os.employee.add_employee',compact('covid_statuses'));
// return view('admin.hr.os.employee.add_employee',[
// 'covid_statuses' => $covid_statuses,
// // 'covid_statuses' => json_decode($covid_statuses['data']),
// ]);
}
The main respone is like {"data":[{"id":1,"name":"1st dose completed"},{"id":2,"name":"2nd dose completed"}],"success":true,"status":200}
When I decode only data, and pass into blade and in blade I use to debug like #foreach($covid_statuses as $covid_status) {{$covid_status}} #endforeach
but I got
json_decode() expects parameter 1 to be string, array given
When I use encode then I got
Invalid argument supplied for foreach()
How Can I solve this issue?
You could use array_walk for that purpose. Try:
$covid_statuses = array_walk($covid_statuses['data'], function(&$item, &$id) {
$item = $item['name'];
$id = $item['id'];
});
Like this, the array will only contain the names of the covid cases, by it will have the same id as in the original request

My recursive function in laravel does not call itself

I am writing a recursive function to call the child record from the parent record. It seems not to be working. i am getting this error; "Trying to get property 'refid' of non-object". Where am i getting it wrong. Please any idea? below is the code Snippet.
the function controller
public function DisplayDetail($id)
{
$displayDetail = DB::table('tblmembers')
->where('refid',$id)
->get();
return $this->DisplayDetail($displayDetail->refid);
}
main controller where the function is called
public function dashboard()
{
$profile = DB::table('tblmembers')->where('username',$userid)->first();
$data['userdetail'] = $this->DisplayDetail($profile->memid);
return view('main.userArea',$data);
}
the blade where the record fetched is displayed
#foreach($userdetail as $userd)
{{ $userd->memid }}
#endforeach
my sample data
refid | memid
-------------------
12345 | 123456
123456 | 1234567
123456 | 1234568
123456 | 1234569
1234567 | 1234570
from the above table; refid: 123456 brought memid: 1234567,1234568,1234569. then refid: 1234567 brought memid: 12345670
i want to display all the memid after login in as a user with memid 123456
You are doing one thing wrong in your function DisplayDetail. Here the the correction in your function
If you want to get single item then here is the correct code.
public function displayDetail($id)
{
$displayDetail = DB::table('tblmembers')
->where('refid',$id)
->first();
if($displayDetail) {
$displayDetail['userdetail'] = $this->displayDetail($displayDetail->refid);
}
return $displayDetail;
}
And dashboard function will be look like this
public function dashboard()
{
$profile=DB::table('tblmembers')->where('username',$userid)->first();
$userDetail = $this->DisplayDetail($profile->memid);
return view('main.userArea',[
'userdetail' => $userDetail
]);
}
This is the correct code. Try this and let me know if you have another query on this.
The error:
Trying to get property 'refid' of non-object
is occuring because your database query is using ->get(), which returns a Collection, rather than an object. You cannot get the property ->refid on a Collection, you can only get it from an object that resides in the collection.
As Lakhwinder Singh shows in his code, you need to use ->first(), as this will return one object. I would suggest using ->firstOrFail(), that way you will either get back an object which matches your ID, or it will fail if it cannot find it.
If you do:
$displayDetail = DB::table('tblmembers')
->where('refid',$id)
->firstOrFail();
You will now be able to call:
$displayDetail->refid
You can use that in your function call to displayDetail.
try this function
public function DisplayDetail($id,$data=[])
{
$displayDetail = DB::table('tblmembers')
->where('refid',$id)
->get();
if($displayDetail && isset($displayDetail->refid))// your condition for last child
{
$data = $this->DisplayDetail($displayDetail->refid,$data);
}
$data[] = array('refid' =>$displayDetail->id ,
'memid' => $displayDetail->secondid );
return $data;
}
i'll explain you later first modify according to your requirement and run

Laravel 5.4 controller function not able to use a get request parameter from ionic 3

I am try to pass a from ionic application to a laravel 5.4 application, and this parameter is an array, i have been able to pass the parameter successfully but i am being able to use the parameter to select records from the database.
Here is my ionic 3 provider function:
getMySmartQueues(data){
let params = new HttpParams();
params = params.append("sq_ids", JSON.stringify(data));
return this.http.get(this.url + 'my/smart/queues', {params: params});
}
And here is my laravel controller function:
public function getMySmartQueues(Request $request){
$ids = $request['sq_ids'];
$my_sq = SmartQueue::whereIn('id', $ids)->get();
return $my_sq;
}
And here is how i subcribe to the provider function is my page:
ionViewDidLoad() {
this.storage.get('sq_ids').then(
res => {
console.log(res);
if(res != null){
this.sq_ids= res;
console.log(this.sq_ids);
this.mService.getMySmartQueues(this.sq_ids).subscribe(
data => {
console.log(data);
}
);
}
}
);
}
But i get Server internal error. But if i have to hard code a default value for the controller function, let say like [5,6], it will return the records of this ids, but it can not returns the records of the ids sent from the ionic 3 application, will be glad if any one can help me out.
Also if i change the request to a put request i can get the records of the ids sent from the ionic application. But a get request is what i want.
if you want to use controller function with GET you need allow arguments in the Route to allow your id array.
for an example.
Route::get('your/url/{ids}', 'Controller#function')->name('mane_of_the_route');
and the controller function
public function getMySmartQueues(array $ids){
$my_sq = SmartQueue::whereIn('id', $ids)->get();
return $my_sq;
}
I have figure it out, and this is what i had to do, i think it might help someone one day, i just had to json_decode the request like so:
public function getMySmartQueues(Request $request){
$ids = $request['sq_ids'];
$my_sq = SmartQueue::whereIn('id', json_decode($ids))
->with(
'station.company'
)->get();
return $my_sq;
}

Where Clause in eloquent is responding with `No Properties`

I am new to laravel. I am trying to keep a where clause for the get method like this.
$employees = newdb::where('status', 'Active');
$response = $employees;
return response()->json($response,200);
But i am getting No Properties as output
In PHP I used this
"SELECT * FROM newdb WHERE status = 'Draft'";
What am i doing wrong? I tried different suggestions But none worked correctly. How do i do this? What is wrong with my code?
where method returns Builder object. So, you have to call get method on it to fetch data.
Try this
$employees = newdb::where('status', 'Active')->get();
return response()->json($employees, 200);

Error using save( ) in Laravel command

I am setting up my first Laravel command, but having an issue saving the the record.
My model:
Public static function contractorDecline(){
return DB::table('job_interviews')
->where('job_interviews.status', '=', 'awaiting contractor response')
->get();
}
I set up a command to change the status after 24 hours with no action:
public function fire()
{
//
$tooLate = Carbon::Now()->subHours(24);
$interviews = JobInterview::contractorDecline();
//
foreach($interviews as $interview){
if ($interview->response_received_on <= $tooLate){
$interview->status = 'contractor declined interview';
$interview->save();
}
}
return('finished');
}
When I run the command, I am getting the error:
"Call to undefined method stdClass::save()"
What do I need to add to the controller to be able to use save() in the command?
You are using Fluent which returns a simple object and calling Eloquent save method on it. If you want to update the row you should use Eloquent instead. Assuming you have a JobInterview class(Model) for the job_interviews table, you can code:
JobInterview::whereStatus('awaiting contractor response')->get();

Resources