Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mateog98's avatar

Trying to access array offset on value of type null

Hi there i'm storing data in my db tables through forms inside a view. The view is about products which contain a brand and a model. Im storing in the products table the brand id and the model id, but what i want to show in my view is the brand name and not the id, and the same for the model.

						@foreach($file as $key => $product)
                            <tr>
                                <td style="display: none">{{ $key+1 }}</td>
                                <td>{{ $product['ptype']['productType']}}</td>
                                <td>{{ $product->brand_id}}</td>
                                <td>{{ $product->model_id}}</td>

When i show the productType it works fine but if i do:

       <td>{{ $product['brand']['brandName']}}</td>

i get the error: Trying to access array offset on value of type null

ProductController:

	public function add(){
    		$product['ptypes'] = Ptype::all();
    		$product['brands'] = Brand::all();
    		$product['models'] = ModelP::all();
    return view('backend.product.add-product', $product);
}

public function store(Request $request){
    
    $product = new Product();
    if($request->file('file')){
        $file = $request->file('file');
        $filename = time().'.'.$file->getClientOriginalName();
        $request->file->move('storage/', $filename);
        $product->file = $filename;
    }
    
    $product->ptype_id = $request->ptype_id;
    $product->brand_id = $request->brand_id;
    $product->model_id = $request->model_id;
    $product->description = $request->description;
    $product->fob = $request->fob;
    $product->sale_price = $request->sale_price;
    $product->buy_coin = $request->buy_coin;
    $product->sale_coin = $request->sale_coin;
    $product->quantity = '0';
    $product->created_by = Auth::user()->id;
    $product->save();
    Session::flash('success');
    return redirect()->route('products.view');
}

I've never seen a view error like this, and i dont have a clue how to fix it. I've look for similar questions in other forums but i still can't solve it.

0 likes
7 replies
automica's avatar

if your product has a brand relationship and its always present then you can do:

  <td>{{ $product->brand->brandName ]}}</td>

if its optional than the following with a check for the brand_id and display the name or a null

  <td>{{ $product->brand_id ? $product->brand->brandName : null ]}}</td>
mateog98's avatar

i tried doing

{{ $product->brand->brandName ]}}

and now i get a similar error:

Trying to get property 'brandName' of non-object
automica's avatar

{{ $product->brand->brandName ]}

will only work if all your products have a brand_id and you have a relationship set in your Product model for brand

{{ $product->brand_id ? $product->brand->brandName : null ]}}

will check first.

mateog98's avatar

This is my Product model:

  class Product extends Model
{
   public function category(){
      return $this->belongsTo(Category::class,'category_id','id'); 
 }


public function ptype(){
    return $this->belongsTo(Ptype::class,'ptype_id','id'); 
}

public function marca(){
    return $this->belongsTo(Brand::class, 'brand_id', 'id'); 
}

}

Snapey's avatar
Snapey
Best Answer
Level 122

so your relationship is called marca then you need to use that and not brand

1 like
automica's avatar

@snapey :P

@mateog98 it helpful to follow convention so that your model relationships match their respective table and class names.

Glad you got the answer even it @snapey snuck it in before me

1 like
mateog98's avatar

Thanks for the suggestion. I´ll work on it.

Please or to participate in this conversation.