Skip to content
Advertisement

problem displaying blade file getting error ” count(): Parameter must be an array or an object that implements Countable ”

I am having issues returning the view on my website i keep getting this error

ErrorException
count(): Parameter must be an array or an object that implements Countable (View: C:xampphtdocscargoresourcesviewsshipmentsingle.blade.php)

and this error points to a section in my blade file which is causing it, below is my blade file for the view

 <div class="card" >
    <div class="card-body ">
        @if(count($shipment) >0)
       @foreach($shipment as $shipment)

                <div class="shipment-number">
        <span class="title" style="display: block; font-size: 25px!important;">{{$shipment->tracking_code }}</span>
    </div><!-- Track_Num -->
</div>
<br><br>

<div id="shipper-info" class="row" >
        <div class="col-md-6 detail-section">
            <p class="shipper details"><strong>Shipper's Information</strong></p>
            <div class="shipper details">
            <p><span class="label">Shipper Name : </span>{{$shipment->sender_name }}</p>
            <p><span class="label">Phone Number : </span>{{$shipment->telephone }}</p>
            <p><span class="label">Email : </span>{{$shipment->sender_email }}</p>
            <p><span class="label">Address : </span>{{$shipment->sender_address }}</p>            
        </div>
    </div>
            <div class="col-md-6 detail-section">
            <p class="shipper details"><strong>Receiver's Information</strong></p>
            <div class="receiver details">
            <p><span class="label">Receiver Name : </span>{{$shipment->receiver_name }}</p>
            <p><span class="label">Phone Number : </span>{{$shipment->receiver_telephone }}</p>
            <p><span class="label">Address : </span>{{$shipment->receiver_address }}</p>
            <p><span class="label">Email : </span>{{$shipment->receiver_email }}</p>        
        </div>
    </div>
        <div class="clear-line"></div>
</div>
@endforeach
@esle
@endif

and the error points to this section

@if(count($shipment) >0)
       @foreach($shipment as $shipment)

here is my controller

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportStr;
use AppShipment;
use IlluminateFoundationAuthRegistersUsers;
use IlluminateSupportFacadesHash;
use IlluminateSupportFacadesValidator;
use AppHttpControllersController;
use IlluminateSupportFacadesDB;
use IlluminateSupportMessageBag;
use IlluminateSupportFacadesGate;




class ShipmentController extends Controller
{
   
    /**
     * Create a new user instance after a valid registration.
     *
     * @param  Request  $request
     * @return AppShipment
     */
    protected function store(Request $request)
    {
        
        $shipment = new Shipment();

        $data  = $this->validate($request, [
            'sender_name' => 'required|string|max:255',
            'sender_email' => 'required|string|email|max:255',
            'receiver_email' => 'required|string|email|max:255',
            'sender_address' => 'required|string',
            'estimated_Date' => 'required||date|after:today',
            'shipment_type' => 'required|string|max:255',
            'content' => 'required|string|max:255', 
            'receiver_name' => 'required|string|max:255', 
            'receiver_address' => 'required|string|max:255', 
            'sender_country' => 'required|string|max:255', 
            'sender_telephone' => 'required|string|max:255',
            'comments' => 'required|string|max:255',
            'receiver_telephone' => 'required|string|max:255',
            'receiver_country' => 'required|string|max:255',
            'package_weight' => 'required|string|max:255',
            
        ]);
        

        $shipment->sender_name = $request->input('sender_name');
        $shipment->receiver_email= $request->input('receiver_email');
        $shipment->sender_email= $request->input('sender_email');
        $shipment->sender_address= $request->input('sender_address');
        $shipment->estimated_Date= $request->input('estimated_Date');
        $shipment->shipment_type= $request->input('shipment_type');
        $shipment->content= $request->input('content');
        $shipment->receiver_name= $request->input('receiver_name');
        $shipment->receiver_address= $request->input('receiver_address');
        $shipment->sender_country= $request->input('sender_country');
        $shipment->sender_telephone= $request->input('sender_telephone');
        $shipment->comments= $request->input('comments');
        $shipment->receiver_telephone= $request->input('receiver_telephone');
        $shipment->receiver_country= $request->input('receiver_country');
        $shipment->package_weight= $request->input('package_weight');
        $shipment->tracking_code = strtoupper(Str::random(20));
        
        $shipment->save();   

           
            return redirect('/index')->with('Success', 'Your Shipment has been created');
       
    }

    public function tracking(Request $request){

        $this->validate($request,
            [
                'tracking_code' => 'required|max:25',

            ]);

        $shipment = Shipment::where('tracking_code', $request->input('tracking_code'))->first();
        
        if ( $shipment == null)
        {
            
            return redirect('/shipment/track')->with('error', 'Incorect Tracking Number');
            
        }
        else{
            return view('shipment.single')->with('shipment', $shipment);
        }

    }



    public function view(Shipment $shipment){

        $page_title = "view shipment";
        

       
        if (Gate::allows('isAdmin')) {
           return view('shipment.single',compact('shipment'. title));
    }
            
    else {
        return redirect('/home')->with('error', 'Unauthorized Page');
    }

and my routes

Route::Post('/tracking', 'ShipmentController@tracking')->name('tacking');
Route::get('/shipment/view/{shipment}', 'ShipmentController@view')->name('shipment.view');

i cant get around this and have been on it for a while now , any help will be appriciated, i would like for the view to be shown along with all the data parsed for both the input(tracking function) and the single blade (view function)

Advertisement

Answer

you don’t need to apply loop on single record as you can see it’s single record.

<div class="card" >
        <div class="card-body ">
    
                <div class="shipment-number">
        <span class="title" style="display: block; font-size: 25px!important;">{{$shipment->tracking_code }}</span>
    </div><!-- Track_Num -->
</div>
<br><br>

<div id="shipper-info" class="row" >
        <div class="col-md-6 detail-section">
            <p class="shipper details"><strong>Shipper's Information</strong></p>
            <div class="shipper details">
            <p><span class="label">Shipper Name : </span>{{$shipment->sender_name }}</p>
            <p><span class="label">Phone Number : </span>{{$shipment->telephone }}</p>
            <p><span class="label">Email : </span>{{$shipment->sender_email }}</p>
            <p><span class="label">Address : </span>{{$shipment->sender_address }}</p>            
        </div>
    </div>
            <div class="col-md-6 detail-section">
            <p class="shipper details"><strong>Receiver's Information</strong></p>
            <div class="receiver details">
            <p><span class="label">Receiver Name : </span>{{$shipment->receiver_name }}</p>
            <p><span class="label">Phone Number : </span>{{$shipment->receiver_telephone }}</p>
            <p><span class="label">Address : </span>{{$shipment->receiver_address }}</p>
            <p><span class="label">Email : </span>{{$shipment->receiver_email }}</p>        
        </div>
    </div>
        <div class="clear-line"></div>
</div>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement