mardi 27 novembre 2018

Truncate a table base on database connection in Laravel 5

I have a database connection nuc I have a table name Alerts

I'm trying to truncate that table by doing this

DB::connection('nuc')->statement('TRUNCATE Alerts');

I kept getting

enter image description here

QueryException in Connection.php line 629: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "alerts" does not exist (SQL: TRUNCATE Alerts)

How do I force truncate a table base on database connection?



via Chebli Mohamed

lundi 26 novembre 2018

How do I prevent other users from high jacking the HTTP request payload as other users?

I have

a laravel app with the route

Route::put('/api/{deviceMac}/access/update','DeviceController@update');

rule

If user A have deviceMac 000000000000, should only be making a PUT to

http://www.app.com/api/000000000000/access/update
{deviceMac:000000000000, access: true}

If user B have deviceMac 111111111111, should only be making a PUT to

http://www.app.com/api/111111111111/access/update
{deviceMac:111111111111, access: true}

User A should not be able hijacking the route update of other users

hijacking

User A should have access to 000000000000 only,

Right now, User A can tweak the HTTP request and make a PUT as User B

http://www.app.com/api/111111111111/access/update
{deviceMac:111111111111, access: false}


Questions

How do I prevent other users from high jacking the request payload as other users ?

Should I adjust my middleware to take care of this issue ?


I'm open to any suggestions at this moment.

Any hints/suggestions / helps on this be will be much appreciated!



via Chebli Mohamed

samedi 24 novembre 2018

Laravel: is it possible to do orWhere(Model::find())?

I am using Lumen Laravel 5.1. I have a many to many relation between task and user, and pivot table called task_user, I recently asked this question, inspired by the answer and by reading the documentation, I think I can do this

$users = User::tasks()
        ->where('domain_id', $domainId)
        ->where('created_by', $userId)
        ->orWhere(User::tasks()->find($userId))
        ->Where(function ($query) {
            $query->where("is_done", 0)
                ->orderBy('due_date', 'DESC');
        })
        ->Where(function ($query) {
            $query->Where("is_done", 1)
                ->Where("closed_dated", Carbon::today())
                ->orderBy('closed_date', 'ASC');
        })
        ->get();

Before I accept his answer I have two questions:

1- A user can have his id related to a task, in 2 places

  • task_user table if is an assignee
  • created_by in tasks table, if he created it

So I want to know if he's an assignee or if he created it, both cases I want to display his tasks. Found nothing on google, so can I do this?

where(....) -> orwhere(model::find())

  1. My second question is: Should I use ->pivot->id to get to task, instead of find()? The docs about ->pivot are confusing and the tutorials I read don't use it. I don't know if it helps me.


via Chebli Mohamed

mardi 20 novembre 2018

vendredi 16 novembre 2018

auth()->user()->id is not working when I use it in controller using routes to api.php

public function store(Request $request)
    {
        $booking = ($request->isMethod('put')) ? Booking::findOrFail($request->booking_id) : new Booking;
        $booking->checkIn = $request->checkIn;
        $booking->checkOut = $request->checkOut;
        $booking->room_id = $request->room_id;
        $booking->user_id = auth()->user()->id;//not working

        if($booking->save()){
            return new BookingResource($booking);
        }
    }

Route::put('/booking','BookingsController@store');//api.php

Here auth()->user()->id is not working but its working find if i use it the same code but route code in routes/web.php



via Chebli Mohamed

jeudi 15 novembre 2018

Can't modify 'updated_at' in my database after adding a column with a migration in Laravel 5.1

I am trying to add a new integer "id" column in a table of my database to map every row with an id. In order to do so, I am using a migration in Laravel 5.1. The run() function of the migration is exactly the following:

public function up()
{
    Schema::table('license_keys', function($table) {
        $table->integer('id_nuevo');
    });
}

The table I am trying to modify is set with default 'updated_at' and 'created_at' timestamps.

I execute the migration and the column is added correctly. I made sure to add the new column in the $fillable variable in my model. The next step in the process is to set the ids correctly, because that column is created with all 0s. In order to do so, I am using a Seeder with the following code:

public function run()
{
    $i=1;
    $licenses = LicenseKey::getEveryLicenseKey();
    foreach ($licenses as $license){
        $license->id_nuevo = $i;
        //$license->timestamps = false;
        $license->save();
        $i++;
    }

}

But the problem starts here. When I try to update any field of any row with the save() function it gives me the following error:

[Illuminate\Database\QueryException]                                                                                                                                                                     
 SQLSTATE[21S01]: Insert value list does not match column list: 1136       Column count doesn't match value count at row 1 (SQL: update `license_keys`   set `id_nuevo` = 1, `updated_at` = 2018-11-15 13:24:11   
 where `hash_key` = 0...0b)                                                                                                                                                     



  [PDOException]                                                                                                       
  SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1

But if I set the timestamps to false (commented line in the code), the operation succeeds. Even if I try to manually change the value of the 'updated_at' column in phpMyadmin, it gives me the same error. Can anybody help me with this problem?

Thanks



via Chebli Mohamed

dimanche 11 novembre 2018

The installation directory "/usr/local/bin" is not writable - How to fix this? I am trying to install the composer globally

ERROR message while trying to setup Composer

192:~ biancalouisedairo$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 192:~ biancalouisedairo$ php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" Installer verified 192:~ biancalouisedairo$ php composer-setup.php --install-dir=/usr/local/bin All settings correct for using Composer The installation directory "/usr/local/bin" is not writable 192:~ biancalouisedairo$



via Chebli Mohamed