Bakanyaka

Experience

18,865

3 Best Reply Awards

  • Member Since 3 Years Ago
  • 199 Lessons Completed
  • 0 Favorites

11th April, 2018

Bakanyaka left a reply on Arrays Key/value Pairs ... (brain Freeze) • 3 months ago

You need to provide more info on what $rent is and how you suppose to save it to DB. But I'l try to guess what you meant.

foreach($request->item as $key => $item) {
         $rent->$item = $request->values[$key];
}

Bakanyaka started a new conversation What Type Of Relationship Is This? • 3 months ago

Hello. I'm working with existing database and trying to create eloquent models for it. What I have is 4 tables:

ITEMS with following columns:

  • ID
  • NAME
  • COATINGMATERIAL_ID

COATINGMATERIAL with following columns:

  • ID
  • COATING_ID
  • MATERIAL_ID

COATINGS with following columns:

  • ID
  • NAME

MATERIALS with following columns:

  • ID
  • NAME

What I need is to get Material and Coating of Item from reference to row id of COATINGMATERIAL pivot table which is stored in COATINGMATERIAL_ID column. So basically I need it to work like this:

$item = App\Item::find(1)
$item->coating
$item->material

What's the best way to implement this as Eloquent relationship?

13th March, 2018

Bakanyaka left a reply on Is This Too Complex For Eloquent? • 4 months ago

Nah. It's only 50 or so users. And it's not likely to grow. Why do you think it's gonna affect perfomance? It's only a single query now.

Bakanyaka left a reply on HasOne Relationship Not Saving • 4 months ago

You need to retrieve Manufacturers model first. For example:

$product->Manufacturers()->save(App\Manufacturers::where('name', $request->manufacturer)->first());

Bakanyaka left a reply on Is This Too Complex For Eloquent? • 4 months ago

Yes. I want counts for all users. The result should be like this:

#items: array:2 [
    0 => {#1053
      +"assignee_id": "0"
      +"participated_issues_count": "1"
    }
    1 => {#1050
      +"assignee_id": "97"
      +"participated_issues_count": "2"
    }
  ]

As I said, I managed to do this with raw sql query but I have doubts that it is the proper way to do it.

Bakanyaka left a reply on Show Links() With Collection • 4 months ago

You need to manually make html for that. Or you can use https://github.com/spatie/laravel-collection-macros#paginate

Bakanyaka left a reply on Forms • 4 months ago

'requird'. You have a typo. 'e' is missing

Bakanyaka left a reply on Laravel Tutorial For A Nice Project • 4 months ago

I'm not sure if I can leave a link here to another subscription tutorial website. So just google for "Build a File Marketplace with Laravel" and "Build a classified ads site". If you want something free then you can try Amitav Roy youtube channel: https://www.youtube.com/channel/UC4gijXR8cM4gmEt9Olse-TQ/

Bakanyaka started a new conversation Is This Too Complex For Eloquent? • 4 months ago

Let's say I have 3 tables (unnecessary columns omitted in example):

  • issues table with columns: id, status_id, assigned_to_id - issues currently assigned to users
  • statuses table with columns: id, is_closed - issues statuses
  • time_entries table with columns: assignee_id, issue_id, spent_on, hours - issues that users spent time on

I need to calculate the sum of the number of open issues currently assigned to each user and the number of issues that user spent time on within given period. Also following should not be counted multiple times:

  • If multiple time entries exist for same issue
  • If user spent time on issue and issue is currently assigned to him.

That's my code atm which works fine according to my tests:

        $query = 'select assignee_id, COUNT(*) as participated_issues_count from (';
        $query .= 'select id, assigned_to_id as assignee_id from issues where exists (select * from statuses where issues.status_id = statuses.id and is_closed = ?)';
        $query .= ' UNION ';
        $query .= 'select distinct issues.id as id, time_entries.assignee_id from time_entries inner join issues on time_entries.issue_id = issues.id where spent_on > ? and spent_on < ?';
        $query .= ') s group by assignee_id';
        $participatedIssuesCount = DB::select($query, [0, $periodStartDate, $periodEndDate]);              

Same as SQL query:

select assignee_id, COUNT(*) as participated_issues_count from (select id, assigned_to_id as assignee_id from issues where exists (select * from statuses where issues.status_id = statuses.id and is_closed = ?) UNION select distinct issues.id as id, time_entries.assignee_id from time_entries inner join issues on time_entries.issue_id = issues.id where spent_on > ? and spent_on < ?) s group by assignee_id

But this is raw SQL. I spent a long time trying to do the same as Eloquent or Query Builder query but it never works as I need it to.

I tried this:

        $spentIssues =  TimeEntry::spentWithin($periodStartDate, $periodEndDate)
            ->join('issues', 'time_entries.issue_id', '=', 'issues.id')
            ->select('issues.id as id', 'time_entries.assignee_id')
            ->distinct();

        $participated_issues_count = Issue::open()
            ->select('id', 'assigned_to_id as assignee_id')
            ->union($spentIssues)
            ->groupBy('assignee_id')
            ->select(DB::raw('count(*) as participated_issues_count'));

But in ends up in totally wrong SQL query which doesn't even execute:

select * from (select count(*) as participated_issues_count from "issues" where exists (select * from "statuses" where "issues"."status_id" = "statuses"."id" and "is_closed" = ?) group by "assignee_id") union select * from (select distinct "issues"."id" as "id", "time_entries"."assignee_id" from "time_entries" inner join "issues" on "time_entries"."issue_id" = "issues"."id" where "spent_on" > ? and "spent_on" < ?)

So what do you think is the best way to do this? Leave is at as raw SQL? Do separate queries and merge the result on PHP side? Or maybe you know how to do correct Eloquent query?

Thank you for spending your time reading this long post.

20th February, 2018

Bakanyaka left a reply on How To Use Laravel In Vuejs Admin Dasboard • 5 months ago

Sorry. I forgot to mention that you need to copy required dependencies from adminify/package.json to package.json in your Laravel project root. It might be pretty complicated to merge if you are not familiar with webpack and laravel-mix.

But I found laravel vuetify starter App for you: https://github.com/codeitlikemiley/vuetified

19th February, 2018

Bakanyaka left a reply on PhpStorm IDE Helper • 5 months ago

Did you run php artisan ide-helper:generate and php artisan ide-helper:models?

Bakanyaka left a reply on How To Use Laravel In Vuejs Admin Dasboard • 5 months ago

You have two options:

  1. Integrate vuejs frontend into Laravel. Basically you put all the files from adminify/src/ into /resources/assets/js/ in your Laravel project folder. So main.js becomes your entry point and you need to replace app.js with it in webpack.mix.js. You'll also likely need to copy axios headers configuration from Laravel's resources/assets/js/bootstrap.js into adminify's http.js. Then you configure your Laravel routes to serve same view (your frontend app) on all routes except your backend Api routes which gonna return json data to your frontend.
  2. Serve your frontend and backend from different domains\subdomains. One server is static html\js another is Laravel API. It is easier to setup initially and you can use your admin dashboard build system without modifications but you'll likely run into CORS problems so you might read into it and use https://github.com/barryvdh/laravel-cors.

Bakanyaka left a reply on Can't Create A New Vue Component Using Laravel Mix. • 5 months ago

Yes, you either need to run npm run dev every time you change something in your js files or you can run npm run watch once and keep it running in background, it will automatically watch for changes and recompile your js and\or scss files.

14th February, 2018

Bakanyaka left a reply on Can't Create A New Vue Component Using Laravel Mix. • 5 months ago

You don't need to run npm to install vue components. Are you missing dot in the path? Probably should be require('./components/company-comment.vue').

Bakanyaka started a new conversation Extracting Filtering Logic From Controllers • 5 months ago

I have a controller with a lot of filtering logic based on request data. I managed to extract large part of it into a separate filter class like was shown in lesson 15 of "Let's Build A Forum with Laravel and TDD series". https://laracasts.com/series/lets-build-a-forum-with-laravel/episodes/15

But that filtering is based on Eloquent model scope and I have some filtering logic that uses several computed attributes that are not present in database (they are based on current time and working hours). So I wonder what is a good place to extract that logic into? I have several ideas myself:

  1. Make a repository for my model. Move filtering logic there. But is it proper use of repositories?
  2. Make a custom collection to be returned from eloquent model. Add custom methods to it similar to Eloquent scopes. Maybe even make it accept separate filtering class similar to eloquent one.

What do you think?

4th October, 2015

Bakanyaka started a new conversation Share Data Between All Controllers And Views • 2 years ago

What's the best way to share data between all controllers and views in Laravel?

I've found a way to make it available between all views (https://laracasts.com/discuss/channels/laravel/share-data-using-view-composers) but now I need to use it in one of the controllers without making another request to my database.

Bakanyaka left a reply on Share Data Using View Composers • 2 years ago

I've found a solution to my problem. In my HeaderComposer compose method I check if view already has mydata variable defined like this: public function compose(View $view) { $viewdata = $view->getData(); if (!array_key_exists('departments', $viewdata)) { //Retreive data from datasource } }

Bakanyaka started a new conversation Share Data Using View Composers • 2 years ago

I need to render some data retrieved from my datasource on all page headers and also on one specific page body. After some research I found out a way to do that using View composers (http://laravel.com/docs/5.1/views#view-composers). So I've created HeaderComposer and bound it to my header partial like this: view()->composer('partials.header', 'App\Http\ViewComposers\HeaderComposer'); And it works fine until it get to the page where I also want to use this variable $mydata in the page body where I get "Undefined variable: mydata" exception.

It seems that Laravel tries to render @section('content') first, can't find $mydata variable throws an exception and HeaderComoser doesn't even get called yet. If I bind HeaderComposer to both page and header like this: view()->composer(['pages.index','partials.header'], 'App\Http\ViewComposers\HeaderComposer'); It works but I end up with HeaderComposer compose method executed twice which results in two queries to my datasource.

Is there a way around that or probably some totally different way to insert data into all views?

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.