ERROR: Object of class stdClass could not be converted to string - php

When I run the code below
$id = Input::get('branch_id');
$retailer_code = DB::table('branches')->select('retailer_code')->where('id', $id)->first();
$user = new User;
$user->user_firstname = Input::get('user_firstname');
$user->user_lastname = Input::get('user_lastname');
$user->user_email = Input::get('user_email');
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->position_id = Input::get('position_id');
$user->retailer_code = $retailer_code;
$user->branch = Input::get('branch_code');
$user->status = "1";
$user->save();
return Redirect::to('admin/users')->with('message', 'New User Added!');
I get this error "Object of class stdClass could not be converted to string"

$retailer_code which is stdClass and you cannot use directly as variable ,
$retailer_code = DB::table('branches')->select('retailer_code')->where('id', $id)->first();
will returns the retailer_code so
$retailer_code->retailer_code //would be the solution.
Please refer this link.

The problem is probably on this line:
$user->retailer_code = $retailer_code;
$retailer_code is an object and you need to get the property of it like this:
$user->retailer_code = $retailer_code->retailer_code;

You should change:
$user->retailer_code = $retailer_code;
into
$user->retailer_code = $retailer_code->retailer_code;
or you could modify your query:
$retailer_code = DB::table('branches')->where('id', $id)->pluck('retailer_code');
and then use:
$user->retailer_code = $retailer_code;
without a change

Related

How to get Multiple data in laravel

this is the controller im getting error Property [{$key}] does not exist on this collection instance.
public function showcustom($id)
{
$custompack = CustomPackages::find($id);
$venue = Venue::find($custompack->venue);
$food = Food::all();
$attire = Attire::all();
$hairmakeup = HairMakeup::all();
$invitation = Invitation::all();
$photosvideos = PhotosVideos::all();
$cakescupcakes = CakesCupcakes::all();
$lightsound = LightSound::all();
$programhost = ProgramHost::all();
$eventsingers = EventSingers::all();
//$custompack = CustomPackages::all();
return view('packages.customshow',compact('venue','custompack','packages','food','attire','hairmakeup','photosvideos','cakescupcakes','lightsound','programhost','eventsingers','invitation'));
}
Please check if the venue you are passing in Venue::find(integer_expected) is an integer..

PHP Fatal error: Class 'Eloquent' not found

I have a script
<?php
require_once "vendor/autoload.php";
Dotenv::load(__DIR__);
use \App\VSE, \App\User;
// use Eloquent;
$accounts = VSE::account_all();
$accounts = json_decode (json_encode ($accounts), FALSE);
foreach ($accounts as $account) {
$user = new User;
$user->id = $account->account_id;
$user->account_id = $account->account_id;
$user->email = $account->email_address;
$user->auto_provisioning = null;
$user->service_plan = null;
$user->fb_email = '';
$user->tw_email = '';
$user->fb_access_token = '';
$user->fb_profile_id = '';
$user->fb_page_id = '';
$user->fb_username = '';
$user->save();
}
?>
I kept getting
PHP Fatal error: Class 'Eloquent' not found in /Applications/MAMP/htdocs/code/portal/app/User.php on line 6
How do I avoid that ?
It looks like the User model is using Eloquent.
Are you trying to use Eloquent in a standalone app? There is some setup required in order to do this... see example here:
https://github.com/illuminate/database
What is in your composer.json?

Can't save Entity Even there is no Errors in CakePhp3

I Have user table and when i want to save it save() return null even when i debug $user there is no error..
'[errors]' => [],
This is my code :
$user = $this->Users->newEntity();
$tmp_hash = md5(rand(0, 1000));
$tmp_id = time();
/* Save individual data */
$user->id = $tmp_id;
$user->first_name = (!empty($user_profile->firstName)) ? $user_profile->firstName : "";
$user->last_name = (!empty($user_profile->lastName)) ? $user_profile->lastName : "";
$user->username = (!empty($user_profile->lastName) && !empty($user_profile->lastName)) ? $user_profile->firstName . "." . $user_profile->lastName : "";
$user->avatar = (!empty($user_profile->photoURL)) ? $user_profile->photoURL : "";
$user->role = "public";
$user->provider = $provider;
$user->provider_uid = $user_profile->identifier;
$user->gender = !empty($user_profile->gender) ? (($user_profile->gender == 'male') ? 'm' : 'f') : "";
$user->email = !empty($user_profile->email) ? $user_profile->email : "";
$user->password = $user_profile->identifier;
$user->link = $user_profile->profileURL;
$user->avatar = $user_profile->photoURL;
//$user->confirm_password = $user_profile->identifier;
$user->tmp_hash = $tmp_hash;
$user->isverified = (!empty($user_profile->emailVerified)) ? 1 : 0;
$user = $this->Users->patchEntity($user, $this->request->data);
debug($user);
$this->Users->save($user);
//debug($this->Users->invalidFields()); // he don't know invalidFields()
How can i show sql statement or something to debug and find the issue ?
die('2');
I found the solution..
The problem is errors() before saving it doesn't contain any message..
But after saving it show the errors..
Liket that:
$this->Users->save($user);
debug($user->errors());
Now i can sees the problems that i have .. these problems is due to unique validation..

Cannot use object of type Entity\X as array

I have little problem, I have no idea what's wrong with my code :/
$ilosc = $_POST["ilosc"];
$recipe = new Recipe();
$em = $this->getDoctrine()->getManager();
$repository = $this->getDoctrine()->getRepository('MainBundle:Recipe');
$query = $repository->createQueryBuilder('p')->select('p.id')->where('p.nazwa = :nazwa AND p.adres = :adres')->setParameters(array('nazwa' => $_POST["nazwaprzepisu"], 'adres' => $_POST["adresprzepisu"]))->getQuery();
$test = $query->getResult();
$id = $test[0]['id'];
$idprod = $_POST['idprod'];
$iloscprod = $_POST['iloscprod'];
for ($i = 0; $i < $ilosc; $i++)
{
$ingredient = new Ingredient();
$repositorying = $this->getDoctrine()->getRepository('MainBundle:Ingredient');
$query = $repositorying->createQueryBuilder('p')->select('p')->where('p.przepis_id = :id AND p.produkt_id = :idprod')->setParameters(array('id' => $id, 'idprod' => $idprod[$i]))->getQuery();
$result = $query->getResult();
if(!$result)
{
$ingredient->setProduktId($idprod[$i]);
$ingredient->setPrzepisId($id);
$ingredient->setIlosc($iloscprod[$i]);
}
else
{
$nowailosc = $result[0]['ilosc'] + $iloscprod[$i];
$stareid = $result[0]['id'];
echo "kokoko";
}
$em->persist($ingredient);
$em->flush();
}
I have problem with those two lines:
$nowailosc = $result[0]['ilosc'] + $iloscprod[$i];
$stareid = $result[0]['id'];
I get an error:
"Fatal error: Cannot use object of type My\MainBundle\Entity\Ingredient as array in".
Somebody have idea what I am doing wrong?
Problem solved. I had to use:
$nowailosc = $result[0]->getIlosc() + $iloscprod[$i];
$stareid = $result[0]->getId();

lithium insert entity array into model object property

The following coding sample doesn't work.
$joiner = new Entity();
$joiner->name = $post['name'];
$joiner->address = $post['address'];
$meeting = Meeting::first($_id);
$meeting->joiner[] = $joiner;
$meeting->save();
I hope each time when the form posted success. a new joiner entity should be pushed into the 'joiner' property of meeting object.
How to correct it ? and thanks for all help.
updated
now I have changed it like this .it works fine but still not very good yet.
$joiner = new Entity();
$joiner->name = $post['name'];
$joiner->address = $post['address'];
$meeting = Meeting::first($_id);
if(empty($meeting->joiner)){
$joiners = array($joiner);
}
else{
$joiners = array();
foreach($meeting->joiner as $one){
$joiners[] = $one;
}
$joiners[] = $joiner;
}
$meeting->joiner = $joiners;
$meeting->save();

Categories