lundi 21 septembre 2020

Laravel 5.4 - Get logged user info with other table (contests) information

Showing right now on PageController

 public function getBlazeTalent() {
    $contestars = DB::table('contests')->where('user_id', Auth::id())->get();
    return view('blazetalent')->withContestars($contestars);
 }

But I want to show current user information with other contestars (users) information.

Contest Model

public function user() {
  return $this->belongsTo('BlazeChannel\User');
}


via Chebli Mohamed

dimanche 20 septembre 2020

Laravel strange behavior in 1 to Many relationship

Very strange situation in Laravel 1 to Many relationships, My User model

public function reminder(){

        return $this->hasMany(Reminder::class);
    }

Reminder Model

public function user(){

        return $this->belongsTo(User::class);
    }

When I try to get

dd(auth()->user()->reminder) -> null
$user_id = auth()->user()->id;
$user = User::findOrFail($user_id);
dd($user->reminder) -> success I got results.

Question is why I can't get relationship result using auth()->user()->reminder???



via Chebli Mohamed

Laravel: Getting Orders between a certain time

I am bit a new to Laravel. I am trying to get a orders between a certain point in time. I have a variable $streamStartTime that is set early on to Carbon::now()->timestamp.

$streamStartTime = Carbon::now()->timestamp;

Later on, I call this:

 $streamPaidOrders = Order::->whereBetween('orders.created_at', [$streamStartTime, Carbon::now()]);

Would those 2 parameters give me orders between them or are they in different formats?



via Chebli Mohamed

Using Validator methods on custom validation rules in AppServiceProvider

This is what my AppServiceProvider's boot method looks like. My custom rule must do this: it must check that the input specified in 0'th parameter does exist and is not null, then must check current attribute's value is and array and is not null;

The problem is, when I try to access $validator's getValue() or validateRequired() methods app gives me an exception like:

Method [getValue] does not exist.

How can I access those methods?

      Validator::extendImplicit('imageArrayWith', function($attribute, $value, $parameters, $validator){
        $imageArray = json_decode($value);
        $requiredRow = $parameters[0];
        $requiredRowVal = $validator->getValue($requiredRow);
        if($validator->validateRequired($requiredWithRow)){
          if(is_array($imageArray)){
            if(count($imageArray) == 0){
              return false;
            }
          }
          return true;
        }else{
          return false;
        }
      });


via Chebli Mohamed

Laravel return 404 for js file that it exist

I have newly added app.js file which I include in the footer as follow:

<script src="" defer></script>

it return 404 status code, but the file exists and the generated URI is correct, what is the problem?

it work fine locally, but on production server it return 404



via Chebli Mohamed

samedi 19 septembre 2020

saving dropdown list value in laravel

I need assistance please. The scenario: I have a table called users and transactions in laravel. Created a blade for transaction table which will pull data as user id drop down list from users table. All user id of users table are show in transaction with dropdown lists. Transaction table has amount column with debit/credit options. User table has total amount column.

My controller -

DB::table('users')->where('userid', $request->userid)->increment('amount', $request->amount); and DB::table('users')->where('userid', $request->userid)->decrement('amount', $request->amount);

What I want to achieve

Currently, the dropdown list is working and save data in transaction table but amount can’t be debited or credited in total amount in user tables.

$request->userid is not getting value from dropdown list.

Please how do I achieve this? Thank you.



via Chebli Mohamed

Laravel echo how to set it to use custom driver?

I have the following in broadcasting.php:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Broadcaster
    |--------------------------------------------------------------------------
    |
    | This option controls the default broadcaster that will be used by the
    | framework when an event needs to be broadcast. You may set this to
    | any of the connections defined in the "connections" array below.
    |
    | Supported: "pusher", "redis", "log", "null"
    |
    */

    'default' => env('BROADCAST_DRIVER', 'null'),

    /*
    |--------------------------------------------------------------------------
    | Broadcast Connections
    |--------------------------------------------------------------------------
    |
    | Here you may define all of the broadcast connections that will be used
    | to broadcast events to other systems or over websockets. Samples of
    | each available type of connection are provided inside this array.
    |
    */

    'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'useTLS' => true,
            ],
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],

        'log' => [
            'driver' => 'log',
        ],

        'null' => [
            'driver' => 'null',
        ],
        'internal_chat_pusher' => [
            'driver' => 'pusher',
            'key' => env('INTERNAL_CHAT_PUSHER_APP_KEY'),
            'secret' => env('INTERNAL_CHAT_PUSHER_APP_SECRET'),
            'app_id' => env('INTERNAL_CHAT_PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('INTERNAL_CHAT_PUSHER_APP_CLUSTER', 'ap2'),
                'useTLS' => true,
            ]
            ],

    ],


];

then in my controller I try to use 'internal_chat_pusher' driver as follow:

  public function send(Request $request)
    {
        $message = Message::create([
            'from' => auth()->id(),
            'to' => $request->contact_id,
            'text' => $request->text
        ]);
        // $internal = Broadcast::driver();
        \Config::set('broadcasting.default', 'internal_chat_pusher');
        broadcast(new NewMessage($message));
        \Config::set('broadcasting.default', 'pusher');
        return response()->json($message);
    }

in my JavaScript I try to subscribe user to channel using the following:

window.Echo = new Echo({
    broadcaster: 'pusher',
    driver: 'internal_chat_pusher',
    key: 'xxx',
    cluster: 'ap2',
    encrypted: true
});

but I get in Pusher debug console:

Invalid key in subscription auth data: 'xxxx'

here is my channels.php:

<?php

/*
|--------------------------------------------------------------------------
| Broadcast Channels
|--------------------------------------------------------------------------
|
| Here you may register all of the event broadcasting channels that your
| application supports. The given channel authorization callbacks are
| used to check if an authenticated user can listen to the channel.
|
*/

Broadcast::channel('messages.{id}', function ($user, $id) {
    return $user->id === (int) $id;
});

I guess the problem is related to something like the authentication and authorization are done aginst the 'pusher' driver not 'internal_chat_pusher' because when I set the key in echo to be the one in pusher I get subscribed successfully, so, how to solve this problem please?



via Chebli Mohamed