vendredi 16 juillet 2021

Laravel join 3 tables using Query Builder

I have a function that expects a query builder as input parameter. On the query I want all the records returned from this query:

$query = $model->newQuery()
            ->leftJoin('materials', 'products.material_id', '=', 'materials.id')
            ->leftJoin('vehicles', 'products.vehicle_id', '=', 'vehicles.id')
            ->leftJoin('parts', 'products.part_id', '=', 'parts.id')
            ->select(
                'products.id as id',
                'products.name as product_name',
                'parts.part_name as part_name',
                'materials.public_name as material',
                'vehicles.name as vehicle'
            );

Using leftJoin the function runs but returns nothing and if I replace leftJoin for join (what I think it's the right solution for this case) vsc says it's not a Query Builder and executing returns 5xx error.

$model binds to my Product model.



via Chebli Mohamed

How to remove double quotes in laravel stdClass [duplicate]

I am getting: "{"lesson_type":"quiz","questions":[],"transcript":"storage\/1626421168176448258ktzpvaqnefg.txt","extra_contributors":null}"

But I want: {"lesson_type":"quiz","questions":[],"transcript":"storage\/1626421168176448258ktzpvaqnefg.txt","extra_contributors":null}

I just want to remove the double quotes from start and end.



via Chebli Mohamed

jeudi 15 juillet 2021

Which one Realtionship Suitable In this Case. - Laravel

I have 3 Table Like This,

One of User Table

users
  id,
  name

second one

documents
  id,
  type

third One

  clinician_credentials
     id,
     user_id,
     document_id => in this user_id from users table and document_id from documents table

I am tried hasManyThrough but did not give perfect data, please suggest to me which one relationship is suitable in this case.

public function clinician_credential() {
   return $this->hasManyThrough(User::class, ClinicianCredentials::class, 'id', 'user_id', 'id', 'clinician_id');
}

Then I am using for each loop to get clinician_credential but I want to use eager loading, not for each loop.

Right Now My query Like this,

    $documents = Documents::orderBy('type')->get()->toArray();
    if (count($documents)) {
        foreach ($documents as &$document) {
            $document['dp_credential'] = null;
            $clinician_credential = ClinicianCredentials::where('document_type', $document['id'])
                    ->where('clinician_id', $user_id)
                    ->select(DB::raw('*, datediff(expiry_date, now()) as days, IF(datediff(expiry_date, now()) < 30, 1, 0) as is_expiry'))
                    ->first();
            if ($clinician_credential) {
                $document['dp_credential'] = $clinician_credential;
            }
 


via Chebli Mohamed

execute notification javascript with session

how can execute the notification with session?, so i don't use onclick

  @if (session('success'))
 <script>
                $('.snackbar-bg-primary').click(function() {
    Snackbar.show({
    text: 'Primary',
    actionTextColor: '#fff',
    backgroundColor: '#1b55e2'
});
});
     </script>
  @endif

I pass the session with:

 return redirect('solicitudes')->with('success', 'Solicitud Envíada exitosamente!');

how can I execute the notification without using the click?



via Chebli Mohamed

Laravel 5 changing domain cookie issue

I'm moving a Laravel 5 app from a domain to another. All work right but if I inspect with consolle I see that I've a lot of warning about cookies ("The XyZ cookie will be soon used like cross-site because schema doesn't match..") Where can I define this cookie's schema into Laravel framework? I think it reads somewhere old domain one...

I ask this because, sometimes when I perform a simply INSERT query (submitting data via form) I notice that the page is very slow to refresh and sometimes happends that I receive a 500server error (timeout?)

Do you think the cookie truble could be connects to my crash here?



via Chebli Mohamed

Viu js and Laravel Route and project structure

I am working on a project , i need to build front end with viu js and laravel 8, for users(abc.com as front users) like user login , dashboard etc and there will be a admin (abc.com/admin) with admin login , dashboard etc will be in laravel with blade.

 1. I configure and running vue js with laravel 8 for front users.
 2. how to run or route for this --> abc.com/admin or abc.com/client , (don't want api here regular laravel approch parsing data to blade)
 3. like i install vue js and laravel and for front user resources/js have all, the components etc running. i am not sure about project structure with laravel and vue. is this correct way?


    Route::get('/{any}', function ($any) {
        if ($any == 'admin') {
            return View::make('admin.index');  // this is blade
        } if ($any == 'client') {
            return View::make('client.index'); // this is blade
        }  else {
            return View::make('front.index');  // this is viu js 
        }
    });


via Chebli Mohamed

create associative array with key and value generated in for

i´m traying to create associative array, from query in laravel.

public function assignAllCall(Request $request)
    {
        $callList = [];
        $newCall = [];

        for($i=0; $i<count($request->get('calls')); $i++){
            array_push($callList, Listado::where('id', $request->get('calls')[$i])->get());
        }

        for($i=0; $i<count($callList); $i++){
            for($j=$i; $j<count($callList[$i]); $j++){
                $newCall = [
                    'nomape'            => $callList[$i][$j]->nomape,
                    'direccion'         => $callList[$i][$j]->direccion,
                    'provincia'         => $callList[$i][$j]->provincia,
                    'ciudad'            => $callList[$i][$j]->ciudad,
                    'cp'                => $callList[$i][$j]->cp,
                    'telefono'          => $callList[$i][$j]->telefono,
                    'movil'             => $callList[$i][$j]->movil,
                    'id_teleoperadora'  => $request->get('teleoperadora'),
                    'id_estado'         => 1,
                    'fecha_asignacion'  => Carbon::now()->format('Y-m-d H:m:s'),
                    'created_at'        => Carbon::now()->format('Y-m-d H:m:s'),
                ];
                
            }

            print_r($newCall);
            // create call
            //$result = Llamada::create($newCall);
        }  

        //return $result;
    }

i have this function that receive param from ajax and this values it´s ids array.

I need get all data from this ids for get all data and create new calls with this data, for this i have one for to do query and assign to array. after i´m traying to create associative array but when i do print_r always i return same result:

Array
(
    [nomape] => x
    [direccion] => x
    [provincia] => x
    [ciudad] => x
    [cp] => x
    [telefono] => x
    [movil] => x
    [id_teleoperadora] => x
    [id_estado] => x
    [fecha_asignacion] => x
    [created_at] => x
)

always same value. I don´t know if i do well my function or have one better solution for to do this.

Thanks for read and thanks for help.



via Chebli Mohamed