dimanche 31 juillet 2022

Laravel SwitMessage add Reply-path header

We have a Laravel application which sends newsletters to customers. As per the client's request, we would like to add a header in the email indicating that it's a newsletter or mail to a list to avoid out of office email bounces. Upon research, I found that one way to do so is to add Precedence: bulk header. But answer also says that RFC 2076 discourages adding Precedence header. Some post recommend adding Return-Path: <> (a NULL Return-Path). However, I'm not able to add this header. I have tried following codes to add such a header but not working.

$headers->addTextHeader('Return-Path', ''); - Uses the mail id defined in config file 
$headers->addTextHeader('Return-Path', '<>') - This generates a malformed header as in the attached image
$headers->addTextHeader('Return-Path', ' '); - Adds a header like Return-Path   
< > but not sure if this is correct or it will create other issues


via Chebli Mohamed

Guzzle Laravel simple connection and errors checker

Im using Guzzle on a laravel Project, Im want to have a working connection validation and error checker if has errors, also want first check the connection if its 200 if isnt then show the error message, if is 200 then proceed to second check that checking if has Error from the remote server like "duplicate ID" if has no error then make the POST or GET request is it possible that? because now when run this get error on $errormsg

My function:

  public function showinvoice()
    {
        $client = new \GuzzleHttp\Client([
            'http_errors' => false,
            'headers' => [
                'aade-user-id' => 'blabla',
                'Ocp-Apim-Subscription-Key' => 'blablabla',
               
            ]
        ]);

        $request = $client->GET('https://mydatapi.aade.gr/myDATA/RequestMyExpenses/?dateFrom=11/11/2021&dateTo=11/12/2022');

        $response = $request->getBody()->getContents();

        $reasonPhrase = $request->getReasonPhrase();

        $statusCode = $request->getStatusCode();
        $errormsg = strval(simplexml_load_string($response)->response->errors->error->message);
        if ($statusCode <= 200) { 

            if (isset($errormsg)) {

                $this->dispatchBrowserEvent('alert', ['type' => 'error', 'message' => $errormsg]);
            }
            else{
               return $response;
            }
   
        } 
        else {
            $this->dispatchBrowserEvent('alert', ['type' => 'error', 'message' => $statusCode .' '. $reasonPhrase]);
        }
    }
    ```


via Chebli Mohamed

jeudi 28 juillet 2022

Laravel Grouping a list of todos into one folder

Please I am trying to group a list of todos by selecting them and grouping them into one link. so when I click on the link I will be able to see all the todos I selected and added to the group. how can I do that? I have been able to add these codes but I do not know how to move forward because I am quite new to laravel. here are my codes

 <tbody>
                @foreach($todos as $todo)
                    <tr>
                        <td><input type="checkbox" name="selected_todos[]" value=""></td>
                        @if($todo->completed)
                            <span><td style="text-decoration:line-through"></td></span>
                        @else
                            <td></td>  
                        @endif     
                        <td></td>
                        <td></td>
                        <td><a class="btn btn-primary" href="" role="button" ><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pen" viewBox="0 0 16 16">
                            <path d="m13.498.795.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001zm-.644.766a.5.5 0 0 0-.707 0L1.95 11.756l-.764 3.057 3.057-.764L14.44 3.854a.5.5 0 0 0 0-.708l-1.585-1.585z"/>
                            </svg></a></td>
                        <td><a class="btn btn-danger" href="" role="button" ><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16">
                            <path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/>
                            <path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/>
                            </svg></a></td>
                        @if($todo->completed)
                            <td><a class="btn btn-info" href="" role="button" ><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-bag-x" viewBox="0 0 16 16">
                                <path fill-rule="evenodd" d="M6.146 8.146a.5.5 0 0 1 .708 0L8 9.293l1.146-1.147a.5.5 0 1 1 .708.708L8.707 10l1.147 1.146a.5.5 0 0 1-.708.708L8 10.707l-1.146 1.147a.5.5 0 0 1-.708-.708L7.293 10 6.146 8.854a.5.5 0 0 1 0-.708z"/>
                                <path d="M8 1a2.5 2.5 0 0 1 2.5 2.5V4h-5v-.5A2.5 2.5 0 0 1 8 1zm3.5 3v-.5a3.5 3.5 0 1 0-7 0V4H1v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V4h-3.5zM2 5h12v9a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V5z"/>
                                </svg></a></td>
                        @else
                            <td><a class="btn btn-success" href="" role="button" ><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-bag-check" viewBox="0 0 16 16">
                                <path fill-rule="evenodd" d="M10.854 8.146a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708 0l-1.5-1.5a.5.5 0 0 1 .708-.708L7.5 10.793l2.646-2.647a.5.5 0 0 1 .708 0z"/>
                                <path d="M8 1a2.5 2.5 0 0 1 2.5 2.5V4h-5v-.5A2.5 2.5 0 0 1 8 1zm3.5 3v-.5a3.5 3.5 0 1 0-7 0V4H1v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V4h-3.5zM2 5h12v9a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V5z"/>
                                </svg></a></td>
                        @endif 
                    </tr>
                @endforeach
            </tbody> 
  public function todos()
    {
        return $this->hasMany(TodoList::class);

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

    }

and I have not included a controller for the grouping because I do not know how to go about it.

Thank you



via Chebli Mohamed

mercredi 27 juillet 2022

Could not load mock: class already exists when run with RefreshDatabase Trait

public function testSomething()
{
    $this->login();

    m::mock('overload:MyClass', function ($mock) {
        $mock->shouldReceive('METHOD')
            ->andReturn(
                (object)[
                    'status'  => true,
                    'message' => 'Message',
                ]
            );
    });

    $this->post(route('ROUTE'))
        ->assertRedirect()
        ->assertSessionHas(
            'success',
            'Message'
        );
}

When I try to run this with:

use DatabaseTrasactions: It works

use RefreshDatabase: It does NOT work

Error: Mockery\Exception\RuntimeException : Could not load mock \MyClass, class already exists

Laravel: 5.5 PHPUnit: 6.5.14

Tried

  • @runTestsInSeparateProcesses (on entire class)
  • @runInSeparateProcess (on single test)


via Chebli Mohamed

Laravel 5.5.50 not resetting password

Good day programmers, I'm using laravel 5.5.50 and I don't intend to reinvent the wheel so I'm using the default password reset. Normally everything goes fine;

  1. I clicked on forgot password
  2. I put my email and send password reset link
  3. I go to my email and click the link
  4. The link takes me to reset page then I put my new password
  5. The password change is successful then it redirects me to my user dashboard

But when I logout and login again with the new password, it says incorrect. In fact the password was not changed at all. How do I rectify this please. I am new to laravel



via Chebli Mohamed

dimanche 24 juillet 2022

Auth::user() always return null

recently I have installed spatie/permissions in a fresh Laravel 9 installation. before I install this package whenever I called Auth::user() it returns back with the result with the currently authenticated user. but now after I installed spatie/permissions it always returns null in any controller, but it's working normally in views.

is there anyone have the same issue here. and how he managed to fix it.



via Chebli Mohamed

samedi 23 juillet 2022

Data Pass from controller to view

I want to display sum of column debit but when i pass data after sum value of column store in varibale, I pass the data from controller to view but laravel said its undefined can anyone help me with this ?

View.blade.php

                      <tr>
                        <th>Total Debit:</th>
                        <td>
                          @foreach ($totaldebit as $tdebit)
                          
                          @endforeach
                        </td>
                      </tr>

UserController.php

$tdebit = DB::table('debitemps')->sum('debit');
        return view('pages/payments/debit/debit',['workerlists'=>$data,'debitid'=>$id,'Empdebitdata'=>$dataEmp,'totaldebit'=>$tdebit]);


via Chebli Mohamed