I am trying to update method in Laravel but error is:
"Call to a member function tradereason() on boolean"
I also check same question of other people asked but there're a lot of different in my process. I have lot tables.
let me show you my create code and update method coding.
Create method code:
public function store(Request $request)
{
$tradeID= Auth::user()->trade()->create($input);
$input = $request->all();
$reasons = $request->input('reason');
//Loop for creating KEY as Value
$data = [];
foreach($reasons as $key => $value) {
$data[] = ['reason_id' => $value];
};
if( $data > 0 ) {
foreach ($data as $datum) {
$tradeID->tradereason()->save(new TradeReason($datum));
}
}
}
this is my tring code for update method:
public function update(Request $request, $id)
{
$tradeID= Auth::user()->trade()->whereId($id)->first()->update($input);
$input = $request->all();
$reasons = TradeReason::whereId($id)->first();
$reasons->update($input);
$reasons->tradereason()->sync($request->input('reason'));
$data = [];
foreach($reasons as $key => $value) {
$data[] = ['reason_id' => $value];
};
if( $data > 0 ) {
foreach ($data as $datum) {
$tradeID->tradereason()->whereId($id)->first()->update($datum);
}
}
}
update returns a boolean. So, don't overwrite $tradeID with the results of update.
$tradeID = Auth::user()->trade()->whereId($id)->first();
$tradeID->update($input);
Calling update on the Builder returns an 'int'. Calling update on the Model returns a 'bool'. They don't return Model instances.
// bool
$tradeID= Auth::user()->trade()->whereId($id)->first()->update($input);
The model instance would be what is returned from the first call:
$tradeID = Auth::user()->trade()->whereId($id)->first(); // assuming it finds a record
You can update that, you can use it in the foreach loop.
Related
i use laravel:command for function, which is i need to know how many reviews been given to my products, i will capture my database example below:
Product fields
Reviews fields
i've already declare the relationship which is:
App\Models\Product :
public function rev()
{
return $this->hasMany(Reviews::class, 'product_id', 'id');
}
App\Models\Review :
public function prod()
{
return $this->belongsTo(Products::class, 'product_id','id');
}
and this is my command for the function:
protected $signature = 'review:product {product_id}';
public function handle()
{
$id = $this->arguments('product_id');
$products = Products::with('rev')->whereId($id)->get();
foreach ($products as $key => $value) {
$rating = $value->rev->rating;
}
dd($products);
}
after i run my command it returns this error
Error
what i'm expecting is i could get the whole rating of specific id
You did not declare the variable $rating prior to using it.
But there is a much easier approach to your problem, since youve already setup the relationships.
public function handle()
{
$id = $this->arguments('product_id');
$products = Products::find($id);
$ratings = $products->rev->pluck('rating');
dd($ratings);
}
Because of product can have many revs, so you can use loop for this
foreach ($value->rev as $item) {
$val = $item->rating;
}
if you are expecting to sum up all rating for particular product id, then you should declare initial number for variable $rating before foreach, then sum up with += operator :
public function handle(){
$id = $this->arguments('product_id');
$products = Products::with('rev')->whereId($id)->get();
$rating = 0;
foreach ($products as $key => $value) {
$rating += $value->rev->rating;
}
dd($rating);
}
i want to update status receive to yes after that data also insert to other table multiple rows, i using ajax
this is my controller
public function bulkupdate(Request $request, ItemPR $item_code)
{
if($request->ajax())
{
$item_code = $request->item_code;
$item = ItemPR::whereIn('item_code', explode(",", $item_code))->update(['received'=> 'yes']);
$data = [];
foreach($item as $value){
$data[] = [
'item_code' => $value->item_code,
];
}}
WarehouseInventory::insert($data);
return response()->json(['success'=>"Products Updated successfully."]);
}
but i getting error like this
invalid argument for suplied foreach
how to fix that ?
plis help
thanks
I am using Angular2 for my frontend and laravel for my back end. I am trying to parse an array and store all of it into different rows but currently only the last record being recieved is being saved to the database.
public function SaveOrder(Request $request) {
$input = $request->all();
$order = new Order;
foreach ($input as $arr) {
foreach ($arr as $key => $value) {
if (array_key_exists($key, $arr) && !empty($value)) {
$order->$key = $value;
}
}
}
$order->save();
}
$input = $request->all();
foreach ($input as $arr) {
var_dump($arr);
foreach ($arr as $key => $value) {
if (array_key_exists($key, $arr) && !empty($value)) {
}
}
}
var_dump of the array
If you take a closer look, you'll notice your SaveOrder method loops through the given collection, sets the $order->$key = $value for productName and productDesc - and in the next iteration overwrites these values. That's why, when you save() the Order model, all you're inserting is the last (iterated) pair of values.
What you should do is to build an array of arrays, containing all "rows" to insert, and then perform a bulk insert.
Thanks #lesssugar I figured it out although I am having second thoughts of doing it this way since it seems inefficient
public function saveOrder(Request $request) {
$input = $request->all();
foreach ($input as $arr) {
foreach ($arr as $key => $value) {
if (array_key_exists($key, $arr) && !empty($value)) {
$data = array($arr);
}
}
DB::table('test')->insert($data);
}
}
My controller code
function edtpost($id)
{
$this->load->model('post');
$data = $this->post->edt_post($id);
$this->load->model('category');
$data['catname'] = $this->category->retrivecat();
$this->load->view('dashboard/edit_post',$data);
}
my model code
model post
public function edt_post($id)
{
$query = $this->db->get_where('post', array('id' => $id));
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
return $row;
}
}
return false;
}
category model
public function retrivecat()
{
$query = $this->db->get('category');
foreach ($query->result() as $row)
{
$data[] = $row->catname;
}
return $data;
}
in controller & this code $data['catname'] = $this->category->retrivecat();
I have one error:
Cannot use object of type stdClass as array
Just change your that line with following :-
$data->catname = $this->category->retrivecat();
codeIgniter form_dropdown() function can receive only associative array but I have multi dimension array by using result_array() function. How can I fetch my data on form_dropdown() function?
Let's say you want a dropdown of items, using result_array():
$query = $this->db->query("SELECT id, item_name FROM items");
$options = array();
foreach ($query->result_array() as $row){
$options[$row['id']] = $row['item_name'];
}
echo form_dropdown('my_items_dropdown', $options, '1');
I have extended the class DB_result.php in system/database with this new function
public function dropdown_array($id_field = FALSE, $list_field = FALSE)
{
$result = array();
if ($id_field === FALSE || $list_field === FALSE) return $result;
$array = $this->result_array();
foreach ($array as $value) {
$result[$value[$id_field]] = $value[$list_field];
}
return $result;
}
Now you can simply call from every Model class your new function to generate a dropdown-compliant array like this:
$customers = $this->db->get('customers');
$result = $customers->dropdown_array('id', 'name');
return $result;