i am trying to access property of object but while it shows the data is object but when i try to access the property it gives error 'Use of undefined constant 'id';
the code is ;
function returnProducts(Request $request,$id){
$user = Auth::user();
if($user->hasRole(['customer','bos_customer','admin','vendor','business_owner',
'merchant','city_manager','city_order_manager'])){
$sale = Sale::find($id);
$saleDetail=$sale->saleDetails;
$num=count($saleDetail);
return $request->product_ids[id];
the input recieved from the front-end is called from
return $request;
how can i access the object property i tried accesing it but cannot wrap my head around the problem i am facing
problem probably is here:
change this:
return $request->product_ids[id];
to:
return $request->product_ids['id'];
or:
return $request->product_ids[$id];
Related
I working in Laravel project and I can't call static method across variable
For example:
$objName = 'User';
$objName::get();
On this way I get error.
$objName = 'User';
Is a string, for using the get() method $objName should be an object, for example:
$objName = User::all()->first(); // this will return an object
Ok I use
User::all();
But I want get parametar from URL for example www.example.com/User, www.example.com/Articles -> User and Article is parametar in URL (this is Laravel web route) and call static method. When I wrtie first URL than call User object if I write first URL than call Article object.
www.example.com/User
$param= 'User';
$param::all();
www.example.com/Article
$param= 'Article';
$param::all()
I find solution!
This solution in Laravel:
$data = call_user_func( array('\App\\'.$param , 'all'));
but if you want use in plain PHP then is:
$data = call_user_func( array($param , 'all'));
This will call Object and Method.
If you want to send arg in methond thent is:
$data = call_user_func( array('\App\\'.$param , 'all'), $arg); /*For Laravel*/
$data = call_user_func( array($param , 'all'), $arg); /*For plain PHP*/
I try to display Google Map which is display an address from database(I have address column in DB table). For this I made a blade, and bind to route and path it with controller. I am having display issue.
My step is right below. I used this google API.
https://github.com/farhanwazir/laravelgooglemaps
And set the route:
Route::get('/show', 'PagesController#map');
Set the controller:
public function map(){
$config['center'] = allestates::where('address')->get();
$config['zoom'] = '10';
$config['map_width'] = '300px';
$config['scrollwheel'] = false;
GMaps::initialize($config);
$map = GMaps::create_map();
return view('pages.show',[ 'map' => $map]);
}
And in my blade. This is how I am calling it in body tag.
{{$map['html']}}
But getting this error.
Non-static method FarhanWazir\GoogleMaps\GMaps::initialize() should
not be called statically
Any idea what the problem is?
This code works for me:
$gmap = new GMaps();
$gmap->initialize($config);
$map = $gmap->create_map();
return view('your_view', compact('map'));
Notice that $gmap->create_map() is not a static calling.
Try to do it like this
return view('pages.show',[ 'map' => $map]);
return view(pages.show)->with(['map'=> $map]);
Also check if the value is not empty
Goodluck
I have a method, which takes a reference
// CarService.php
public function getCars(&$carCollection = null)
{
$promise = // guzzle request for getting all cars would be here
$promise->then(function (ResponseInterface $response) use (&$carCollection) {
$cars= json_decode($response->getBody(), true);
$carCollection= new CarCollection($cars);
});
}
However, when accessing the collection and trying to reuse it, I'm getting the error
Argument 1 passed to {placeholder} must be an instance of {placeholder}, null given
I know that the reason for this is, that the constructor returns nothing, but how can I still assign my variable to a new instance of the CarCollection (which extends Doctrine's ArrayCollection)
I even tried it with a static method as a work around
// CarCollection.php
public static function create(array $cars): CarCollection
{
$carCollection = new CarCollection($cars);
return $carCollection;
}
// CarService.php
public function getCars(&$carCollection = null)
{
$cars = // curl request for getting all cars would be here
$carCollection = CarCollection::create($cars)
}
but it's still null. Why is that? How can I set a referenced variable to a new class?
I access the method like this
$carService = $this->get('tzfrs.vehicle.services.car');
$carCollection = null;
$promises = [
$carService->getCars($carCollection)
];
\GuzzleHttp\Promise\unwrap($promises);
var_dump($carCollection); // null
When I set the reference directly, eg.
// CarService.php
public function getCars(&$carCollection = null)
{
$carCollection = new CarCollection([]);
}
it works without any problems. Seems like the callback is somehow the problem.
Whoever downvoted this, can you please elaborate why and why you voted to close?
I might be misunderstanding the question, but you should be able to modify an object when passing by reference. See here for an example: https://3v4l.org/KtFvZ
In the later example code that you added, you shouldn't pass $carCollection by reference, the & should only be in the method/function defintion, not provided when you call it. I don't think that is your problem though, that should be throwing an error in php7.
I want to give a 3rd party PHP application access to Yii2 user data (HumHub) and have tried this:
function getUserId() {
require_once('../protected/vendor/yiisoft/yii2/Yii.php');
$yiiConfig = require('../protected/config/common.php');
(new humhub\components\Application($yiiConfig));
$user = Yii::$app->user->identity;
return $user;
}
This does not work. There are no errors up until new humhub\components\Application($yiiConfig) but then the 3rd party app breaks with no error thrown and the function does not return anything.
I did find this solution which does not work.
Is there are reason this does not work or is there an alternate solution to getting Yii2 user data properly?
This is how to do it in HumHub V1.0
require_once('../protected/vendor/yiisoft/yii2/Yii.php');
$config = yii\helpers\ArrayHelper::merge(
require('../protected/humhub/config/common.php'),
require('../protected/humhub/config/web.php'),
(is_readable('../protected/config/dynamic.php')) ? require('../protected/config/dynamic.php') : [],
require('../protected/config/common.php'),
require('../protected/config/web.php')
);
new yii\web\Application($config); // No 'run()' invocation!
Now I can get $user object:
$user = Yii::$app->user->identity;
Indeed an error should be thrown, but, the error settings of your PHP may be overridden or set to not display errors.
You call undefined object Yii::$app->user->identity. The reason, from documentation because you have not initialized the Yii object. So your code should be as follows:
function getUserId() {
require_once('../protected/vendor/yiisoft/yii2/Yii.php');
$yiiConfig = require('../protected/config/common.php');
(new humhub\components\Application($yiiConfig)); // try to comment this line too if it does not work
// Add The Following Line
new yii\web\Application($yiiConfig); // Do NOT call run() here
$user = Yii::$app->user->identity;
return $user;
}
I'm trying to pass an array from a function to another function in laravel.
In my PageController.php, I have
public function show($code, $id){
//some code
if(isset($search))
dd($search);
}
and another function
public function search($code, $id){
//some queries
$result = DB::table('abd')->get();
return Redirect::action('PageController#show, ['search'=>$search]);
}
But this returns me an error like this: ErrorException (E_UNKNOWN)
Array to string conversion
I'm using laravel.
You could maybe get it to work with passing by the URL by serialization, but I'd rather store it in a session variable. The session class has this nice method called flash which will keep the variable for the next request and then automatically remove it.
Also, and that's just a guess, you probably need to use the index action for that, since show needs the id of a specific resource.
public function search($code, $id){
//some queries
$result = DB::table('abd')->get();
Session::flash('search', $search); // or rather $result?
return Redirect::action('PageController#index');
}
public function index($code){
//some code
if(Session::has('search')){
$search = Session::get('search');
dd($search);
}
}