lundi 29 octobre 2018

laravel 5- get related products by search in Many To Many

Table 1: products: id,title
Table 2: features: id,name,values
Table 3:feature_product:id,product_id,values

I want get all related products when I search in values in feature_product table.

I do these:

in product model:

public function features()
{
    return $this->belongsToMany(Feature::class)->withPivot('values');
}

public function feature()
{
    // ???
}

and query for search:

 $q = 'yellow';
 $query->where(function($query) use ($q)
 {
    $query->WhereHas('feature' , function ($query) use 
    ($q){$query->where('values' , 'LIKE' , '%' . $q . '%' );});
 }

how can I search in related features of products? (and get those products)

I think I must do something in this function in product model:

public function feature()
{
        // ???
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2DaaWqV
via IFTTT

Set class active on click navigation link

I need help setting a link as active upon clicking on my html top nav bar.

The nav bar looks like this

   <div class="collapse navbar-collapse pull-left" id="navbar-collapse">
    <ul class="nav navbar-nav">
      <li>
          <a href="">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Clients <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="">Add New Client</a></li></i></a></li>
          </ul>
      </li>
    </ul>
   </div>

So when i click Home it must highlight Home when i click on Clients it must highlight Clients. I really don't know how to achieve this so any help will be very much appreciated.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2PrxKbn
via IFTTT

Query Builder Eloquent Where clause for TimeStamp - Laravel 5.1

I have a table and search bar.

enter image description here

When user input in the search that when I grab that and query my database.

This is what I got

public function getLogsFromDb($q = null) {

    if (Input::get('q') != '') {
        $q = Input::get('q');
    }
    $perPage = 25;

    if ($q != '') {

        $logs = DB::table('fortinet_logs')
            ->orWhere('account_id', 'like', '%'.$q.'%')
            ->orWhere('cpe_mac', 'like', '%'.$q.'%')
            ->orWhere('p_hns_id', 'like', '%'.$q.'%')
            ->orWhere('g_hns_id', 'like', '%'.$q.'%')
            ->orWhere('data', 'like', '%'.$q.'%')
            ->orWhere('created_at', 'like', '%'.$q.'%')
            ->orderBy('updated_at', 'desc')->paginate($perPage) <----🐞
            ->setPath('');


            //dd($logs);

        $logs->appends(['q' => $q]);

    } else {

        $logs = DB::table('fortinet_logs')
            ->orderBy('created_at', 'desc')->paginate($perPage)
            ->setPath('');
    }

    return view('telenet.views.wizard.logTable', get_defined_vars());

}


Result

In the network tab, I kept getting

Undefined function: 7 ERROR: operator does not exist: timestamp without time zone ~~ unknown

enter image description here


Questions

How would one go about and debug this further ?


I'm open to any suggestions at this moment.

Any hints/suggestions / helps on this be will be much appreciated!



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2OerLSK
via IFTTT

How to fix **Swift_TransportException** in Laravel 5.6

I am going to send contact form details via the gmail. My .env file is look like this,

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myname@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null

but when I send submit buttons following error is coming,

 (1/1) Swift_TransportException

Expected response code 250 but got code "530", with message "530 5.7.0 Must issue a STARTTLS command first. u5-v6sm3569015pgk.46 - gsmtp
"

then How Can I fix this problem?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Su8gZJ
via IFTTT

Does hasManyThrough ignore global scopes on the intermediate table?

I have 3 tables: companies, games and tests.

  • Companies have many games
  • Games have many tests

The Game model has a global scope, which I can confirm is working:

public function apply(Builder $builder, Model $model)
{
    $builder->where('type', 'live');
}

Any direct queries I do using the Game model will only return results where the game type is set to "live".

I am using return $this->hasManyThrough('App\Test', 'App\Games') in my Company model to get all tests for a particular company.

However, this is returning results for all games, regardless of their type.

So I am wondering if using hasManyThrough bypasses the global scope that I've set in the Game model?

If so, is there any way around this? I want to make sure that all queries I'm doing are filtering out any games that aren't set to "live".

Cheers



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2EN3hAf
via IFTTT

Eloquent manytomany polymorphic nested query withCount

I have the following manytomany polymorphic relationship

Asset

public function countries()
{
    return $this->morphToMany('App\Country', 'locationable');
}

Country

public function assets()
{
    return $this->morphedByMany('App\Asset', 'locationable');
}

Also Category has manytomany with Assets

public function assets()
    {
        return $this->belongsToMany('App\Asset', 'category_asset');
    }

I need to query a category and eager load assets that have country assigned.

Here is my eloquent query

$category = Category::with(['children.assets' => function ($query) {
            $query->whereHas('countries', function($q) {
                $q->where('code', '=', 'FR');
            });
        }])
            ->where('id', 1)
            ->first();

This seems to work, but then when I use the Category model with $this->assets It loads all of them, even if only one is returned in the query.

I am using API resources like so

AssetResource::collection($this->whenLoaded('assets'))

Where can I put a condition to only use assets that passed the condition where('code', '=', 'FR')



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2AyLXuN
via IFTTT

Duplicate Queries in Laravel- Debugger

I am trying to update the columns of the records based on the other column records but it results in the duplicate queries since its being executed for each record in the table.I s there a way to avoid it and improve this code.

 public function updateReviewColumn(){
        $reviewData = $this->pluck('indicator')->toArray();
        foreach ($reviewData as $datum) {
            if ($datum == 'Y')
                $this->where('indicator', $datum)
                    ->update(['reviewindicator' => 'Yes']);
            else
                $this->where('indicator', $datum)
                    ->update(['reviewindicator' => 'No']);
        }
    }



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2CK1vgV
via IFTTT