jeudi 2 juillet 2020

Load relationships before foreach loop

I have a User model that does have many Networks. Each Network does have many Lists.
I have this code in the User model that is really slow:

foreach ($this->networks as $network) {
    if ($myCondition) {
        foreach ($network->lists()->get()->lists('id')->toArray() as $newId) {
            $ids[] = $newId;
        }
    }
}

I wonder if there is a way to load all lists of all networks before the 2 foreach loops.



via Chebli Mohamed

Upload several images Laravel

Good I would like to know if someone can help me, it is something easy but I can't figure out how to do it, I have a property and I need to create several images of it when creating the property, is there any post or tutorial that I can follow to implement the upload of several images? since I only have to upload one



via Chebli Mohamed

What do you think is the problem if shows the id not the targeted column I want to be shown? [closed]

Output [1]: https://i.stack.imgur.com/QgfDW.jpg

PS: I want availability, category, and unit of measurement to show in the table, not the id. Thank you in advance for your inputs.



via Chebli Mohamed

Laravel allowing null dates on Model::create() but not Model->update()

I have setup my Laravel migration to allow nullable dates for my StartTime and EndTime entries as such:

$table->dateTime( 'StartTime' )->nullable();
$table->dateTime( 'EndTime' )->nullable();

When I create a new entry through eloquent, it allows me to insert null values into my database successfully:

+----+-------+--------+-----------+---------+---------------------+
| Id | Name  | Active | StartTime | EndTime | created_at          |
+----+-------+--------+-----------+---------+---------------------+
|  1 | Test2 |      0 | NULL      | NULL    | 2020-07-02 22:01:22 |
+----+-------+--------+-----------+---------+---------------------+
1 row in set (0.00 sec)

However, when I later try and update my record using eloquent and still passing a null value for StartTime, it throws an error:

(22007) SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: 'null' for column 'StartTime' at row 1

Am I missing something here? I can't seem to find a solution to this anywhere.



via Chebli Mohamed

uploading images to one folder using two laravel websites

i have 2 different laravel projects using same database as an example here firstwebsite.com secondwebsite.com

in the secondwebsite.com users can register through a form by uploading images and videos to a storage folder and in the firstwebsite.com that registered users is listed along with the profile images which i am fetching from secondwebsite.com. Both websites have separate admin dashboards and admin can edit profiles of the users. now i want admin of the firstwebsite.com to have an option to edit the registered users profile which i already did but only have an issue of images and videos to update because if i update the images then it is stored in firstwebsite.com and i want it to be stored in the storage directory of secondwebsite.com

below is some of my code.to upload images from secondwebsite.com

               foreach ($request->file('picture') as $picture) {
                 $pictures[] = $fileName = time().$picture->getClientOriginalName();
                 Storage::put('public/'.$fileName,file_get_contents($picture));
               }

configuration of filesystem in config/filesystem.php

 'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],
    ]


via Chebli Mohamed

Laravel - conditional filtering with relation table

I have to conditionally filter results in the below query, based on $search is present or not. Below is my query,

    Sellers::with(
        'shop:id,width,height,business_id'
    )->with(
        'unit.location:id,name as l_name'
    )->select(
        'sellers.id', 'sellers.name' , 'address'
    )->when($search, function ($query, $search) {
         $query->where('l_name', 'like', '%' . $search . '%');
    })
    ->findOrFail($request->seller_id);

The relations are,

Sellers has many units. Units has one location.

I need to filter the above result for location names based on the availability of search param. I have tried adding aliases, but they are throwing errors.

How can this be attained?



via Chebli Mohamed

How to create this type of array within class in laravel

Create one class Define one property called : Userlist, it will be a 2D array Key of the array will be : user_id, user_name, City, state, country, Mobile Define method : Get_user_by_name : return the match record of array Create one method for each key.

Do it using Laravel



via Chebli Mohamed