mercredi 1 avril 2020

Laravel Mix - Is Chaining Required to Ensure Execution Order?

TLDR;

Do you have to chain Laravel Mix methods to maintain the execution order? Are any methods async that would prevent one from using the following non-chaining pattern, mix.scripts(); mix.js(); mix.sass();?

The few tests I've run suggest I do not need to chain.

An Example

Due to how our Laravel app is setup, we need to have more that one Laravel Mix setup. Instead of copy-n-pasting a webpack.mix.js file and modifying a few lines here and there in each file, we're looking at creating a config object that is passed to a singular webpack.mix.js file. In this file, we would check if various things have been configured, and if so, run the appropriate Mix method. Below is a pseudo-code example.

if ( config.js ) {
  mix.js( config.js.src, config.js.dist );
}

if ( config.sass ) {
  mix.sass( config.sass.src, config.sass.dist );
}

if ( config.concat ) {

  if ( config.concat.styles ) {

    // Could be more than one set of files that need to be combined, so array.
    config.concat.styles.map( ( files ) => {
      mix.styles( files.src, files.dist );
    }
  }

  if ( config.concat.scripts ) {

    // Could be more than one set of files that need to be combined, so array.
    config.concat.scripts.map( ( files ) => {
      mix.scripts( files.src, files.dist );
    }
  }

}

Currently, our code is more like most examples you see on the web.

mix
  .options()
  .webpackConfig()
  .styles()
  .styles()
  .scripts()
  .js()
  .sass();


via Chebli Mohamed

Query data laravel

i want to know how get this working in laravel :

I have two tables: "orders" and "delivery_boys" .

When an order is created i want to get "id" in table "delivery_boys" that correspond to the "store_id" in the "order" table.

which looks like that in mysql SELECT `id` FROM `delivery_boys` WHERE store_id = '3'

Thanks for your help.

I use this fonction:

 protected $table = 'orders';

   public function addNew($data)
   {
      $user                = AppUser::find($data['user_id']);


      if(isset($data['address']) && $data['address'] > 0)
      {
          $address = Address::find($data['address']);
      }
      else
      {
          $address = new Address;

      }

      $add                 = new Order;
      $add->user_id        = $data['user_id'];
      $add->store_id       = $this->getStore($data['cart_no']);
      $add->d_boy          = $add->?????;  <---- here i want to add from "delivery_boys" table the "id" that correspond to the "store_id" added just above from the "order" table ---->

      $add->name           = $user->name;
      $add->email          = $user->email;
      $add->phone          = $user->phone;
      $add->address        = $address->address;
      $add->lat            = $address->lat;
      $add->lng            = $address->lng;
      $add->address        = $address->address;
      $add->d_charges      = $this->getTotal($data['cart_no'])['d_charges'];
      $add->discount       = $this->getTotal($data['cart_no'])['discount'];
      $add->total          = $this->getTotal($data['cart_no'])['total'];
      $add->payment_method = $data['payment'];
      $add->payment_id     = isset($data['payment_id']) ? $data['payment_id'] : 0;
      $add->type           = isset($data['otype']) ? $data['otype'] : 1;
      $add->notes          = isset($data['notes']) ? $data['notes'] : null;
      $add->save();


via Chebli Mohamed

Como hace un Where de un append o ultimo elemento de una traza en Laravel 5.4 [closed]

pasa lo siguiente , tengo una un modelo de nombre

class Vehiculos {

protected $append = ['estado_vehiculo'] 
public function  _traza()
{
    return $this->morphMany('App\Comun\Trazabilidad\Trazabilidad','traceable');
}
public function getEstadoVehiculoAttribute()
{
    return $this->_traza()->orderBy('id','DESC')->first();
}

}

Necesito hacer lo siguiente $vehiculos = Vehiculos::where('estado_vehiculo',405)->paginate(); En otras palabras necesito filtra los vehiculos cuyo ultimo estado en la tabla morph sea 405.



via Chebli Mohamed

How can I display arrays one value in Laravel/PHP?

I have created an array and I want to display the array's value opens and closes

store_opens[store_times][monday][opens]

"store_opens" => array:1 [▼
    "store_times" => array:7 [▼
      "monday" => array:1 [▼
        "opens" => "09:00"
      ]
      "tuesday" => array:1 [▼
        "opens" => "09:00"
      ]
      "Wednesday" => array:1 [▼
        "opens" => "09:00"
      ]
      "Thursday" => array:1 [▼
        "opens" => "09:00"
      ]
      "Friday" => array:1 [▼
        "opens" => "09:00"
      ]
      "Saturday" => array:1 [▼
        "closes" => "10:00"
      ]
      "Sunday" => array:1 [▼
        "opens" => "10:00"
      ]
    ]
  ]

When I try to display it like this: $shop->jsonLDFields->store_closes. The result is all arrays values.

When I try to display it like this: $shop->jsonLDFields->store_closes['opens'] The result is null.

How could I display the opens and closes values in the view?



via Chebli Mohamed

laravel 7 in update function update error

I am trying to make a Laravel projact that allows a logged-in admin to edit products. The edit view works, but as soon as the changes are saved, it throws a Call to a member function update() on null error.

 public function edit($id)
    {
        $products = Product::with(['categories'])->find($id);
        $categories = Category::all();
        return view('products.edit', compact('products', 'categories'));
    }

    public function update(Request $request, $id)
    {
        $product = Product::find($id);
        $product->update($request->only(['name','description','weight','price']));
        $product->categories()->sync($request->get('category_id'));
        return redirect('/products');
    }

but i recived error Call to a member function update() on null

my view code is

 <div style="width: 50%;position: relative;left: 20%;top: 30px;">
        <form method="POST" action="">
            @csrf
            @method('PUT')
            <div class="card">
                <div class="card-body">
                    <h1 style="text-align: center">
                        Edit Product 
                    </h1>
                    <div class="form-group" style="display: grid;background: cornsilk;">
                        <div class="form-group">
                            <label for="name">Name:</label>
                            <input name="name" value="" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="description">Description:</label>
                            <input name="description" value="" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="weight">Weight:</label>
                            <input name="weight" value="" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="price">Price:</label>
                            <input name="price" value="" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="category">Category:</label>
                            <select name="category_id[]" class="form-control" multiple>
                                @foreach($categories as $category)
                                    <option value=""
                                        
                                    ></option>
                                @endforeach
                            </select>
                        </div>
                        {!!  Form::submit('submit',['class','btn btn-success']) !!}
                    </div>

                    @if ($errors->any())
                        <div class="alert alert-danger" style="direction: rtl;text-align: right">
                            <ul>
                                @foreach ($errors->all() as $error)
                                    <li></li>
                                @endforeach
                            </ul>
                        </div>
                    @endif
                </div>
            </div>
        </form>
    </div>


via Chebli Mohamed

Class 'App\Http\Controllers\Auth\GuzzleHttp\RequestOptions' not found

I have a simple registration form I would like to send post data to external service using guzzle

Here is what I have so far in the register controller

 protected function create(array $data)
    {
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            ]);

        $client = new Client();
        $response = $client->post('app.salesmanago.pl/api/contact/insert', [
            GuzzleHttp\RequestOptions::JSON => [        
            'allow_redirects' => true,
            'timeout' => 2000,
            'http_errors' => true,
            'headers' => [
                'Accept' => 'application/json, application/json',
                'Content-Type' => 'application/json',
                'clientId' => 'mykey',
                'apiKey' => 'mykey',

            ],
            'form_params' => [
                'name' => $data['name'],
                'email' => $data['email'],
            ]

          ]
        ]);


    dd($response);

        return $user;
    }

Now when I fill the form and send the form data I get the following error

Class 'App\Http\Controllers\Auth\GuzzleHttp\RequestOptions' not found

Note I have installed guzzle like this: composer require guzzlehttp/guzzle:~6.0

What am I doing wrong here?



via Chebli Mohamed

Trying to get property 'name' of non-object (View: /var/www/html/gandhisir_tests/resources/views/admin/institute/show_students.blade.php)

This is my Students model

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

This is my blade file code

      @foreach($lists as $index => $list)
                      <tr>
                        <td>      

                      </tr>
                       @endforeach

This is my InstituteController Code

 public function students()
    {
      $students = Students::all();  
      return view('admin/institute/show_students')->with('lists',$students);
    }


via Chebli Mohamed