My code is
public function listCompanies()
{
$companyObj = new company();
$companies = $companyObj->getCompanies();
$this->layout->content = View::make('admin/main',['companies'=>$companies]);
}
The error I am getting is
ErrorException
Creating default object from empty value
The error line is
$this->layout->content = View::make('admin/main',['companies'=>$companies]);
Set a:
protected $layout = 'your_layout_name';
Related
Trying to save data while open page but stuck at error :
"Method Illuminate\Database\Eloquent\Collection::save does not exist."
I have 2 database :
Buffalodata
Buffalomilkrecord
From 2nd table i need to get the avg of totalmilk and update the same to main database (1). This help me to show updated avgmilk data on dashboard front page.
Route:
Route:: get('buffalo-details', 'App\Http\Controllers\BuffalodataController#buffalodetails');
BuffalodataController Controller :
public function buffalodetails()
{
$buffalidforavgmilk = Buffalodata::groupBy('buffaloID')->get('buffaloID')->pluck('buffaloID')->toArray();
foreach ($buffalidforavgmilk as $id )
{
$milkperid = Buffalomilkrecord::where('buffaloID', $id)->sum('totalmilk');
$avgbuffalocount = Buffalomilkrecord::where('buffaloID',$id)->count();
$getavg = $milkperid / $avgbuffalocount;
$data = Buffalodata::find($buffalidforavgmilk);
$data->avgmilk = ($getavg);
$data->save ();
// dump([$milkperid,$avgbuffalocount,$getavg,$data,$id]);
}
return view ('pages.Buffalo.BuffaloDetails',[---------]);
}
Thanks again in Advance
When you pass an Array to ::find(), it returns a Collection, which doesn't have a save() method. This is your code:
// This is an Array of `buffaloID` values
$buffalidforavgmilk = Buffalodata::groupBy('buffaloID')->get('buffaloID')->pluck('buffaloID')->toArray();
...
// `$data` is now a `Collection` of `Buffalodata` instances
$data = Buffalodata::find($buffalidforavgmilk);
// This now fails, as `Collection` doesn't have a `save()` method
$data->save();
You can rewrite your code as follows:
Buffalodata::whereIn('buffaloID', $buffalidforavgmilk)->update(['avgmilk' => $getavg]);
This will update all records in a single call. If you want to iterate, that's an option too:
$data = Buffalodata::find($buffalidforavgmilk);
foreach ($data as $record) {
$record->avgmilk = $getavg;
$record->save();
}
Or, since you have $id already:
$record = Buffalodata::find($id);
$record->avgmilk = $getavg;
$record->save();
i'm trying to query two related model in the controller but the request failed.
here is my code:
$product = 'smartphone';
$prod = Produit::where('designation', $product)->with('tarifs')->get();
foreach($prod->tarifs as $tarif){
$tarif->prixAchat = $prix[$id];
$tarif->save();
}
this is the error message: Property [tarifs] does not exist on this collection instance.
i have this in the Produit model:
public function tarifs()
{
return $this->hasMany('App\Models\Tarif');
}
and the Tarif model:
public function produit()
{
return $this->belongsTo('App\Models\Produit');
}
you're getting a collection of products, and trying to get tarifs of it, thats why it throws the error, heres the solution
$product = 'smartphone';
$products = Produit::where('designation', $product)->get();
foreach($products as $prod){
foreach($prod->tarifs as $tarif){
$tarif->prixAchat = $prix[$id];
$tarif->save();
}
}
I try to dispatch a job in Laravel.
Job:
public $items;
public $milestone_delivery_date;
public $project_id;
public function __construct($items, $milestone_delivery_date, $project_id)
{
$this->items = $items;
$this->milestone_delivery_date = $milestone_delivery_date;
$this->project_id = $project_id;
}
PartController:
public function storeImportedParts(Request $request, Project $project)
{
$milestones = $project->milestones->where('material_delivery', 1);
$milestone_delivery_date = '';
if ($milestones->count()) {
$milestone_delivery_date = $milestones->first()->due_date;
}
$items = $request->items;
$this->dispatch(new StoreImportedParts($items, $milestone_delivery_date, $project));
In this case, the following error occurs:
[2020-01-31 08:23:16] local.ERROR: Too few arguments to function App\Jobs\StoreImportedParts::__construct(), 0 passed in /xxx/xx/xxx/xxx/app/Jobs/StoreImportedParts.php on line 37 and exactly 3 expected
???
If I change the vars with numbers like this, it works:
$this->dispatch(new StoreImportedParts(1, 1,1));
Of course, there is an error but the params arrive in the Job:
[2020-01-31 08:24:02] local.ERROR: Invalid argument supplied for foreach() {"userId":2,"exception":"[object]
In PHPStorm, the display is also different:
Why the params do not pass into the Job?
I Have 2 tables : Posts - Comment
I Want to Make Relation Between These 2 Tables and Need to Update The Comment That Has "1" Post_ID :
Route:
Route::get('join', 'JoinController#Join');
Controller :
public function Join()
{
$Comment = Posts::find(1)->Comment;
$Comment->Title = "JOIN";
$Comment->save();
}
Posts Model :
public function Comment()
{
return $this->hasOne('App\Comment');
}
Comment Model :
public function Posts()
{
return $this->belongsTo('App\Posts');
}
but i recieve this error :
Trying to get property of non-object
Do this instead:
$comment = new Comment;
$comment->Title = 'JOIN';
#comment->post_id = 1;
$comment->save();
Or you can use create() method:
$Comment = Posts::find(1)->Comment()->create(['Title' => 'JOIN']);
If you'll use create() method, make sure Title is in the $fillable array.
$Comment = Posts::find(1)->Comment()->first();
$Comment->Title = "JOIN";
$Comment->save();
SOLVED
I get this error when I got to a page where I call a method in template.
ErrorException in Product.php line 104:
Trying to get property of non-object (View: C:\wamp64\www\ibpc\resources\views\admin\products\edit.blade.php)
In blade template I am calling a method attribute:
{{ $product->attribute($attribute->id) }}
$product in template is passed from controller as:
$product = Product::with('categories.specifications.attributes')->find($id);
Method attribute in Product.php:
public function attribute($id)
{
$attribute = $this->attributes()->find($id);
$test = $attribute->pivot->value;
return $test;
}
I did debug with xdebug and everything seems to be fine:
Why the error?
If I do something like this then I get no errors:
public function attribute($id)
{
return 'Hello';
}
I had to check for empty values...
public function attribute($id)
{
$attribute = $this->attributes()->find($id);
if ($attribute) {
$test = $attribute->pivot->value;
} else {
$test = null;
}
return $test;
}