Mehdi_Souihed

Experience

2,675

2 Best Reply Awards

  • Member Since 3 Years Ago
  • 58 Lessons Completed
  • 9 Favorites

14th October, 2015

Mehdi_Souihed left a reply on How Do You Unit Test File Uploads? • 3 years ago

Here is an other (simpler) way to do it !

Mehdi_Souihed left a reply on File Upload Unkown Error With Phpunit • 3 years ago

Hi Guys,

I came across a similar problem while trying to test uploading a file on a form. Here is what I have done to make it work :

    
    // ... Your test file ...

    // array
        $file = 'public/uploads/file.pdf';
        $this->assertFileExists($file);
        $pdf = new UploadedFile ($file, null, 'application/pdf', null, null, true);
        $form = $this->fakeFormData();
        $form['file'] = $file;
        $this->visit('signup')->submitForm('Submit', $form);

However if you typically have the following rule in your validator:

   //...
     'file' => 'mimes:pdf',
  //...

You will get an error in these terms : ** file must be of one these types: pdf**

This is because the above rule checks against the mimeType sent by the browser (which is not a terribly good practice) and the PHPUnit crawler always sends a mime type of 'application/octet-stream' and then laravel winges.

So to make the server check itself the mimeType and not rely on the browser, we have to define a custom validator rule:


          Validator::extend('is_pdf', function($attribute, $value, $parameters, $validator) {

            $mime = \Request::file($attribute)->getMimeType();
                        return $mime == 'application/pdf';
        });

You should also define a custom error message, it is explained in the Laravel Doc (link above).

2nd April, 2015

Mehdi_Souihed left a reply on Eloquent Eager Loading Modify 'Where ... IN ' Clause • 3 years ago

@constb For records this is what my query looks like now. It kind of simulates eager loading with the ability to choose which records go in the WHERE .. IN(...) clause automatically generated by Eloquent

$users = User::whereHas('settings', function ($q) { 
             $q->where('settingsid', 16)->where('value', 'LIKE', '%o%'); 
         
         })->with(['settings' => function ($q) {
              $q->whereIn('settingsid', $this->settingsColumns);
        // Get only some columns in the settings table
              $q->addSelect(['value', 'id', 'userid', 'settingsid']); 
  
         }])->get();

Thanks

Mehdi_Souihed left a reply on Eloquent Eager Loading Modify 'Where ... IN ' Clause • 3 years ago

Thanks very much @constb

I have tinkered with it a bit and it looks like it is going to solve it :-)

I'll update this thread very soon and let you know

Cheers

Mehdi_Souihed left a reply on Eloquent Eager Loading Modify 'Where ... IN ' Clause • 3 years ago

@constb Thanks for your reply

I have tried that approach, the result of that query would be :

User, Location, Dept.
5   , London

Billing is left out and not returned.

Mehdi_Souihed left a reply on Eloquent Eager Loading Modify 'Where ... IN ' Clause • 3 years ago

Thanks for your replies.

Right, I have a user_settings table which is an Entity Attribute Value Model, I know ... that's not great. It basically looks like this :

settingsid, userid, value 15 , 5, Billing 15 , 6, Operations 16 , 5, London 16 , 6, Edinburgh ...

What I already have is all settings for each user lined up in rows : User, Location, Dept. 5 , London, Billing 6 , Edinburgh, Operations

I am looking to be able to filter per Location or Dept or any other setting.

I hope that is clear enough, thank you

Mehdi_Souihed started a new conversation Eloquent Eager Loading Modify 'Where ... IN ' Clause • 3 years ago

Hi,

I am using eager loading with one model and the query looks like this

select `value`, `id`, `userid`, `settingsid` from `user_settings` where `user_settings`.`userid` in ('1', '4', '5', '8', '9', '11', '13', '14', '15', '16', '28')

I have a User model and a User settings models containing a foreign key to User.

What I would like to do is have control over the User ids used by the eager loading.

Is that possible

23rd March, 2015

Mehdi_Souihed started a new conversation Codeception Dump Page • 3 years ago

Hi all,

Does anyone know how to dump the html page Codeception uses for the tests ?

I would like to be able to see what Codeception sees because I have a failing test that doesn't make sense to me.

Thank you

3rd March, 2015

Mehdi_Souihed left a reply on Class Not Found Exception • 3 years ago

@User62 Have included the class Class 'CreateTablePlayer' in your migration ?

23rd February, 2015

Mehdi_Souihed left a reply on [L5] Role And Permission • 3 years ago

@nazar1987 You need to create the 'models' folder yourself and then put the different files in it (Role.php, Permission.php)

Also don't forget to update the entrust config file config/entrust.php with the correct namespaces for Role and Permission models because they are defaulted to the [base namespace]:(https://github.com/Zizaco/entrust/blob/7037d6ab52f9341c26e61fc61ca90813cdb9b32c/src/config/config.php#L14).

18th February, 2015

Mehdi_Souihed left a reply on Eloquent. Infinite Children Into Usable Array And Render It To Nested Ul • 3 years ago

@AlnourAltegani @pmall I see, this is at the database level ;-) I came across that problem for a hierarchy of users. I have used Baum to manage the hierarchies for me. For your case I guess you'll need to keep querying the database recursively.

Mehdi_Souihed left a reply on Eloquent. Infinite Children Into Usable Array And Render It To Nested Ul • 3 years ago

@pmall From his question @AlnourAltegani is only looking for the values of his elements :

I have just tested the following code (I have normalised a bit the array which contained)

$categories = array(
        'name' => 'cat1',
        'children' => array (
                
                'name' => 'cat1',
                'children' => array (
                    
                     'name'=>'subcat1',
                     'children'=> array ()
                )
            )
);


function test_print($item, $key)
{
    echo "Category name : $item\n";
}

array_walk_recursive($categories, 'test_print');

Which outputed :

Category name : cat1
Category name : cat1
Category name : subcat1

Mehdi_Souihed left a reply on Eloquent. Infinite Children Into Usable Array And Render It To Nested Ul • 3 years ago

The most straightforward however maybe not fastest way is to use a array_walk_recursive function to traverse your array (which is called a tree datastructure) Here is the snippet from the PHP doc adapted to your case :


function test_print($item, $key)
{
    echo "Category name : $item\n";
}

array_walk_recursive($categories, 'test_print');

Another method would be to use the Recursive Iterator however I think in your case it is overkill.

Then to print it to an unordered list you have a Laravel HTML helper, if you use Laravel 5 you will need to install illuminate

Pass your array to your view and print it with :

  {{ HTML::ul($categories) }}

17th February, 2015

Mehdi_Souihed left a reply on Redirect In AfterFilter • 3 years ago

@dante_dd you need to return the result as follows :

if ($something) {
           return $this->afterFilter(function($route, $request, $response)
            {
                return Redirect::to('http://google.com', 301);
            });
        }

Note the return on line 2.

Mehdi_Souihed left a reply on Laravel 4 TokenMismatchException Error When Login Or Signup • 3 years ago

Are you sure that

Input::get('_token')

is not empty ?

If it is not I would suggest clearing all your caches.

Mehdi_Souihed left a reply on Access Errors In Form Macro • 3 years ago

You can probably access your $errors variable using :

Session::get('errors');

Mehdi_Souihed left a reply on Eloquent DB Questions.... • 3 years ago

If what you mean is how to resolve your foreign key and get joined data from your three tables, you could do the following :

VendorDetails::with('vendor')->with('client')->where('client_id', =, Session::get('client_id')) ;
Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.