mardi 29 janvier 2019

How to reduce the product stock when the item is used

This is my 1st table

public function up()
    {
        Schema::create('perbaikans', function (Blueprint $table) {
            $table->increments('id_perbaikan');
            $table->string('nomor_dokumen_perbaikan', 25)->unique();
            $table->integer('id_teknisi')->unsigned();
            $table->integer('id_kulkas')->unsigned();
            $table->integer('id_tipe_pekerjaan')->unsigned();
            $table->string('temuan_masalah', 100);
            $table->date('tanggal_perbaikan');
            $table->timestamps();
            $table->foreign('id_teknisi')->references('id_teknisi')->on('teknisis');
            $table->foreign('id_kulkas')->references('id_kulkas')->on('kulkas');
            $table->foreign('id_tipe_pekerjaan')->references('id_tipe_pekerjaan')->on('tipe_pekerjaans');
        });
    }

This my 2nd table

public function up()
    {
        Schema::create('sukucadangs', function (Blueprint $table) {
            $table->increments('id_sukucadang');
            $table->string('nomor_sukucadang', 5)->unique();
            $table->string('nama_sukucadang', 35);
            $table->integer('stok');
            $table->integer('id_kategori_sukucadang')->unsigned();
            $table->timestamps();
            $table->foreign('id_kategori_sukucadang')->references('id_kategori_sukucadang')->on('kategori_sukucadangs');
        });
    }

And this is my intermediate table

public function up()
    {
        Schema::create('pemakaian_sukucadangs', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('id_perbaikan')->unsigned();
            $table->integer('id_sukucadang')->unsigned();
            $table->integer('qty');
            $table->foreign('id_perbaikan')->references('id_perbaikan')->on('perbaikans');
            $table->foreign('id_sukucadang')->references('id_sukucadang')->on('sukucadangs');
        });
    }

My Controller

class PerbaikanController extends Controller
{
    public function create()
    {
        $teknisis = Teknisi::all();
        $kulkas = Kulkas::all();
        $tipepekerjaans = TipePekerjaan::all();
        $sukucadangs = Sukucadang::all();
        return view('perbaikan.tambah', compact('teknisis', 'kulkas', 'tipepekerjaans', 'sukucadangs'));
    }


    public function store(Request $request)
    {
      
        $perbaikan = new Perbaikan;

        $perbaikan->nomor_dokumen_perbaikan = $request->nomor_dokumen_perbaikan;
        $perbaikan->id_teknisi = $request->id_teknisi;
        $perbaikan->id_kulkas = $request->id_kulkas;
        $perbaikan->id_tipe_pekerjaan = $request->id_tipe_pekerjaan;
        $perbaikan->temuan_masalah = $request->temuan_masalah;
        $perbaikan->tanggal_perbaikan = $request->tanggal_perbaikan;
        $perbaikan->qty = $request->qty;

        $perbaikan->save();

        $sukucadang = Sukucadang::findOrFail($request->id_sukucadang);
        $sukucadang->stok -= $request->qty;
        $sukucadang->update();

        $perbaikan->sukucadang()->sync($request->sukucadang, false);

    }
}

How to reduce stok in sukucadangs tables if the item is used. For example, I have item1 with stock 10, and item2 20, when I use item1 and item2 with each qty 2 and 3, then the stock of items in the sukucadangs table will automatically decrease Thank you very much



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2sRcsXK
via IFTTT

Choose recipient email on Laravel email verification

I had implemented Laravel email verification (https://laravel.com/docs/5.7/verification) in my Laravel application.
By default, when a user is registered, the verification email is sent to the user. However, what i want is to send a verification email to my own email, that is, to choose the recipient, so the site administrator (in this case, me) can approve the user registration himself.
Is there any way to do this? How could it be done?



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2S0ChDX
via IFTTT

How do you add extra fields to the database when a user registers in Laravel?

I am trying to add a default value into my users table when a user registers. I have a trait which I use for my encryption and decryption with LibSodium.

This is the method that I am calling in my register create method to assign the database with values upon user registration.

trait Cipherous
{
    protected function issueKeys()
    {
        $pki = sodium_crypto_box_keypair();
        return (object) ['private' => sodium_crypto_box_secretkey($pki), 'public' => sodium_crypto_box_publickey($pki)];
    }
}

This method works fine if I create a new controller, attach a web route and manually visit it. However, inside of my \App\Http\Controllers\Auth\RegisterController.php file, in the create method, when I try to use this:

use Cipherous;

protected function create(array $data)
{
    $pki = $this->issueKeys();

    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'public_key' => base64_encode($pki->public),
        'private_key' => base64_encode($pki->private),
    ]);
}

It executes fine when I register a new account but when I look in the database it is empty. The expected data being stored looks like this after I use dd($this->issueKeys()) in my test controller.

{#631 ▼
  +"private": b"ÐOÝ‗QÉð\x18áÿ^¶ÄuıÁ£└û\x04Úê▀ÀäÆ=ÒÍÁHì"
  +"public": b"Ù(PöÝ╠+J¼¦│®╬═*;$¹‗ƒ\x11Y╝│§ïÉïƒfI\e"
}

How can I store these bits of data in the database when the user registers? I am using base64_encode() because it contains illegal character offsets.

In-case you need this, here is my migration which appended these columns to my user table.

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        # Set to nullable because some users are already registered
        $table->string('public_key')->unique()->nullable();
        $table->string('private_key')->unique()->nullable();
    });
}



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2RWdpNy
via IFTTT

How to sort through two merged collections that have different models

I have two models Cities and States. One City has 1 State and one State can have 0 or more Cities. I need to retrieve all Cities and States separately because I need to display states even if no a state has no related cities (like Alabama in the below example). The issue is I need to sort by State name first, and than by Cities in that state (if there are any)

Cities

id, state_id, name 
1, 1, San Diego 
2, 1, Hollywood
3, 2, Seattle
4, 3, Pheonix

States

id, name
1, California
2, Washington 
3, Arizona
4, Alabama 

Controller

$cities = Cities::with('state')->get(); // Returns the state relationship 
$states = States::get();

$merged = $states->merge($cities);

I would now like to sort by State name first, and than all the cities in that State and return a merged collection similar to this

{
    id: 4,
    name: Alabama,
}, 
{
    id: 3,
    name: Arizona,
}, 
{
    id: 3,
    name: Pheonix,
    state_id: 3
    state: {
        id: 3,
        name: Arizona
    }
},
{
    id: 1,
    name: California
},
{
    id: 2,
    name: Hollywood
    state_id: 1,
    state: {
        id: 1,
        name: California
    }
},
{
    id: 1,
    name: San Diego,
    state_id: 1,
    state: {
        id: 1,
        name: California
    }
}, 
{
    id: 2,
    name: Washington,

},
{
    id: 2,
    name: Seattle,
    state_id: 2,
    state: {
        id: 2,
        name: Washington
    }
}



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2RVzmfn
via IFTTT

Creating a data import page for Laravel Voyager

I am using Voyager for a basic BREAD admin to a small web application I am building for a small non-profit. Yearly they need to import 300-500 new semi-complex entries into the database from Excel, so I want to build an admin script that will store all the data in the right places automatically.

Is there a structured way to add a custom controller/view to Voyager?

(I have not found such documentation yet, maybe I am blind. So I have started manually extending existing bits of Voyager, but as I get deeper I want to make sure this is the best option for future growth.)

Thank you.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2S0tG3R
via IFTTT

How to use Laravel query builder with AND where claus in PHP?

I am trying to ensure that a record exists in the database prior to performing a user related action. When I execute the query directly in my PHPMyAdmin like so (for testing purposes)

SELECT * FROM `chat_participants` WHERE `chat_id` = 2 AND `user_id` = 2

I receive the correct record. However, when I try to use Laravels query builder to achieve the same like so:

dd($this->participants
        ->where('chat_id', '=', 2)
        ->where('user_id', '=', 2)
        ->get()
        ->first());

I get null. Is there a way I can ensure that the record exists in the database using query builder? Do I need to declare AND in the query builder?

Update, I set the participants variable in my constructor:

public function __construct()
{
    $this->middleware('auth');

    $this->header = DB::table('chat_headers');
    $this->participants = DB::table('chat_participants');
    $this->messages = DB::table('chat_messages');
}



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2S9ruGJ
via IFTTT

Laravel/Eloquent get all appointments from a polymorphic "member_id" through a appointment_members table

I have an appointments table and an appointment_members table and my users need to be able to get a collection of appointments by searching with a "member_id", which could be a Person, Incident or CustomerID. The appointment_members table has member_id and appointment_id columns, so the member_type (also a column) is irrelevant. This all set up by a previous dev and what is missing are the relationships on the Eloquent models. I'm just creating a simple GET route that needs to return any/all appointments by that member_id. Each row has one appointment, so if I were to pass in a member_id that returned 10 results, some could have appts and others not, but at the end of the day I just need a collection of appts that are related to that member_id. Here's a screenshot of the appointment_members table: enter image description here

If I create a simple hasOne relationship to appointments on appointment_members:

public function appointments()
{
    return $this->HasOne(Appointment::class, 'id', 'appointment_id');
}

I can get a collection of appointment_members with it's respective appointment, but not sure how I boil it down to just getting the appointments.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2DGElYR
via IFTTT