Laravel Dataset Parsing Error - laravel

I need to parse this dataset I already but I tried several ways but it doesnt work.
{"id":2,"area_id":"1","payment_term_id":"1","payment_type_id":"1","trade_channel_id":"1","salesman_id":1,"customer_name":"xxx","contact_number":"1231212","fax_number":"1231232","home_address":"asd","delivery_address":"dasd","email_address":"test#edit.com","created_by":"4","updated_by":"4","is_active":"1","created_at":"2017-10-17 06:02:59","updated_at":"2017-10-17 06:13:22"}
I tried this one but This error says that
(1/1) ErrorException
Trying to get property of non-object
$details = $request->get('details');
return $details->id;
I also tried this one
$details = $request->get('details');
return $details['id'];
(1/1) ErrorException
Illegal string offset 'id'
I'm using laravel 5.4

Since the data comes from the request, it a JSON and not a collection or an object. So, you need to do something like this:
return json_decode($request->details)->id;

To use as array like $details['id'] try this :
$details = json_decode($request->details, true);
echo $details['id'];

To retrieve the JSON payload in your controller, use:
$details = json_decode($request->details)->id);
dd($details);
instead of
$details = $request->get('details');
return $details['id'];

Related

Not able to get Route parameter - Laravel 6

I have tried multiple solution but nothing worked yet, i am trying to get route Parameter in controller that was passed from a view.
Here is how i have created the route:
Route::get('addOptions/{questionId}', 'QuestionController#addOptions')->name('addOptions');
Here is how i am passing parameter to route from view:
Add Options
And here is what i am trying to get in controller but it's returning empty array:
public function addOptions(Request $request)
{
$allParameters = $request->input(); //not working
//$allParameters = $request->all(); //not working
//$allParameters = Input::all(); //not working
return $allParameters;
}
It returns empty array [] like this.
EDIT: But url at route addOptions look like this http://127.0.0.1:8000/admin/addOptions/4 in which 4 is questionId which means parameter is being passed but not retrieved.
What am I doing wrong here? Please guide, Thanks.
You should be passing the route like this:
Add Options
as for Laravel docs. the route params are passed an array with the key referencing the param
$url = route('profile', ['id' => 1]);
To retrieve the data in your controller, you should use:
$request->route()->paremeters()
or
$request->route('parameter_name')
If you want to pass the parameter
Add Options
I think your function parameters are wrong
You are passing question id from Route So your function should be like
public function addOptions($questionId)
{
$allParameters = $questionId; // you question ID passed throught Route
return $allParameters;
}

Laravel: Function name must be a string

I'm currently working on a project. It was all working just fine until i tried to migrate some tables that I edited. I got this error:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Function name must be a string
Since I doesn't directly show me where the error is, I couldn't find it. Last things I changed before I tried to migrate tables:
Migrations
Laravel colletive/html forms
Store method in my controller
As I know, migrations and forms shouldn't be a problem with this error, so here's my controller code:
public function store(Request $request)
{
$user = Auth::user();
$input = $request->all();
if ($file = $request->file('photo_id')){
$name = time().$file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
$user->posts()->create($input);
return redirect('/userPanel');
}
If the error isn't even in a controller code, where could it be. Any help appreciated.
I have found out it was because of a very silly mistake, i was calling a variable as a function...
$ownedMacs = intval($data()['owned_mac']);
Changed to
$ownedMacs = intval($data['owned_mac']);
This error message usually appears when we are doing something real stupid! :)
Same issue in Laravel can solve
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Function name must be a string
When you checked on storage/logs/laravel.log
local.ERROR: Function name must be a string {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Function name must be a string at E:\\Project\\workspace\\turttyKidsProject\\app\\Http\\Controllers\\SiteContactController.php:52)
[stacktrace]
Solution
Data field requesting another format but you are trying to save an object.
$objectSave= new ObjectClass;
$objectSave->feild_db = $request->post('request_name');
Check whether you access request with your form submit method. it may be POST or GET

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);

laravel update table returns error

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');

Laravel Pagination with appends() Error

I am new to Laravel and am using version 4.1.
I am attempting to query database using pagination and then run the results through the appends() function to add additional parameters to my URL.
Here is the code I am using
$query = DB::table('tableName');
$query->paginate(50);
$results = $query->get();
And that runs as desired. Now when I attempt to create the pagination list (Bootstrap default) and run the following code I get an error.
$pagination = $results->appends(array('key' => 'value'))->links();
This is the error I receive.
Call to a member function appends() on a non-object
I know I'm doing something wrong, I just can't figure out what...
Thanks in advance,
SC
I'm not familiar with the appends function, but you do have an error I can see. Try changing
$query = DB::table('tableName');
$query->paginate(50);
$results = $query->get();
To...
$query = DB::table('tableName');
if(Input::has('someinput')) {
$query->where('someinput', Input::get('someinput'));
}
if(Input::has('otherinput')) {
$query->where('otherinput', Input::get('otherinput'));
}
$results = $query->paginate(50);
Once you run paginate(), an instance of Paginator is returned. I don't see a get() method for Paginator though so I'm not sure how you weren't getting an error there.

Resources