mercredi 23 mai 2018

How to filter products by range price and attributes in laravel 5.5

products table: id,name,price

attributes table: id,name,label,type{select or text}

attribute_values table: id,attribute_id,name,label

attribute_product_attribute_value table(pivot table): product_id,attribute_id,attribute_value_id,value

my code:

if ($category = CategoryProduct::getCategoryByValue('id', decode(\request()
   ->input('category')))) {'
        $query = $category->products()->where('price', '>=', 
          (int)\request()->input('min_price'))->where('price', '<='
          , (int)\request()->input('max_price'))->whereHas('atts', function 
            ($query) {
           foreach (Input::get('att_filter') as $atts) {
             foreach ($atts as $index => $value) {
             $query->atts()->where('att_id', decode($index))
           - >where('sub_att_id', decode($value));

           }
   ' }

    })->get();



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

Laravel Eloquent distinct not working as intended

I'm currently creating a basic private messaging system in Laravel. In my database I have a messages table. Two of the columns are "sender_id" and "recipient_id".

What I am trying to do is use eloquent to go through this table and list in the inbox all the times the current logged in user's id is listed in these columns. I will then use "distinct" to ignore duplicate entries so that the user is only listed once. Using this ID, I can display the username of the recipient so that when the user clicks on the username, it will take them to the message thread for that user.

For the sake of testing, I am only currently retrieving the column "recipient_id" and outputting it into the blade. Here is the line I believe should only output the recipient once despite being found many times in the table.

MessageController

$messages = Message::where('recipient_id', $user)->distinct('recipient_id')->get();

However in the blade, the same user is being output twice (as they have 2 messages found) when I would have expected the distinct function to remove the duplicate entry.

I have also tried

$messages = Message::where('recipient_id', $user)->distinct()->get();

But that also did not work.

If I have received 3 messages from 2 different users, it is outputting something like below:

Messages from:

  • Example User 1
  • Example User 1
  • Example User 2

What it should output

  • Example User
  • Example User 2

Thanks



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

Error in app.css after installing laravel 5.4

After installing laravel using composer, I found out that there's an error in app.css. Is this normal?



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

Laravel - how to use belongsToMany relationship using second index and not primary key

App overview

I'm learning Laravel and my dummy app allows users to log issues against projects. A project can have many issues and an issue can have many tags assigned to it.

I'm currently working on projects/show.blade.php which outputs all issues associated with a project in an HTML table with columns for name and tags.

Issue

I've setup 'belongs to many' relationships so that my issues, tags and issues_tags (intermediary) tables can talk to each other but I've run into a problem whereby tags are being output against the wrong issues.

The problem stems from the fact that my issues table (by design) includes an item_id column, which is an index but not the primary key. I use item_id for routing/display on the frontend because I wanted the issue ID that users see to start from 1 for every new project added instead of being sequential and everyone knowing how many there are in the database.

For example, tags assigned to issue #3 are called using issues.id and not issues.item_id.

Sample data

Sample data

Code

Issues migration

    Schema::create('issues', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('item_id')->index()->unsigned();
        $table->integer('project_id')->unsigned();
        $table->string('name');
        $table->longText('description');
        $table->tinyInteger('status')->default('1');
        $table->integer('created_by');
        $table->timestamps();
    });

Tags migration

Schema::create('tags', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Issues tags migration

Schema::create('issues_tags', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('tag_id')->unsigned()->index();
    $table->foreign('tag_id')->references('id')->on('tags');
    $table->integer('issue_id')->unsigned()->index();
    $table->foreign('issue_id')->references('item_id')->on('issues');
    $table->timestamps();
});

Issue model

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

Tag model

public function issues()
{
    return $this->belongsToMany('App\Issue', 'issues_tags', 'issue_id')->withPivot('item_id');
}

View (projects/show.blade.php)

  <table>
    <tr>
      <th>ID</th>
      <th>Item ID</th>
      <th>Issue</th>
      <th>Tags</th>
    </tr>
    @foreach($issues as $issue)
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td>
        @foreach($issue->tags as $tag)
          <span class="badge badge-primary"></span>
        @endforeach
      </td>
    </tr>
    @endforeach
  </table>


Question

How can I use issues.item_id instead of issues.id when using a belongsToMany relationship in Laravel?



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

What is the best way to make this query

I three have tables

Products table id title e.tc

sub_category table id prodouct_id e.tc

collections table here i save all products that will go live under a subcategory

id product_id subcategory_id

i want query that will pull all products under a subcategory form the collections table.

Thanks guys.



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

How can get the value from the third tanble in laravel and order by with id in descending order

In controller i have a code like this :

public function index(Request $request)
    {
        $course=Student::with('StudentDetail')->get();
        $courses=$course->toArray();
        echo "<pre>";print_r($courses);exit;
        return view('student.student_listing',['result'=>$courses]);

    }

through this i am getting result like :

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Neha
            [email] => neha@sob.com
            [roll_no] => 101
            [created_at] => 2018-05-22 09:20:18
            [updated_at] => 2018-05-22 00:00:00
            [student_detail] => Array
                (
                    [id] => 1
                    [student_id] => 1
                    [phone] => 11691110876
                    [course] => Java
                    [city] => Goa
                    [state] => 1
                    [created_at] => 2018-05-22 16:48:36
                    [updated_at] => 2018-05-22 09:20:43
                )

        )

    [1] => Array
        (
            [id] => 2
            [name] => Ankit
            [email] => Ankit@sbo.com
            [roll_no] => 102
            [created_at] => 2018-05-22 09:20:18
            [updated_at] => 2018-05-22 00:00:00
            [student_detail] => Array
                (
                    [id] => 2
                    [student_id] => 2
                    [phone] => 12313311
                    [course] => Phps
                    [city] => Delhi
                    [state] => 2
                    [created_at] => 2018-05-23 13:28:19
                    [updated_at] => 2018-05-15 03:15:13
                )

        )

)

Here i am getting a field state where i am getting the id this id related to the states table id how can i get the value from states table bassed on this id and i want this result in Descending order with name or id how can i do that can anyone please help me related this .

models i have like :

StudentDetail Model:
<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use App\Http\Model\Student;
use App\Http\Model\State;
class StudentDetail extends Model
{ 
    public function Student()
    {
         return $this->belongsTo(Student::class);

    }
  public function state()
    {
         return $this->hasOne(State::class);

    }

}



Student model:
<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use App\Http\Model\StudentDetail;

class Student extends Model
{ 
     public function StudentDetail()
    {
        return $this->hasOne(StudentDetail::class);
    }

}



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

Laravel: get url parameter in view

I'm trying to get the URL parameter in a view. what would be the way if my URL is like locahost:8000/example/10 and I wanna get the value 10.



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