AKmahato
6 months ago
35
10
Laravel

"Trying to get property 'id' of non-object (View: E:\LaravelProject\demoApplication-auth\resources\views\category\add_category.blade.php)"

Posted 6 months ago by AKmahato

I am new in laravel I don't know how to resolve this error please help me to resolve this error.

Model
------------------

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AddSubCategory extends Model
{
    protected $fillable = [
        'sc_name',
        'sc_desc',
        'cat_id'
      ];

      public function category(){
        return $this->belongTo('App\AddCategory');
      }
}


View
---------------------


@php
//$weekdays = weekdays();
$increment = autoincrement();
@endphp
@extends('layouts.app')

@extends('layouts.sidebar')

@section('title')
Add Category
@endsection

@section('content')

<div class="add_category">
    <!-- form card register -->
    <div class="card card-outline-secondary" id="card-category">
        <div class="card-header">
            <h3 class="mb-0">Add Category</h3>
        </div>
        <div class="card-body">
            <form method="POST" class="form" role="form" action="{{route('addcategory.store')}}">
                @csrf
                <div class="form-row">
                    <div class="form-group col-md-6">
                        <label for="c_id">Category ID</label>
                    <input class="form-control" name="c_id" value="{{ $increment }}" >
                    </div>
                    <div class="form-group col-md-6">
                        <label for="c_date">Category Date</label>
                        <!-- <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea> -->
                        <input class="form-control" name="c_date" type="text" value="{{date('Y-m-d')}}" required>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="c_name">Category Name</label>
                        <!-- <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea> -->
                        <input class="form-control" name="c_name" type="text" required>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="c_desc">Category Description</label>
                        <textarea name="c_desc" class="form-control" id="exampleFormControlTextarea1" rows="3" required></textarea>

                    </div>

                    <div class="form-group col-md-12">
                        <h3 class="mb-0 ">Add Sub Category</h3>
                    </div>


                    <!--form-row ended-->
                    <div class="form-row">
                        <div class="form-group col-md-4">
                            <label for="inputCity">Sub category name</label>
                            <input type="text" class="form-control" name="sc_name" required>
                        </div>
                        <div class="form-group col-md-4">
                            <label for="inputState">Sub category description</label>
                            <input type="text" class="form-control" name="sc_desc" required>
                        </div>
                        <div class="form-group col-md-4 btns">
                            <button class="btn btn-success " type="submit">Add</button>
                            <button class="btn btn-danger " type="submit">Remove</button>
                            
                        </div>
                        <div class="">

                        </div>
            
                <button class="btn btn-success btn-lg " type="submit">Add Category</button>

            </form>
        </div>
    </div><!-- /form card register -->
</div>
@endsection


Controller
---------------------------

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\AddCategory;
use App\AddSubCategory;
use DB;

class AddCategoryContoller extends Controller
{
    protected $category, $subcategory;

    public function __construct()
    {
        $this->category = new AddCategory();
        $this->subcategory = new AddSubCategory();
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list= AddCategory::all();
        return view('category.categorylist', compact('list'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('category.add_category');

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // $request->validate([
        //     // 'c_id' => 'required',
        //     'c_date'=>'required',
        //     'c_name'=>'required',
        //     'c_desc'=>'required',
        //   ]);
        //   $category = new AddCategory([
        //     'c_date' => $request->get('c_date'),
        //     'c_name' => $request->get('c_name'),
        //     'c_desc'=> $request->get('c_desc'),
        //   ]);

        DB::beginTransaction();
        try
        {
            $category = $this->category->create([
                'c_id' =>$request->c_id,
                'c_date' => $request->c_date,
                'c_name' => $request->c_name,
                'c_desc' => $request->c_desc
            ]);
    
            $subcategory = $this->subcategory->create([
                'sc_name' => $request->sc_name,
                'sc_desc' => $request->sc_desc,
                'cat_id' => $category->id
            ]);
            
            if($category && $subcategory)
            {
                DB::commit();
                
            }
            else{
                DB::rollback();
            }
            return redirect()->back();
        }
        catch(Exceptio $ex){
            DB::rollback();
            return redirect()->back();
        }
       
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
       // $category = AddCategory::find($id);
      // echo "Id is---------------".$id;

        // $query = DB::table('add_categories')
        // ->where(['add_sub_categories.sc_id',$id])
        // ->join('add_sub_categories', 'add_categories.id', '=', 'add_sub_categories.cat_id')
        // ->where('add_categories.id', '=', '$id') //Join the child table
        //->select('add_categories.*','add_sub_categories.*' )
        // ->get();

      //  var_dump($query);
      
    //    $query=DB::table('add_categories')
    //    ->where(['add_sub_categories.sc_id',$id])
    //    ->join('add_sub_categories','add_sub_categories.cat_id' , '=' , 'add_categories.id')
    //    ->select('add_categories.*','add_sub_categories.*' );

    //    var_dump($query);
       
       //return view('category.category_edit', compact('query'));

    //    DB::table('add_categories')->where('id', $id)->orderBy('id')->chunk(100, function ($add_categories) {
    //     foreach ($add_categories as $user) {
    //         var_dump($user);
    //     }
    // });

    $query = DB::table('add_categories')
                ->where('id', $id)
                ->join('add_sub_categories','add_sub_categories.cat_id' , '=' , 'add_categories.id')
                ->select('add_categories.*','add_sub_categories.*' )
                ->get();

                //var_dump($query);

                return view('category.category_edit', compact('query'));

    }
    

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
     
        echo "This is the id :". $id;
        
            $request->c_name =$_POST['c_name'];
            $request->c_desc =$_POST['c_desc'];
            $request->sc_name =$_POST['sc_name'];
            $request->sc_desc =$_POST['sc_desc'];
        
        $category = AddCategory::find($id);

        AddCategory::where('id', $id)->update(array(
            'c_name' =>  $request->c_name,
            'c_desc' =>  $request->c_desc
        ));
        AddSubCategory::where('cat_id', $id)->update(array(
            'sc_name' =>  $request->sc_name,
            'sc_desc' =>  $request->sc_desc
        ));
       // return Redirect::route('all-customers')->with('message', 'You have successfully updated');
    
        
   
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}


Helper file where this error is occuring
---------------------------------------------------------------

<?php

use Illuminate\Support\Facades\Input;
use App\AddCategory;
if(!function_exists('autoincrement')) {
  function autoincrement() {

     // $student = Student::orderby('id', '=', Input::get('name'))->first();
       $category = AddCategory::orderby('id', 'desc')->first();
      $incrementid = $category->id;

      if ($category === null) {
        $nextId = 'MCGID0000001';
      }
      else {
        //$nextIntId = substr('MCGID0000001',5);
        $nextIntId = $incrementid + 1;
        //$nextId = 'MCGID'. sprintf("%07d", intval($nextIntId) + 1);
        $nextId = 'MCGID00000'.$nextIntId;
      }
      return $nextId;
    }
  }

?>

Please sign in or create an account to participate in this conversation.