Laravel 5 getparentsTree - laravel-5

Trying to get property 'title' of non-object (View: C:\php\laravellp\resources\views\admin\product_edit.blade.php)
C:\php\laravellp\app\Http\Controllers\Admin\CategoryController inner title line error i think
public static function getParentsTree($category,$title)
{
if ($category->parent_id == 0)
{
return $title;
}
$parent = Category::find($category->parent_id);
***$title = $parent->title. ' > ' . $title;***
return CategoryController::getParentsTree($parent,$title);
}

catagories table ss
database error. if you have already entered data in the category table in your database. If the product_id remains automatically assigned, it will be the value of product_id. The reason for the error here is that the product_id are confusing the database. I got rid of the error by setting the product_id in the table to 0.

Related

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'order clause'

I'm trying to make pagination after sorting in my Laravel project , so I did this method, it sorts my data but when I want to see next paginate table it shows me this error
SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'order clause'
SELECT * FROM `customers` ORDER BY `` DESC limit 3 OFFSET 3
My method
public function Sortfilter(Request $request)
{
$active = Customer::where('active','=','1')->count();
$inactive = Customer::where('active','=','0')->count();
$customer = Customer::query();
$customer = Customer::orderBy($request->filter,'desc')->paginate(3);
return view('customers.index', compact('customer','inactive','active'));
}
is there a method to save the $request->filter data when I click on next button
EDIT
when i sort my data the URL change like that : http://127.0.0.1:8000/sortCustomer?filter=phone&_token=9xw0q9MKa5ABZc4CwkLaPqf5ko4BhJ4ZaEk0VKYY
and when i click to the pagination button the URL be like that :
http://127.0.0.1:8000/sortCustomer?page=2
The problem is sometimes your $request->filter is empty, so adding a default value when it's empty fixes that.
public function Sortfilter(Request $request)
{
$active = Customer::where('active','=','1')->count();
$inactive = Customer::where('active','=','0')->count();
$customer = Customer::query();
$filter = $request->filter ?: 'created_at';
$customer = Customer::orderBy($filter,'desc')->paginate(3)>appends(['filter' => $request->filter]);
return view('customers.index', compact('customer','inactive','active'));
}
I think you are passing '' (null) in the first parameter of orderBy clause.
please confirm that the value in the $request->filter parameter not null and it should be a column name as in the customer table.
In Laravel there is the option to append query string values to pagination links.
Using this your code could look like this:
public function Sortfilter(Request $request)
{
$active = Customer::where('active','=','1')->count();
$inactive = Customer::where('active','=','0')->count();
$customer = Customer::query();
$customer = Customer::orderBy($request->filter,'desc')->paginate(3)->withQueryString();
return view('customers.index', compact('customer','inactive','active'));
}

Laravel : Update Stock Value After Order Placement

I want to update stock value when order will place. The challenge is that the products have different attributes place in the attribute table.
Small, Medium, Large, Extra large
I want to update the product attribute stock respectively when order is place. For example when user place order user selected products like
product_id = 1 size Small quantity is 7
So 7 quantity must be decrement from the product attribute size Small column.
Checkout
// checkout
public function checkout(Request $request)
{
if ($request->isMethod('post')) {
$data = $request->all();
DB::beginTransaction();
// Get cart details (Items)
$cartItems = Cart::where('user_id', Auth::user()->id)->get()->toArray();
foreach ($cartItems as $key => $item) {
# code...
$cartItem = new OrdersProduct;
$cartItem->order_id = $order_id;
$cartItem->user_id = Auth::user()->id;
// Get products details
$getProductDetails = Product::select('product_code', 'product_name', 'product_color')->where('id', $item['product_id'])->first()->toArray();
$cartItem->product_id = $item['product_id'];
$cartItem->product_code = $getProductDetails['product_code'];
$cartItem->product_name = $getProductDetails['product_name'];
$cartItem->product_color = $getProductDetails['product_color'];
$cartItem->product_size = $item['size'];
$getDiscountedAttrPrice = Product::getDiscountedAttrPrice($item['product_id'], $item['size']);
$cartItem->product_price = $getDiscountedAttrPrice['final_price'];
$cartItem->product_qty = $item['quantity'];
$cartItem->save();
// Want to Update the Product Attribute Table Stock
$item = new ProductsAttribute;
$item->where('product_id', '=', $item['product_id'], 'size', '=', $item['size'])->decrement('stock', $request->quantity);
}
// Insert Order Id in Session variable for Thanks page
Session::put('order_id', $order_id);
DB::commit();
}
}
When i run this code it shows me an error
InvalidArgumentException
Non-numeric value passed to decrement method.
When i enter value directly like decrement('stock', 7) it shows and error
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '`product_id` is null' at line 1 (SQL: update `products_attributes` set `stock` = `stock` - 7, `products_attributes`.`updated_at` = 2021-05-01 17:18:30 where size `product_id` is null)
i search alot but yet not find any solution. Please any one help
Just change the update query and make it simple and clean. The rest of the code is fine.
// Want to Update the Product Attribute Table Stock
$product_attribute = ProductsAttribute::where(['product_id' => $item['product_id'], 'size' => $item['size']])->first();
if($product_attribute){
$stock = $product_attribute->stock - (int) $request->quantity;
$product_attribute->update(['stock' => $stock]);
}
you have to warp it in an array or use two of wheres better and understandable
$item->where([
['product_id', '=', $item['product_id']],
['size', '=', $item['size']],
])->decrement('stock', (int) $request->quantity);
Or like this:
$item->where('product_id', $item['product_id'])
->where('size', $item['size'])
->decrement('stock', (int) $request->quantity);

How to update field in table Laravel or create?

I try to update field rate in table by primary key user_id.
So, I need to increment of decrement rate field. If row is not exist I need to create it.
I tried:
$this->point = -1; // Or $this->point = 1;
Rating::updateOrCreate(["user_id", $id], ["rate" => $this->point]);
But it does not work:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from `rating` where (`0` = user_id and `1` = 5) limit 1)
This is because your passing the column and the value as two separate values.
Change:
["user_id", $id]
to:
["user_id" => $id]
Hope this helps!
Laravel (eloquent) provides a fluent way to update related models.
Assuming the following model structure:
Rating.php
public function user()
{
return $this->belongsTo('App\User');
}
User.php
public function ratings()
{
return $this->hasMany('App\Rating');
}
You can easily update the relation via associate()
$user = User::findOrFail($userId);
$rating = Rating::findOrFail($ratingId);
$rating->user()->associate($user)->save();
Read more about Relationships in Eloquent

belongsTo not going to the right table

I have a controller with a function:
public function show(Activity $linkActivity, $id)
{
$activity = $this->activityRepository->getById($id);
$project = $linkActivity::find($activity->project_id)->project;
$employee = $linkActivity::find($activity->employee_id)->employee;
//\Debugbar::info($manager);
return view('activity/show', compact('activity'))->with('project',$project)->with('employee',$employee);
}
and here is my model Activity.php:
public function employee()
{
return $this->belongsTo('App\Employee');
}
The problem is that it is not looking at the right table and it is calling the activity table instead of the employee table. In the debugbar, I see the request to the db done which is:
select * from `activity` where `activity`.`id` = '2' limit 1
And id is well the 2 from the employee I m asking it but it looks in the wrong table.
Why is that?
You are searching for a row in Activity with Employee ID? I'm not sure what exactly you want, but if you want an employee why not simply do this
$employee = $activity->employee;
I think that the error is in find($activity->project_id).
In fact in the find, I need to put the id of the record holding the foreign key and not the id of the foreign key itself.

How to get property from second relationship table using eloquent?

I need help on how to get the property of relationship table using eloquent. I am building a list of users in HTML table and want to display info from three tables join together using eloquent relationship
Example:
I have 3 table users, users_profile, and branch. Each user can have one profile and one branch, and each branch can have multiple users.
users
---
id
username
email
users_profile
-----
id
user_id
branch_id
fullname
address
branch
-----
id
branch_name
User model
public function user_info()
{
return $this->hasOne('User_info','user_id','id');
}
User_info model
public function user() {
return $this->belongsTo('User');
}
public function branch() {
return $this->belongsTo('Branch');
}
Branch model
public function user_info()
{
return $this->hasMany('User_info','branch_id','id');
}
User controller
public function index()
{
$users = User::with('user_info')->get();
foreach($users as $value)
{
//show data from table users
echo $value->username;
echo $value->email;
//show data from table user_info
echo $value->user_info->fullname;
echo $value->user_info->address;
//how to display data from table branch?
//this code below will trigger property error
echo $value->user_info->branch->branch_name;
}
}
Question is how do I display data from table branch in the list as this code below will trigger Trying to get property of non-object error.
I suspect there is some mistake on how construct the eager loading but cannot find the proper way on how to do it.
echo $value->user_info->branch->branch_name;
Thanks in advance
I don't see any error here, you before displaying any property from relationship you should make sure it really exists. For example user might have not user_info and if he has, its user_info might not to have nay branch.
So correct code for display should look like this:
public function index()
{
$users = User::with('user_info')->get();
foreach($users as $value)
{
//show data from table users
echo $value->username;
echo $value->email;
//show data from table user_info
if ($value->user_info) {
echo $value->user_info->fullname;
echo $value->user_info->address;
//how to display data from table branch?
//this code below will trigger property error
if ($value->user_info->branch) {
echo $value->user_info->branch->branch_name;
}
}
}
}

Resources