mercredi 1 juin 2016

Laravel / MYSQL (Eloquent) - Querying large table

I am making an online directory, this directory contains businesses, this is how the current table structure is set out:

1) "Business"

  • Name
  • Phone_Number
  • Email

2) Tags

  • id
  • tag

3) Business_tags

  • id
  • business_id
  • tag_id

There are over 9k rows inside of the business table, and over 84,269 rows and there are over 29k rows inside the ("Business_tags") table (As a business can have multiple tags).

Inside the business model, is the following:

public function tags()
{
   return $this->belongsToMany('App\Tags');
}

The issue is when I am trying to do a search, so for example, let's say that someone wants to search for a "Chinese" then it's takes more time than it probably should to return a value. For example, I am using:

$business = Business::where(function ($business) use ($request) {

    $business->whereHas('tags', function ($tag) use ($request) {

    }); 

})->paginate(20);

Searching takes on average: 35 seconds to display the results.

Here is the raw sql:

select * from `businesses` where (exists (select * from `tags` inner join `business_tags` on `tags`.`id` = `business_tags`.`tags_id` where `business_tags`.`business_id` = `businesses`.`id` and `name` in ('chinese')))

This takes on average: 52.4s to run inside Sequel pro (Using the raw SQL statement)

Any ideas how I can improve the performance of this query so that it's a lot faster? I want to have this functionality, but the user is not going to wait this long for a response!



via Chebli Mohamed

Display name instead of id. Laravel

I have two tables; Students and Grade. Students contains foreign key grade_id referencing Grade. For viewing students, I have used following code; Controller:

public function viewStudent()
    {

       $students=Student::all();
        return $students('grade_id');
        $grade = Grade::find($id, ['id', 'grade_name']);

        return view('students.view',compact('students'));
    }

View:

 <tbody>
            @foreach($students as $student)
            <tr class="info">
                <td></td>
                <td></td>
                <td></td>
                <td></td>


            </tr>

            @endforeach
        </tbody>

Model:

class student extends Model
{
    //
    protected $fillable = ['first_name','middle_name','last_name','grade_id'];
}

It shows grade_id as i have called it. But I want grade_name instead of that. Can anyone help me?



via Chebli Mohamed

Laravel pass data to view from controller?

How to pass data to a view in laravel using controlleror if we are using api then how to use composer to to get data in view??



via Chebli Mohamed

How would I connect to two databases in laravel 5.2, one SQL and one SQL Server?

I'm looking online and the only examples are of the same type of database.

Any help would be appreciated!



via Chebli Mohamed

how to use "belongTo" relationship in larvael

i have two models 'users' and 'doctors' i want have used eloquent relatonahiops hasMany and belongaTo to relate them as below

This is the model for users
   protected $fillable = [
    'name',
    'email',
    'password',
    'role_id',
    'active_user',
];

public function doctor()
{
    return $this->hasMany('App\Doctor','user_id');
}

and the model for doctors is below protected $table="doctors";

protected $fillable = [
    'role_id',
    'user_id',
    'name',
    'date',
    'gender',
    'specialization',
    'state',
    'country',
    'email',
    'mobile',
    'phone',
    'address',
    'password',
    'pic'

];
public function user()
{
    return $this->belongsTo('User','user_id');
}

now i have a table where i display the doctors using @foreach statement below

     <table id="example" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                                <thead >
                                <tr >
                                    <th>
                                        S/N
                                    </th>
                                    <th>
                                        Picture
                                    </th>

                                    <th style="text-align: center;">
                                        Name
                                    </th>

                                    <th style="text-align: center;">
                                        Specialization
                                    </th>

                                    <th  style="text-align: center;">
                                        Phone Number
                                    </th>

                                    <th  style="text-align: center;">
                                        Status
                                    </th>


                                    <th style="text-align: center;">
                                        Book
                                    </th>
                                    
                                    
                                    

                                </tr>
                                </thead>
                                <tbody>
                                <?php $c=0;?>
                                @foreach($doctors as $dr)
                                    <tr style="padding: 10px; text-align: center;">
                                        <td></td>
                                        <td>
                                            <img src='' style="height:40px; width:40px;" class="img-circle">
                                        </td>
                                        <td style="padding: 20px;"></td>
                                        <td style="padding: 20px; "></td>
                                        <td style="padding: 20px;"></td>
                                        <td style="padding: 20px;">

                                        </td>
                                        <td style="padding: 20px;">
                                            <input type="radio" name="book" value="">

                                        </td>
                                       This is where i want to show if they are offline or online
                                    </tr>
                                @endforeach


                                </tbody>
                            </table>

My Controller is below

public function BookDoctors(){
    if(!Auth::check()) {
        return Redirect::to('admin/login');
    }
    $opt=User::findOrFail(Auth::User()->id)->operator->first();
    $doctors = Doctor::all();
    $patients = PatientInfo::all();
    return view('Others.Operator.Appointment.book_doctors')->with(compact('opt','doctors','patients'));

}

The Challenge now is that i want to get the "active_user" field from the users migration and include it the form that has a table head of "status" using belongsTo relationship in laravel



via Chebli Mohamed

Laravel Scheduler - specify sender when using emailOutputTo()

How to specify sender for email reports which are sent by using emailOutputTo() method of Laravel Scheduler?

Currently from header is empty and I see in Gmail:

enter image description here

enter image description here


My code is:

$schedule->command('some:task')
    ->dailyAt('05:30')
    ->sendOutputTo(config('path.logs') . '/output.log')
    ->emailOutputTo('email@example.com');


I'm using built-in Laravel Mail provider with mailgun driver.



via Chebli Mohamed

preg_replace error at the time of upload multiple image in laravel

I got error while uploading multiple image in laravel.

HTML Code :-

<div class="form-group">
<label for="title">Art Gallery</label>
 <input type="file" name="art_image[]" id="art_image" value=""  accept="image/*"  multiple="multiple">
</div>

:- Controller code

if ($request->hasFile('art_image')) {
   $fileImage1 = $request->file('art_image');
   $StoreName = array();
   foreach ($fileImage1 as $files) {
         $filename1 = time().rand(1,100).".".$files>getClientOriginalExtension();
     $StoreName[] = $filename1;
     if($files->move(ART_IMAGE_DIR_PATH, $filename))
         {
              $data['art_image'] = $filename1;
           }
        }
   $artdetail_model->art_image = serialize($StoreName);
     }

I got below error

preg_replace(): Parameter mismatch, pattern is a string while replacement is an array



via Chebli Mohamed