Search code examples
phplaravelcontrollerlaravel-bladelaravel-9

Laravel 9 Attempt to assign property "category_id" on null


I can't understand why it doesn't work, if the number is lower than 100000 everything works perfectly if I try to open this 684356 existing database it gives me this error, all higher numbers give me this error, lower numbers it works perfectly, from what does it depend?

An example if I open a category with number 31085 --> this works An example if I open a category with number 684356 --> Attempt to assign property "category_id" on null

Controller

    public function editAttributes(Request $request, $category_id){
        Session::put('page','tickets-log');
        $category_logs = TicketsLog::find($request->category_id);
        $category_logs->category_id= $request->category_id;
        $category_logs->category_name = $request->category_name;
        $category_logs->category_desc = $request->category_desc;
        $category_logs->category_not = $request->category_not ;
        $category_logs->category_not2 = $request->category_not2;
        $category_logs->category_not3 = $request->category_not3;
        $category_logs->category_not4 = $request->category_not4;
        if($category_logs->save()){
            return view('attributes.edit', compact('category_logs'));
        }
    }


Blade

<form enctype="multipart/form-data" action="{{url('edit-attributes')}}/{{ $category_logs->category_id }}" method="post">@csrf
                                        <div class="col-sm-6">
                                            <div class="card">
                                                <div class="card-body">
                                                    <h5 class="card-title">Category :</h5>
                                                    <input type="number" class="form-control" id="category_id" value="{{ $category_logs['category_id']}}" readonly="">
                                                </div>
                                            </div>
                                        </div>

Solution

  • I think the problem is the action attribute. Instead of

    action="{{url('edit-attributes')}}/{{ $category_logs->category_id }}" put action="{{ url('edit-attributes',$category_logs->category_id ) }}".
    

    I don't know how you defined the route.

    If you have the {category_id} attribute defined in the route, change

    TicketsLog::find($request->category_id); 
    in 
    TicketsLog::find($category_id);
    

    or If category_id isn't defined as primary key this table

     TicketsLog::where('category_id','=',$category_id)->first();