mercredi 1 juillet 2020

Laravel 5.2 comparing greater then or equal to not working?

protected function appendCurrentLeased()
{
    $this->fields['current_leased'] = (!empty($this->data['lease_end'])
        && $this->data['lease_end'] != '0000-00-00')
        && ($this->data['lease_end']->gte(Carbon::today()->toDateString()))
            ? 1
            : 0;
}

With this function it isn't working, is there an alternative to gte that I could use?



via Chebli Mohamed

Autocomplete search with barcode scanner using Laravel & Vuejs

I am using Laravel & Vuejs & want to create invoice using barcode scanner.everything is working fine except barcode scan. in this stage how to insert row using barcode scanner? below my code examples.

addNewLine(){
this.form.items.push({
  barcode:null,
  name:null,
  price:0,
  qty:0,
  subtotal:0
})
}
<div<input type="search" v-model="barcode"></div>
<table>
<thead>
<tr>
<th>SL</th>
<th>Barcode</th>
<th>Item Name</th>
<th>Sale Price</th>
<th>Quantity</th>
<th>Subtotal</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in form.items">
<td></td>
<td><input type="text"v-model="item.barcode"/></td>
<td><input type="text"v-model="item.name"/></td>
<td><input type="text"v-model="item.price"/></td>
<td><input type="text"v-model="item.qty"/></td>
<td><input type="text"v-model="item.subtotal"/></td>
</tr>
</tbody>
</table>
<button class="btn btn-sm " @click="addNewLine">Add New Line</button>


via Chebli Mohamed

Laravel Create Product Order Api

I'm building an API for e-commerce app

now, i get stuck in creating order

i have the following Migrations

Orders

        Schema::create('orders', function (Blueprint $table) {
        $table->id();
        $table->string('order_number');
        $table->unsignedBigInteger('user_id');
        $table->enum('status', ['pending','processing','completed','decline'])->default('pending');
        $table->float('grand_total');
        $table->integer('item_count');
        $table->boolean('is_paid')->default(false);
        $table->enum('payment_method', ['cash_on_delivery'])->default('cash_on_delivery');
        $table->string('notes')->nullable();

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->timestamps();
    });

Order_items

        Schema::create('order_items', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('order_id');
        $table->unsignedBigInteger('product_id');

        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');

        $table->float('price');
        $table->integer('quantity');

        $table->timestamps();
    });

Products

Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->string('img');
        $table->string('name');
        $table->string('desc');
        $table->integer('price');
        $table->timestamps();
    });

and this is Models Relationship

Order Model

public function items()
{
    return $this->belongsToMany(Medicine::class, 'order_item','order_id','product_id')->withPivot('quantity','price');
}

public function user()
{
    return $this->belongsTo(User::class);
}

Controller

    public function store(Request $request)
{

    $order = new Order();
    $order->order_number = uniqid('ORD.');
    $order->user_id = 1;
    $order->item_count = 2;
    $order->grand_total = 20;
    $order->save();

            $items = $request->json()->all();
        foreach( $items as $item ){
            $orderItem = new OrderItem();
            $orderItem->order_id = $order->id;
            $orderItem->product_id = $item['product_id'];
            $orderItem->price = $item['price'];
            $orderItem->quantity = $item['quantity'];
            $orderItem->save();
        }

    return response(['message'=>'successful']);
}

Now, i can add Orders successfully .

but how to add items from JSON Request

for example by posting JSON Data from Postman

JSON Post Request

    [
    {
        "id":1,
        "product_id":4018,
     "price":20,
     "quantity":1
    },
    {
        "id":2,
        "product_id":4019,
     "price":50,
     "quantity":3
    },
    {
        "id":3,
        "product_id":4020,
     "price":45,
     "quantity":2
    }
]

any ideas?

Update

Photo of Post Request



via Chebli Mohamed

whereBetween on two columns in laravel on two time columns in laravel

Basically i have a project in which i have to create a reservation for anything say computer's reservation on cyber shop so suppose one user has start time of reservation is 6:00 PM to 7:00 PM.I have two columns to store the start and end time named start_time and end_time. Suppose second user came and creating a reservation for 6:30 PM to 7:30 Pm then this user get prompts that computer is booked in between this time,3rd scenario will be reservation from 5:30 PM to 6:30 Pm ,4th scenario will be 06:30 to 06:45 PM ,4th scenario will be 5:00 pPM to 8:00 PM. Simply we can say that if the start_time or end_time lies between any reservation then i have to prompt a error. I am unable to do the same validation.



via Chebli Mohamed

Laravel DB get sum of column of left joined table (sum of thumbs up/down against a post)

I have a reviews site, and users can thumbs up or thumbs down a review. The reviews table existed because I am using a Laravel boilerplate that I found. I've added the thumbs table using migrate and created a model and controller.

These are the two tables in question.

reviews
+----+-----------------+------------+-----------+---------+
| id | review_constant | title      | publish   | etc...  |
+----+-----------------+------------+-----------+---------+
| 1  | 1               | Test       | published | blah    |
| 2  | 2               | Test2      | old       | blah    |
| 3  | 2               | Test2 Edit | published | blah    |
+----+-------+---------+------------+-----------+---------+

review_thumbs
+----+---------+-----------------+-------+
| id | user_id | review_constant | thumb |
+----+---------+-----------------+-------+
| 1  |  12     |   2             |  1    |
| 2  |  10     |   2             | -1    |
| 3  |  8      |   2             |  1    |
| 4  |  17     |   2             |  1    |
+----+---------+-----------------+-------+

review_constant is the original reviews.id which is passed to any edits for a single review. We're only interested in rows where publish = 'published'.

I can get each published review no problem.

Reviews::where( 'publish', 'published' )

But I also want the SUM of thumbs that match that review's review_constant, which I can do with SQL

SELECT reviews.id, reviews.review_title, rt.review_constant, rt.thumbs
FROM reviews 
LEFT JOIN ( 
    SELECT review_constant, SUM(thumb) AS thumbs FROM review_thumbs 
) AS rt ON (rt.review_constant = reviews.review_constant)
where reviews.publish = 'published'

I'm struggling to do this in Laravel. I found some instructions telling me I need to add relationships like this

class ReviewThumbs extends Model
{
    public function reviews()
    {
        return $this->belongsTo(Posts::class, 'id', 'post_id');
    }
    ...

}

class Review extends Model
{

    public function reviewThumbs(){
        return $this->hasMany(ReviewThumbs::class, 'id', 'post_id');
    }
    ...
}

and then I can do this Reviews::where( 'publish', 'published' )->with('review_thumbs'), and that I should then be able to use it in a blade template. I don't know the correct syntax, and everything I do try throws an error. I've tried sum(), count(), using getReviewThumbsAttribute and I'm lost.

@if ($review->thumbs->sum() > 0 ) 
    +
@else
    
@endif

What should be in place of $review->thumbs->sum() and if I need to, what methods do I need to add to the PHP classes?



via Chebli Mohamed

How To Use Translation Method That is on Laravel Voyager Admin in My Own Control Panel.?

I am interested with Voyager Admin Panel , Exactly with Translation way, it is very easy for website administrator to add content with many languages at same time and storing them in database, using switch button in create content page. but, I made My own Control panel with custom Features like data tables and Export to excell , pdf , CSV ,, Except Translation ,, All packages doesn't require my Needs. Voyager Admin panel introduce this feature using trait an service provider, I am Not Expert with Laravel I Can't Know How to migrate them to my own project.? Any Help for this job.?



via Chebli Mohamed

Laravel 5.5 close Postgresql database connection

I searched on web but not found a solution to my problem.

My environment:

  • Laravel 5.5
  • PHP 7.2
  • Postgresql 12.3
  • Ubuntu 18.4

My problem is that DB::disconnect() doesn't close the connection.

My project have multiple database connection to Postgre and many jobs that use this connection.

Inside the job I want to disconnect from the default connection and connect to a specific one.

So if I run multiple times the job, it will create multiple connection and never close the old one.

I tried with DB::reconnect(), DB::disconnect() and DB::purge(), but the connection still open.

I read that the PDO connection is close when all references are set to null, it is possible that the framework keep some reference to the PDO connection and so it will never close?

I tried to make a simple script like:

DB::connect('some_connection'); 
DB::disconnect('some_connection'); 

But I can see the connection open on my database.

Any solutions?



via Chebli Mohamed