mercredi 10 novembre 2021

Selecting from multiple tables in laravel

please help.I have a test booking table that looks like this

Schema::create('test_bookings', function (Blueprint $table) {
        $table->unsignedInteger('RequestID');
        $table->string('bookingDate');
        $table->string('timeSlot');
        $table->unsignedInteger('nurse_id');
        $table->timestamps();
    });

and a tests table that looks like this

Schema::create('tests', function (Blueprint $table) {
        $table->unsignedInteger('RequestID');
        $table->unsignedInteger('patientID');
        $table->string('barcode');
        $table->string('temperature');
        $table->string('pressure');
        $table->string('oxygen');
        $table->unsignedInteger('nurseID');
        $table->timestamps();
    });

I want to show the RequestID,bookingDate,timeSlot, name and surname of the nurse only if the test_bookings RequestID is in tests table. This is my nurse table

Schema::create('nurses', function (Blueprint $table) {
        $table->unsignedInteger('nurseID');
        $table->string('name');
        $table->string('surname');
        $table->string('idNumber');
        $table->string('phone');
        $table->string('email');
        $table->unsignedInteger('suburb_id');
        $table->timestamps();


        $table->index('suburb_id');
    });

This is the code that i tried

$tests =  DB::table('tests')
                ->select('RequestID','bookingDate','timeSlot','name','surname')
                ->join('nurses','nurses.nurseID','test_bookings.nurse_id')
                ->join('test_bookings','test_bookings.RequestID','=','tests.RequestID')
                ->get();


via Chebli Mohamed

Conditionally validate request fields in Laravel 5

My variable carType gets a string value.

If additionalFields variable is carType should be required when it is a truck or caravan. If carType is a truck, then additionalFields should be a array.

'carType' => 'string',
'additionalFields' => 'required_if:carType,truck,caravan',

How do i add an array validation based on condition?



via Chebli Mohamed

mardi 9 novembre 2021

issue in laravel php artisan server [closed]

I have installed a live laravel project on my localhost. when I run there are two warning errors that occur.

Anyone have idea how to fix it

Warning: The use statement with the non-compound name 'DateTime' has no effect in app\helper.php on line 22

Warning: The use statement with the non-compound name 'DateTimeZone' has no effect i
n \app\helper.php on line 23


via Chebli Mohamed

Wildcard-like syntax in an eloquent Where clause to search between two strings

I saw the answer provided in this question Wildcard-like syntax in an eloquent Where clause? now I want to know if is there a way to search between two strings?.

basicasicaly in my code I want to show requests that have a status of new or scheduled.

$requests = DB::table('requests')
        ->select('requestDate','requestTime','status','requestID')
        ->where('requestorID', '=',$userID)
        ->where('status', 'LIKE','%New%')
        ->get();


via Chebli Mohamed

lundi 8 novembre 2021

create dynamically checkbox in blade

I´m traying to create list of checkbox in my blade with all roles that i have in my DB. I´m doing loop to get all roles and if to check that roles have this user that i´m doing edit:

@foreach($roles as $rol)
   @foreach ($selRoles as $role)
     @if ($role == $rol->id)
        <div class="col-md-4">
          <input type="checkbox" name="rol" checked=checked class="form-check-input" value="" id="">
          
       </div>
     @else
      <div class="col-md-4">
         <input type="checkbox" name="rol" class="form-check-input" value="" id="">
         
      </div>                                                   
     @endif
   @endforeach
@endforeach

With this code, my problem it´s that all my roles it´s duplicated. In my controller i have this:

public function edit(User $usuario)
    {
        $roles = Bouncer::role()->orderBy('title', 'DESC')->get();
        $selRoles = $usuario->roles->pluck('id')->toArray(); //selRoles it´s roles from user
    
        $usuario->load('roles');

        return view('admin.empleados.edit', compact('usuario', 'roles','selRoles'));
    }

and retult in blade it´s:

teleoperadora
teleoperadora
teleoperadora
repartidor
repartidor
repartidor
prueba
prueba
prueba
Jefe de equipo
Jefe de equipo
Jefe de equipo
jefa-sala
jefa-sala
jefa-sala
instalador
instalador

for example. I don´t know that i´m doing bad for get this result in my blade. Anybody can help me please?

Thanks for read and help. Sorry for my english



via Chebli Mohamed

Laraval storage:link command failing because of Database Error

When I am executing the below command in Laraval

php artisan storage:link

It throws a database connection error

 Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `settings`)
 at /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669

I don't see any relation between storage:link command and database connection. Just want to understand what exactly can be wrong here.



via Chebli Mohamed

How can I fix this error related to to controller?

I have a problem, thank you for your help. I want this to be done in the Store method when a person submits a question But it gives me an error that says: thread_id required ???!

  Subscribe::query()->create([
        'thread_id'=>$thread->id,
        'user_id' => auth()->user()->id
    ]);

This is the code for my Store method, which includes the subscribe model:

 public function store(Request $request,Thread $thread)
{
    $request->validate([
        'title' => ['required', 'min:3'],
        'description' => ['required'],
        'channel_id' => ['required'],
        'thread_id' => ['required']
    ]);

    Thread::create([
        'title' => $request->title,
        'description' => $request->description,
        'user_id' => auth()->user()->id,
        'channel_id' => $request->channel_id,
    ]);

    Subscribe::query()->create([
        'thread_id' => $thread->id,
        'user_id' => auth()->user()->id
    ]);
    return redirect('/');
}

This is also the code related to View

<form action="" method="post">
@csrf
<input type="hidden" name="thread_id" value="">

<div class=" container-fluid w-75 border border-secondary rounded shadow">
    <div style="font-weight: bolder; font-size: large;" class=" my-3">فرم ارسال پرسش</div>
    <div class="input-group mb-3">
        <div class="input-group-prepend">

        </div>
        <input type="text" name="title" class="form-control" placeholder="موضوع" aria-label="Username" value=""
               aria-describedby="basic-addon1">
    </div>
    
    <div class="input-group">
        <textarea name="description" class="form-control" aria-label="With textarea" placeholder="متن پرسش"
                  style="height: 200px;"></textarea>
    </div>


    <div class="input-group mt-3 " style="direction: ltr;">
        <select name="channel_id" class="custom-select" id="channel_id" >
            <option selected disabled value="">انتخاب عنوان</option>
            @foreach(\App\Models\Channel::all() as $channel)
                <option value=""></option>
            @endforeach
        </select>

    </div>

    <div class="my-3" style="width: 100px;">
        <button class="btn btn-success" type="submit">ارسال</button>
    </div>


</div>

the problem about this code on view

<input type="hidden" name="thread_id" value="">

also i sent the Thread model on view

 public function create(Thread $thread)
{
    return view('answer-question.thread.thread-create',compact('thread'));
}

this is subcribe database enter image description here



via Chebli Mohamed