lundi 19 juillet 2021

Can you show database records on a View with no Controller in Laravel?

I am new to Laravel and I tried to create an application for an admin. I did other pages first where I ran queries to pull data from the database and display in tables. I made use of an admin middleware for security and if the user in an admin, they get taken to my Dashboard screen(which I left blank till now). Now I want to add items to that Dashboard page and some database records would need to be on it but I see that I have no controller for my Dashboard page. Is there any way to work around this so that I can run these queries?



via Chebli Mohamed

dimanche 18 juillet 2021

Pass my valuess from php to html in laravel [closed]

This is the output of SQL Query. I am using Laravel. I want to pass these values to chart i dont know how. I am new to this can any one help me.

[{"num":"2021-06-24","createdcount":1},{"num":"2021-07-02","createdcount":3},{"num":"2021-07-05","createdcount":1},{"num":"2021-07-06","createdcount":1},{"num":"2021-07-07","createdcount":2},{"num":"2021-07-08","createdcount":2},{"num":"2021-07-09","createdcount":2},{"num":"2021-07-10","createdcount":3},{"num":"2021-07-11","createdcount":2},{"num":"2021-07-12","createdcount":1},{"num":"2021-07-13","createdcount":2},{"num":"2021-07-14","createdcount":2},{"num":"2021-07-15","createdcount":2},{"num":"2021-07-16","createdcount":1},{"num":"2021-07-18","createdcount":1}]

But i want in this format

[{num:"2021-06-24","createdcount":1},{num:"2021-07-02","createdcount":3},{num:"2021-07-05","createdcount":1},{num:"2021-07-06","createdcount":1},{num:"2021-07-07","createdcount":2},{num:"2021-07-08","createdcount":2},{num:"2021-07-09","createdcount":2},{num:"2021-07-10","createdcount":3},{num:"2021-07-11","createdcount":2},{num:"2021-07-12","createdcount":1},{num:"2021-07-13","createdcount":2},{num:"2021-07-14","createdcount":2},{num:"2021-07-15","createdcount":2},{num:"2021-07-16","createdcount":1},{num:"2021-07-18","createdcount":1}]

can any one help me?



via Chebli Mohamed

Parse Json content in model in Laravel

I have a payment and Payment history models

Payment History:

class PaymentHistory extends Model
{
    public function subscription()
    {
        return $this->hasOne('App\Models\Payment', 'id','payment_id');
    }

}

Payment :

class Payment extends Model
    {
        public function PaymentHistory(){
        return $this->belongsTo('App\Models\PaymentHistory ','id','payment_id');
    }
    
    }

I have a json column named as response in Payment History table that i want to return it after decoding json data in model function with each and every row

My query in controller is as follows

 $data = User::with('Payment.PaymentHistory')->get();

i was trying to do something like this in payment history model:

   public function json_response() 
    {
        return json_decode($json, TRUE);
    }

Any help is highly appreciated



via Chebli Mohamed

samedi 17 juillet 2021

i found some syntax error using where function in query (laravel)

i have two condition which i want to implement in a query but when i used i found some systax error but i did not how to solved it. error in the below:

syntax error, unexpected '}'

and my controller code is :

public function paymentHubList(Request $request, $id){
        $Abc = new Abc();

        $Abc = $Abc->where('status_id','1')->where('payment','1')
                        ->where(function($query){
                            $query->where(function($query){
                                 $query->where('delivery',$id)->where('paid','!=','0');
                             })
                            ->orWhere(function($query){
                                 $query->where('branch',$id)->where('paid', '0');
                             })
                         })

                    ->orderBy('id','desc')->paginate(100);
        dd($Abc);

    }

in the where function i want to execute a and or condition. in the below i gave the condition:

@if((($Abc->delivery == $id) && ($Abc->paid != '0')) || ($Abc->branch == $id) && ($Abc->paid == '0'))


via Chebli Mohamed

How to count downlines in Laravel?

$i=$_SESSION['ruserid'];// the list will show the downline for a member with id = 10
        prin($i);// calling function prin
        function prin($i) //definition
        {
        $id=$i;
        // $q=mysql_query("select id from f2_users where up_id='$id'");
        $q=User::where('sponser_id',$i)->get();
        $count= count($q);// gives new value each time
        foreach($q as $r)
        {
        $i=$r[0];
        echo $i;
        echo "<br>";
        prin($i); // recursion
        }

        }

How to convert above php code in laravel controller to get downlines?

Blockquote



via Chebli Mohamed

Integrity constraint violation: 1048 Column 'cd_id' cannot be null in laravel

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'cd_id' cannot be null (SQL: insert into commvendordata (new_date, cd_id, cn_id, unit_id, vender1, vender2, vender3, vender4) values (07/17/2021, ?, ?, ?, ?, ?, ?, ?))

how remove this error using laravel

form or blade screenshot

<form action="" method="POST" class="shadow-lg p-4 w-f">
                    @csrf
                    <div class="modal-date">
                        <label>Select Date:&emsp;&emsp;</label>
                        <input type="text" name="new_date" placeholder="Select Price Date" class="getdate"/>
                        <button type="button" name="loadform" class="btn btn-warning load-form">Load Form</button>

                    </div>
                  <div class="data" style="display:none;">
                    <table class="table table-bordered" id="table">
                        <thead class="bg-success">
                            <tr>
                                <th scope="col" class="td-size">CCode</th>
                                <th scope="col" class="td-size">CName</th>
                                <th scope="col" class="td-size">CUnits</th>
                                <th scope="col" class="td-size">Vendor 1</th>
                                <th scope="col" class="td-size">Vendor 2</th>
                                <th scope="col" class="td-size">Vendor 3</th>
                                <th scope="col" class="td-size">Vendor 4</th>
                            </tr>
                        </thead>
                        
                            <tr id="template">

                                    <td><input type="text" name="ccode[]" class="td-size"></td>
                                    <td><input type="text" name="cname[]" class="td-size"></td>
                                    <td><input type="text" name="cunit[]" class="td-size"></td>
                                    <td><input type="text" name="vendor1[]" class="td-size"></td>
                                    <td><input type="text" name="vendor2[]" class="td-size"></td>
                                    <td><input type="text" name="vendor3[]" class="td-size"></td>
                                    <td><input type="text" name="vendor4[]" id="addrow" class="td-size"></td>
                            </tr>
                        
                    </table>
                  </div>
                  <div class="modal-footer">
                  <button type="submit" name="commdatasavebtn" class="btn btn-success">Save</button>
                </div>
                </form>

controller screenshot

function commvendordata(Request $request){

$cvendordata = new commvendordata;

$new_date = $request->new_date;
$ccode = $request->ccode;
$cname = $request->cname;
$cunit = $request->cunit;
$vender1 = $request->vendor1;
$vender2 = $request->vendor2;
$vender3 = $request->vendor3;
$vender4 = $request->vendor4;
    for($i=0; $i<count($ccode); $i++) {
            $commdata = [
                'new_date'  => $new_date,
                'cd_id'     => $ccode[$i],
                'cn_id'     => $cname[$i],
                'unit_id'   => $cunit[$i],
                'vender1'   => $vender1[$i],
                'vender2'   => $vender2[$i],
                'vender3'   => $vender3[$i],
                'vender4'   => $vender4[$i]
            ];
            DB::table('commvendordata')->insert($commdata);
    }
    Session::put('Success',"Save Data Successfully...!");
    return back();

}

database screenshot enter image description here

error screenshot enter image description here

enter image description here

form screenshots enter image description here



via Chebli Mohamed

vendredi 16 juillet 2021

Laravel join 3 tables using Query Builder

I have a function that expects a query builder as input parameter. On the query I want all the records returned from this query:

$query = $model->newQuery()
            ->leftJoin('materials', 'products.material_id', '=', 'materials.id')
            ->leftJoin('vehicles', 'products.vehicle_id', '=', 'vehicles.id')
            ->leftJoin('parts', 'products.part_id', '=', 'parts.id')
            ->select(
                'products.id as id',
                'products.name as product_name',
                'parts.part_name as part_name',
                'materials.public_name as material',
                'vehicles.name as vehicle'
            );

Using leftJoin the function runs but returns nothing and if I replace leftJoin for join (what I think it's the right solution for this case) vsc says it's not a Query Builder and executing returns 5xx error.

$model binds to my Product model.



via Chebli Mohamed