【问题标题】:"Trying to get property of non-object" Error Controller“试图获取非对象的属性”错误控制器
【发布时间】:2019-03-13 12:31:22
【问题描述】:

如何解决(尝试获取非对象的属性)错误,但相同的函数在另一个函数中运行良好

这个控制器有什么问题,更新订单时总是出现这个错误 “尝试获取非对象的属性”,(请注意,我使用相同的函数进行存储,它工作正常。

我的数据库中有 4 个表(user_type_id、users、products 和 orders,orders 表包含 4 列(id、product_id、user_id 和 qty),user_id 和 product_id = users 和 products 表的外键。

我还想要一些我不知道如何正确编写的东西,我希望当客户删除一个订单并且它只是一个订单(数量 = 1)时,永久删除并且如果他输入了相同的数量,也被永久删除,因为当我测试这个功能时,我发现它一直删除到 -1 -2 -3 等..

如果产品数量为 0,我希望为除管理员和销售该产品的卖家之外的所有用户隐藏该产品,或者至少为所有人隐藏“添加到购物车”按钮或用无库存的死按钮替换它而不是添加到购物车,如果用户已经订购了一些此产品,他无法更新数量但可以取消它,那么它将再次添加到产品中..

我希望如果有人告诉我如何使用注释跨度,因为我也不知道该怎么做:D 谢谢

    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use App\Http\Requests;
    use App\Order;
    use App\Product;
    use Auth;
    use App\User;
    use App\Details;
    use Validator;

    class OrderController extends Controller
    {

        public function __construct()
        {
            $this->middleware('auth');
        }

        public function myorders()
        {
            $orders = Order::where('user_id','=',Auth::user()->id)->orderBy('id','desc')->get();
            return view('order.myorders',compact('products','orders','users'));
        }   






      public function buy(Request $id)
        {
            $product = Product::find($id);
            return view('product.index',compact('product','order','user'));
        }

    public function store(Request $request, $id)


            {
            $validator = Validator::make($request->all(), [
                'qty' => 'required|max:255',

            ]);

            if ($validator->fails()) {
                return redirect('')
                            ->withErrors($validator)
                            ->withInput();
            }

            $product = Product::find($id);
            $order = new Order();
            $order->user_id = Auth::user()->id;
            $order->product_id = $id;
            $order->qty = $request['qty'];
            $product->inv = $product->inv - $order->qty;
            $order->save();
            $product->save();
            return redirect('/');
        }


    public function edit(Request $id)
        {
            $product = Product::find($id);
            return view('order.myorders',compact('product','order','user'));
        }


    public function update(Request $request, $id)


            {
            $validator = Validator::make($request->all(), [
                'qty' => 'required|max:255',

            ]);

            if ($validator->fails()) {
                return redirect('')
                            ->withErrors($validator)
                            ->withInput();
            }


            $product = Product::find($id);
            $order = new Order();
            $order->user_id = Auth::user()->id;
            $order->product_id = $id;
            $order->qty = $request['qty'];                                  
  this line $product->inv = $product->inv - $order->qty; (it works in store)
            $order->save();
            $product->save();

            return redirect('/myorders');

        }


            public function delete($id)
        {
            $order = Order::find($id);
            return view('order.myorders',compact('product','order','user'));
        }


              public function destroy(Request $request, $id)
            {
            $validator = Validator::make($request->all(), [
                'qty' => 'required|max:255',

            ]);

            if ($validator->fails()) {
                return redirect('')
                            ->withErrors($validator)
                            ->withInput();
            }
            $order = Order::find($id);
        $product= Product::find($order->product_id);
        $order->qty = $order->qty - $request->input('qty');
        $product->inv = $product->inv + $order->qty;
        $product->save();
        $order->save();
            return redirect('/myorders');

        }  

    }

这是路由文件

Route::get('/myorders', 'OrderController@myorders');
Route::get('/add_order', 'OrderController@buy');
Route::get('/add_order/{id}', 'OrderController@store');
Route::get('/update_order', 'OrderController@edit');
Route::get('/update_order/{id}', 'OrderController@update');
Route::get('/delete_order', 'OrderController@delete');
Route::get('/delete_order/{id}', 'OrderController@destroy');

这是我的观点,我正在使用它

<div class="klaviyo_modal" id="update_order_qty" style="display:none;">
    <div class="klaviyo_inner">
        <a href="Cancel" class="klaviyo_close_modal klaviyo_header_close">×</a>
            <form method="GET" action="/update_order/{{ $order['id'] }}" enctype="multipart/form-data">
                {{ csrf_field() }}
                    <p class="klaviyo_header">Please update quantity as you need, then order it.</p>
                        <div class="klaviyo_fieldset">
                            <div class="{{ $errors->has('qty') ? ' has-error' : '' }}">
                                <label for="qty" style="display:block;text-align:center;margin-bottom:25px">Ordered New Quantity will be ADDED to your CURRENT Order Quantity</label>
                                    <input type="number" id="qty" class="qty" name="qty" style="display:block;margin:auto" required="required" placeholder="CURRENT Quantity">
                                    @if ($errors->has('qty'))
                                    <span class="help-block">
                                         <strong style="display:block;text-align:center">{{ $errors->first('qty') }}</strong>
                                    </span>
                                    @endif
                            </div>
                        </div>
          <div class="klaviyo_fine_print"></div>
              <div class="klaviyo_form_actions">
                 <button type="submit" class="klaviyo_submit_button">
                        <span>Update Order Quantity Now</span>
                 </button>
              </div>



            <!-- <span class="help-block">
                  <div class="success_message" style="display: block; text-align:center">Order Updated Successfully</div><br>
            </span>   
            <span class="help-block">  
                  <strong class="error_message" style="display: block; text-align:center">Sorry, Product Out of Stock, Try again later</strong>
            </span> -->        



            </form>
    </div>
</div>

【问题讨论】:

  • 嗨,欢迎来到 StackOverflow。请编辑您的问题,使其仅包含导致问题的特定代码(例如,您的 HTML 并不真正相关),并且还请提供您获得的确切输出和您想要的确切输出。如果您提供相关的数据库表结构和一些示例数据(必要时匿名),这也会很有用。所有这些都将帮助社区为您提供帮助。谢谢。

标签: php laravel view


【解决方案1】:

好的,谢谢大家:),我修好了,这是视图表单的操作:D action="/update_order/{{ $order['id'] }}" , It should be action="/update_order/{{ $order-&gt;product-&gt;id }}",,

现在:)

我想要一些我不知道如何正确书写的东西,我希望当客户删除一个订单并且它只是一个订单(数量 = 1)时,永久删除,并且如果他输入相同的数量,也被永久删除,因为当我测试此功能时,我发现它一直删除到 -1 -2 -3 等..

如果产品数量为 0,我希望为除管理员和销售该产品的卖家之外的所有用户隐藏该产品,或者至少为所有人隐藏“添加到购物车”按钮或用无库存的死按钮替换它而不是添加到购物车,如果用户已经订购了一些此产品,他无法更新数量但可以取消它,那么它将再次添加到产品中..

我希望如果有人告诉我如何使用注释跨度,因为我也不知道该怎么做:D 再次感谢...

【讨论】:

    【解决方案2】:

    您使用action(Request $id) 的所有操作都是错误的!

      public function buy(Request $id)
      {
           $product = Product::find($id);
           return view('product.index',compact('product','order','user'));
      }
    

    此操作的路线:Route::get('/add_order', 'OrderController@buy'); $id 应该是类 Request 的对象。但是您尝试将其用作Product::find($id); 中的整数值。当然 $product 变量为空!

    【讨论】:

      猜你喜欢
      • 2014-08-13
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      • 2016-06-13
      • 2018-06-02
      • 2017-08-20
      相关资源
      最近更新 更多