design-studio

Experience

64,470

5 Best Reply Awards

  • Member Since 3 Years Ago
  • 932 Lessons Completed
  • 2 Favorites

20th February, 2018

design-studio left a reply on Give Notification To User When Integrity Constraint Violeted • 1 day ago

you are missing a return statement.

the last line should be

return  Redirect::to('profile_me')->with($notification);

29th January, 2018

design-studio left a reply on Relationships In Existing Project • 3 weeks ago

I think that there is a mix-up in what you are trying to do. You said the B is a pivot Table, but your explanation does not match. If B is indeed a pivot table it can be simplified.

A has the following:

public function C()
{
    return $this->belongsToMany(C::class)->latest();
}

C will have the following:

public function A()
{
    return $this->belongsToMany(A::class)->latest();
}

You don't even need to create the B Class (although you can if you need to add functionality to it).

If you need to get more data from the pivot table take a look at the 'Retrieving Intermediate Table Columns' section in the many-to-many documentation.

28th January, 2018

design-studio left a reply on I Can't Delete The Thread :/ • 3 weeks ago

A little more data is required.

24th January, 2018

design-studio left a reply on Current Week • 4 weeks ago

You could do something like this:

public function getCurrentWeekRecords() {
    $now = \Carbon\Carbon::now();
    $weekStart = $now->subDays($now->dayOfWeek)->setTime(0, 0);
    return \App\Model::where('created_at', '>=', $weekStart)->get();
}

This would get you a collection of App\Models that were created in the past week.

23rd January, 2018

design-studio left a reply on How Do I Rebind A Singleton ? • 4 weeks ago

I am have not used laravel/passport before, but if you would like to try the first option you shuold extend the passport service provider Laravel\Passport\PassportServiceProvider. There you can override the methods you want, be it registerAuthorizationServer or makeAuthorizationServer.

Don't forget to register your service provider instead of Laravel\Passport\PassportServiceProvider

14th January, 2018

design-studio left a reply on Undefined Property: PHPExcel::officename (View: /var/www/html/test/resources/views/view.blade.php) • 1 month ago

Glad I could help. Please mark one of my responses as best reply, if you find it so.

11th January, 2018

design-studio left a reply on Undefined Property: PHPExcel::officename (View: /var/www/html/test/resources/views/view.blade.php) • 1 month ago

You should check the returned $results class. I think it is a collection of rows or sheet (that extends laravel's collection).

Try to dump the variable (dd($result)) or its class (get_class($result)) to know it's type and how to continue.

If it is a collection of rows or sheets you need to drill to the row and then get the row data.

The row is also a collection so you'll need to use collection methods to retrieve data from it: $results->get('officename')

10th January, 2018

design-studio left a reply on Undefined Property: PHPExcel::officename (View: /var/www/html/test/resources/views/view.blade.php) • 1 month ago

What Error are you getting ?

I think the error might be related to the $data variable. I think you need to pass the $data down the functions:

Another error I see here is that on the code that you show here, there is an unnecessary } at the end.

Try this:

\Excel::load('/storage/app/sample.xls', function($excel) use ($data) {

    $excel->sheet('sample', function($sheet) use ($data) {
        $sheet->loadView('view',array('data' => $data));
        $sheet->setOrientation('landscape');
    });
        
})->export('xls');
  

9th January, 2018

design-studio left a reply on Using Two Methods Of Registration • 1 month ago

I think I would go about it a bit differently.

I would use the standard Auth() method to register, and set a default role. Then I would add a setRole or addRole method (depending on whether a user can have many roles) on the User model. The functions will be called as you wanted from the admin side of the application.

3rd January, 2018

design-studio left a reply on I Need Help To Find Out A Solution • 1 month ago

This fits exacly a Polymorphic Relationship.

In polymorphic relationship the parent model (Location in your case) has a relationship that allow connecting to different models while saving does models (Admin and Agent in your case) classes and their id.

You might need to make some changes thought. I think this should work:

On your location table you need to change the columns to added_by_type and added_by_id.

Now in your Location model you can use this method:

public function added_by() {
    return $this->morphTo()
}

On your Admin and Agent models you can use this methods to get the locations:

public function locations() {
    return $this->morphMany(App\Location::class, 'added_by');
}

You can use different names, but you'll need to dive into the code to know what extra parameters you'll need to use.

Hope i didn't make any mistakes, since I just wrote the code here :-)

25th December, 2017

design-studio left a reply on Laravel Page And Subpage Schema • 1 month ago

I think That you should choose from either option A or B. Option C to me seems complex considering the other 2 and Option D to me is not worth considering.

For both options A and B don't forget to use either trait or hierarchy.

I would go with option A, since I think this would be the simplest to manage, although option B might be faster performance wise (I am not a performance guy).

My last suggestion, and could be the most important one is to checkout the MorphOne Class. It is not a documented Class but dive into it's code, try and use it to see if it fits your need. From what i saw (and it can be guessed by the name) its result is not a Collection but rather a Model.

It would be great if you let us know what was you choice in the end, and why.

3rd December, 2017

design-studio left a reply on Help With Querying A Model • 2 months ago

The ->get() method returns an Eloquent Collection. if you are unfamiliar it is kind and array which is a multi result of the query you created (see the docs). In you case it will bring all App\Sb model that matches the given $token, even if there is only one model matching that.

The ->first() method returns the first model of that matches the given $token. It would in fact be the first model in the collection from the ->get method. From the model you can get whatever data you need.

You need to use the right method for any given situation. The show method is a classic place to use the ->first() method.

design-studio left a reply on 3 Category Tables With Same Structure, How To Manager Them With One Controller, Model, And View Etc At Admin? • 2 months ago

Not sure what you mean exactly since you didn't gave much data on what exactly yon need help with, but it looks like what you need is a Polymorphic relationship. Check the docs to see if this is what you wanted

26th November, 2017

design-studio left a reply on Order Table Using Another Table • 2 months ago

You should take a look at the counting-related-models docs You need to query the thread with the posts count, and then use that to sort by.

Assuming the relationship method in the Thread class is posts:

$threads = Thread::withCount('posts')->orderBy('posts_count')->get()

Of course if you use the post data in this request, you should also eager load them:

$threads = Thread::withCount('posts')->with('posts')->orderBy('posts_count')->get()

22nd November, 2017

design-studio left a reply on Check BelongsToMany Before Deleting It • 2 months ago

I would use events for this. I would use the deleting event to check if the model can be deleted, and stop the event if necessary.

see model events in the docs

20th November, 2017

design-studio left a reply on How To Dynamically Set Hidden Attribute, When Passing Model Data To Vue Components? • 3 months ago

You said:

I tried to limit the passed data by using the $hidden-attribute on the model, but this doesn't work in all use cases. What does it mean ? what cases ? It should work for all attributes, including relationships.

You can also user the opposite $visible property or hiding/showing them at runtime. See: 'hiding-attributes-from-json' in the doc

19th November, 2017

design-studio left a reply on Keeping Correct Data On Views • 3 months ago

While sharing variables across views is great (as @rumm.an explained), I think in the case you presented here I would rather delete the user and redirect either back or to the index, and let the index method deal with getting the users.

 public function softDelete(User $user)
 {
    $user->delete();

    // redirect back
    return redirect()->back();

    // OR redirect to the [email protected], you started with
    return redirect()->route('home.index')
    return redirect()->to('/')
}

15th November, 2017

design-studio left a reply on How Can I Solve This 404 Not Found Error Even When Route Exist • 3 months ago

  1. You said your route list results in: POST | mappy | location | App\Http\Cont rollers\[email protected] | web,auth there is space in the middle of the word Cont rollers. I assume it is a mistake done when pasting the text here, but check it out.
  2. Are you running the command in the http://127.0.0.1:8000 or maybe in some sub-directory, for example http://127.0.0.1:8000/my_test_app/ ? if so @@rumm.an js points to the wrong url and it won't work, and you'll have to create the url in the js using laravel, like you tried.

14th November, 2017

design-studio left a reply on Poly-polymorpic Relations? • 3 months ago

That looks OK to me. The pricesWhereType method, just like the prices doesn't return a collection. It returns a Illuminate\Database\Eloquent\Relations\MorphMany relation object. This objects delegates some methods to a \Illuminate\Database\Eloquent\Builder so you can use all the Builder methods to get what ever results you want. for example, to get all related retail prices use:

$product->pricesWhereType('retail')->get();

13th November, 2017

design-studio left a reply on Poly-polymorpic Relations? • 3 months ago

I think you should have a Price model, (with prices table) that will have fields for the price value and the price type, priceable_id and priceble_type. The type field will represent the different type possible (retail, advisory, sales etc.. ) Then all objects that needs to have prices will use a trait that will have methods to get the related prices models, and related prices models by type:

trait HasPrices {

    /**
     * All related prices
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function prices() {
        return $this->morphMany(\App\Price::class, 'priceable');
    }

    /**
     * Related prices by type
     *
     * @param string $type
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function pricesWhereType($type) {
        return $this->prices()->where('type', $type);
    }

}

You can even define scope on the Price model and use in the traitto make it more readable.

Does that work ?

design-studio left a reply on Dynamic Routes • 3 months ago

I think you have an exclamation mark where there shoudn't be one. That is it should be if($community) instead of if(!$community)

design-studio left a reply on Laravel Eloquent Order Posts By Created_at And Keep Sticky Post On Top • 3 months ago

You can chain 2 orderBymethods. first order by sticky_post then order by created_at. So it will look like this:

public function index(PostFilter $filter)
{
    return Post::with(['author', 'category', 'tags'])
    ->orderBy('sticky_post')
    ->orderBy('created_at')
        ->withTrashed()
        ->filter($filter)
        ->paginate(request()->perPage ? request()->perPage : 15);
}

note: you can also use the latest method. check the docs for Ordering, Grouping, Limit, & Offset

6th February, 2017

design-studio started a new conversation Visual Studio Code • 1 year ago

Hi Jeffrey. I was wondering if you tried the "Visual Studio Code"? What's your take on it? Also, if you liked it, and since I like simple concise way you explain things, I was wondering if you could give a run of your preferred setup for this editor.

Tnx.

Edit Your Profile
Update

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