Laravel 5.8 Tutorial From Scratch - e15 - Eloquent Accessors & RESTful Controller - Part 1

Поделиться
HTML-код
  • Опубликовано: 1 окт 2024
  • It’s time for us to tackle a refactor of our controllers and views to implement a RESTful controller approach. Follow along as we implement the index, create and store methods in our controller and properly store our views, following the same approach. As a last bit, let’s write an accessor for our active column for better display.
    For the best experience, follow along in our interactive school at www.coderstape...
    Resources
    Course Source Code
    github.com/cod...
    laravel.com/do...
    Hit us up on Twitter with any questions or comments @codertape ( / coderstape )
    About This Course
    Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world's most popular PHP framework. Let's get started!

Комментарии • 88

  • @NitishKumarPatra
    @NitishKumarPatra 5 лет назад +7

    That attribute accessor for status fetching was new to me. I have been using accessor but never knew this was possible. Been into Laravel since two years, still new and lots of things to learn. Thanks for sharing. That will definitely reduce few clutter in the blade.

  • @MaurizioCalarese
    @MaurizioCalarese 5 лет назад +9

    I always find useful tips in this channel ;)
    Keep going!

  • @dgloria
    @dgloria 4 года назад +1

    Fantastic video! A few comments. :)
    1., Careful with VS Code, it doesn't have a refactor skill, you have to replace all occurences within your project by yourself. What I use to do is do a search on the name and go one by one and change them. Namespaces as well.

    2., Also I seem to remember PHPStorm has something like create a new file from this one. Can't remember, perhaps in the context menu.
    3., If you aren't using php artisan serve, action="/customers" wont work. use "../customers" for now, I think url() has to be added on a long term. For now it's ok. Video here: ruclips.net/video/QQl7VJKRoTg/видео.html
    4., In Laravel 7 if your field name is is_active, then use getIsActiveAttribute instead. That's the PascalCase version of the column name I guess.

  • @salmanahmaddsk
    @salmanahmaddsk 5 лет назад +4

    You Made My Day ...
    What an excellent tutorials ....
    I am learning a lot ....
    thank you soo much

  • @marjohnsaycon235
    @marjohnsaycon235 4 года назад +2

    Very neat and clean tutorials! from procedural and then changing the codes how it should be. The explanation is very SUPERB! Don't forget to click the SUBSCRIBED BUTTON!

  • @petritmekuli
    @petritmekuli 3 года назад

    This is how I understood this method. Not really sure were but this method get called somewhere into the model and change the active attribute before we try to use it.
    function getActiveAttribute($attribute){
    // return [
    // 0 => 'Inactive',
    // 1 => 'Active'
    // ][$attribute];
    if($attribute == 0){
    $attribute = 'Inactive';
    }elseif($attribute == 1){
    $attribute = 'Active';
    }
    return $attribute;
    }
    You may see something like this as well:
    public function getFirstNameAttribute($value)
    {
    return ucfirst($value);
    //ucfirst is a string function that upper case the first character of a given string. In this case $value.
    }
    You are overriding the active attribute with something else. I mean not really overriding into the database but when you are using that attribute through the model. I just wanted to share the idea how I think this works(correct me if I'm wrong). This because I saw a lot of comments about this section. Hopefully this make sense.

  • @ErsenKiremitci
    @ErsenKiremitci 3 года назад

    How can i get active customers via url by their active attribute name? something like example.com/customers?active=active
    or ?active=inactive

  • @majdibrahem1399
    @majdibrahem1399 2 года назад

    You really awesome guy, i am professional now in laravel just because of you and traversy media also, thaks from the heart

  • @ayeniadedamola6207
    @ayeniadedamola6207 5 лет назад

    Please am new to laravel can someone help me out i have this error anytime i click customer list link or add new customer i get this error
    ErrorException (E_ERROR)
    syntax error, unexpected '/' (View: C:\Users\AYENI OLUWASEUN\my-first-project
    esources\views\customers\index.blade.php)

    • @dgloria
      @dgloria 4 года назад

      Where do you have customer list link in your code? :)

  • @i_likebeing_home8733
    @i_likebeing_home8733 5 лет назад +2

    Wow. Thats actually a pretty good job! Nice video!

  • @zombi0000000
    @zombi0000000 4 года назад

    i didn't understand why we changed customers to /customers

  • @scibuff
    @scibuff 4 года назад

    Wouldn't it be better to use the customer model getActiveAttribute in the create view where it creates the active/inactive dropdown? Is there way to define something like (Customer::)getActiveOptions which would return [0 , 1] and then you loop on that with customer->active to set the correct text for the select option?

    • @dgloria
      @dgloria 4 года назад

      did you test it? Does it work for you? Personal preferences are always optional.

  • @vashivimmy4548
    @vashivimmy4548 5 лет назад

    I open webpage in localhost(localhost/Laravel/Laravel/public/). How can I write form action path in .?

  • @SuperBatiskaf
    @SuperBatiskaf 5 лет назад

    Hi Victor. Laravel propose the few approaches for JSON model conversion - resource & toJson serialization. Which one you prefer and when? Thanks.

  • @alexcruz457
    @alexcruz457 5 лет назад +1

    wow....thanks for the clear explanation. I love the way accesors and mutators work!!!

    • @CodersTape
      @CodersTape  5 лет назад

      Blew my mind when I first started with laravel.

  • @RiazUddinMasum
    @RiazUddinMasum 5 лет назад +1

    very cool tips on Active column. Thanks

  • @mdmonirulislam4555
    @mdmonirulislam4555 5 лет назад

    I am enjoying your video. Please make an e-commerce website with laravel 5.8.

  • @robiulrobi6115
    @robiulrobi6115 5 лет назад +1

    nice and helpful tutorial,
    thanks sir

  • @EdioIlha
    @EdioIlha 5 лет назад

    Hello there! First of all thank you so very much about the great content you are sharing for free!! I completely agree with the comments here, You Rock *Victor*, keep going please! I'm following you on every social media! I don't wanna lose anything new!
    Can you, or some one reading this, explain why some times we see this code ($customers = Customer::all();
    return view('customers.index', compact('customers'));) written like ($customers = Customer::all();
    return view('customers.index', compact('customers', *$customers*));)
    I have read the documentation about compact() and I understood that, it will look for a variable above it, so the first one is the right way, but the second example are every where in other tutorials.
    I tried it and I don't see any difference!
    Thx a lot!

    • @CodersTape
      @CodersTape  5 лет назад +1

      compact('customers') is just a shortcut for ['customers' => $customers]
      saves you from typing customers twice.

  • @basharmalsafi2849
    @basharmalsafi2849 3 года назад

    Very Very well taught and helpful tutorial

  • @CACAMAKAify
    @CACAMAKAify 5 лет назад +2

    Keep it up! Great series

  • @curious_explorer1654
    @curious_explorer1654 4 года назад

    i follow the code Attributes Active and InActive But they Didnt work from me

    • @dgloria
      @dgloria 4 года назад

      public function getActiveAttribute($attribute)
      {
      return [
      0 => 'inactive',
      1 => 'active'
      ][$attribute];
      }

  • @stephenchen54
    @stephenchen54 5 лет назад +1

    Hi, I am confused with the difference of with and without "/" in form action property and nav href property. Thanks.

    • @CodersTape
      @CodersTape  5 лет назад +7

      When you start with a slash, the url resets. Example, if you’re currently in example.com/some/page and you click on a link of /something then you’ll be sent to example.com/something
      If you don’t have the leading slash, it will compound. With the same example, you would be sent to example.com/some/page/something instead.

    • @stephenchan8578
      @stephenchan8578 5 лет назад

      @@CodersTape Got it. Thanks.

    • @mohammadzuraiqien2341
      @mohammadzuraiqien2341 5 лет назад

      @@CodersTape Thanks for explaining that. I just wonder why the browser was looking for ......./customers/customers when we got the 404 error instead of looking for ......./customers/create/customers as we were on ..../customers/create page at that time.

  • @codewithasghar
    @codewithasghar 4 года назад

    my column name is status in my model i write a same code but its givin me error undifine offset
    public function getStatusAttribute($attribute){

    return [
    0 => 'Inactive',
    1 => 'Active',
    ][$attribute];
    }

    • @CodersTape
      @CodersTape  4 года назад

      You must be passing something other than 0 or 1.

    • @dgloria
      @dgloria 4 года назад

      Check what the field definition is for your status column. Is it integer? Does it only have 0 and 1 values?

  • @mohamedel-damarawy6595
    @mohamedel-damarawy6595 4 года назад

    I came back here again for the mutator trick you had, thanks again :)

  • @joshuaikpefua7821
    @joshuaikpefua7821 5 лет назад

    Please kindly explain this line of code return ['0' => 'active', '1' => 'inactive'] [$atrribute]
    I'm trying to understand it, so I don't forget.
    And again. Thanks very much for your skillful art.

    • @CodersTape
      @CodersTape  5 лет назад +2

      That’s a great question, check out the other comments on this video for an explanation. We had already spoken about what this means.

  • @heyitszad
    @heyitszad 4 года назад

    Hi, is that possible to use Accessors for foreign key? For example, I have foreign key named branch_id, so it getBranch_idAttribute?

    • @CodersTape
      @CodersTape  4 года назад

      It's possible, I think it would be getBranchIdAttribute BUT I wouldn't do that! All foreign keys would need relationships so you would have something like $model->branch and that's belongsTo relationship. Because a user belongs to a branch but a branch has many users.

    • @heyitszad
      @heyitszad 4 года назад

      @@CodersTape It worked, thanks. In my situation, I'm trying to get the data from the "Branch" table which is the name of the branch for my Report blade views. I only able to get the branch_id but not the data from the table.
      My relationship for my Branch model is:
      return $this->hasMany(Report::class);
      and for my Report model is:
      return $this->belongsTo(Branch::class);

    • @heyitszad
      @heyitszad 4 года назад

      Maybe I should put the foreign key for Report in "Branch" table?

  • @hamzaelmhoujab2350
    @hamzaelmhoujab2350 5 лет назад +2

    may god always bless you good man

    • @CodersTape
      @CodersTape  5 лет назад +2

      Thank you, truly appreciate that.

  • @AshishKashyap-zq8ws
    @AshishKashyap-zq8ws 5 лет назад

    Pz tell me the error....................????? have been waiting for ur answer since 2 days........
    when i click the button Add Customer....the url is like this
    127.0.0.1:8000/customers/customers
    and page not found 404.
    Route::get('customers/create','CustomerController@create');
    public function create()
    {
    $companies=Company::all();
    return view('customers.create',compact('companies'));
    }
    // public function store() // return redirect('customers');

    • @CodersTape
      @CodersTape  5 лет назад

      Sorry to take long. Your message went to the spam folder.
      Typically this happens because you’re putting just customers on your form action. Use /customers instead. Adding the slash will fix it.

    • @AshishKashyap-zq8ws
      @AshishKashyap-zq8ws 5 лет назад

      @@CodersTape Thanks for the reply... i did the same as u told me but it did not work....how can i solve this error Victor

    • @CodersTape
      @CodersTape  5 лет назад

      Compare your code to the code repo and spot the difference. Or put up your code so I can take a look.

    • @AshishKashyap-zq8ws
      @AshishKashyap-zq8ws 5 лет назад

      @@CodersTape
      Plz find the errors Victor
      CustomerController.php

    • @AshishKashyap-zq8ws
      @AshishKashyap-zq8ws 5 лет назад

      VIctor Plz Help

  • @zombi0000000
    @zombi0000000 4 года назад

    @Coder's Tape
    i didn't understand why we changed customers to /customers

    • @dgloria
      @dgloria 4 года назад

      Backslash takes you to the application root, whatever you have set it to be. For me it doesn't work, because my root is 'localhost'. This is just an easy solution, later in your project you'll define the exact path for all in your app using environmental variables, or globals.

  • @bo88o
    @bo88o 5 лет назад

    Greate Tutorials.
    I faced an issue
    If I tried to add customers with '/customers' it gives me error 'The post method is not supported for this route'. So, i tried /customers/store and it worked. I don't know how because for you it worked differently.
    I am still following your tutorials... They are excellent. Keep up this good work! and Thanks for sharing this knowledge in a much much simple way. :)

    • @dgloria
      @dgloria 4 года назад +1

      check again your routes. It should look like in the video. :) I guess you still have get('customers','CustomerController@store'), should be post().

    • @bo88o
      @bo88o 4 года назад

      @@dgloria replied very late. 😁 but thanks

    • @dgloria
      @dgloria 4 года назад

      @@bo88o I just started this tutorial.

  • @stephenchan8578
    @stephenchan8578 5 лет назад

    Hi, I am wondering why it is "'/customers"in the form action, while it is "customer" in the Route. Shouldn't these two be the same? Thanks a lot.

    • @CodersTape
      @CodersTape  5 лет назад

      Certainly, if you’d like it to match.

    • @stephenchan8578
      @stephenchan8578 5 лет назад

      @@CodersTape I am curious why "customer" works in Route and a.href, but doesn't work in form.action. Thanks.

    • @TrainDriver186
      @TrainDriver186 4 года назад

      @@stephenchan8578 Slow reply, but only working my way through this now, and I had the same question.
      Because the form is at "localhost/customer/create" so the action tries to point to "localhost/customer/customer," which of course there's no route for. You could also set your action to "../customer".

  • @kevinmutinda8429
    @kevinmutinda8429 4 года назад

    Great content

  • @MuhammadAdnan-gx6rd
    @MuhammadAdnan-gx6rd 5 лет назад +1

    Great

  • @rangabharath4253
    @rangabharath4253 4 года назад

    awesome

  • @cdrz23
    @cdrz23 4 года назад

    You're the best!!!

  • @abdelrahmanshaker9889
    @abdelrahmanshaker9889 5 лет назад +1

    I keep getting 404 page not found when ever i press on customers at the nav bar

    • @CodersTape
      @CodersTape  5 лет назад

      Check out the source code for the project on Github and compare to your local project. Hope that helps.
      github.com/coderstape/laravel-58-from-scratch

    • @kosticnemanja1914
      @kosticnemanja1914 5 лет назад

      @@CodersTape i have too 404 and redirect('customers') to /customers/customers url insted of /customers for some reason

    • @CodersTape
      @CodersTape  5 лет назад

      @@kosticnemanja1914 try adding a leading slash both in your controller and in your views. Something like redirect('/customers') and then in the link of the html .....

    • @kosticnemanja1914
      @kosticnemanja1914 5 лет назад

      @@CodersTape i just did it as you said, and its all the same url redirect to customers/customers .. (in controller in web.php in html and in redirect('/customers')) .. ill try to figure it out

    • @CodersTape
      @CodersTape  5 лет назад +1

      Very strange. Please write back on this comment when you sort it out so others may benefit as well.

  • @shuvoahmed5217
    @shuvoahmed5217 5 лет назад

    Anyone please explain me what it is actually doing
    In the Accessors [$attrbt] end of the array.... ???

    • @CodersTape
      @CodersTape  5 лет назад

      Not sure what you mean by that question.

    • @shuvoahmed5217
      @shuvoahmed5217 5 лет назад +1

      public function getActiveAttribute ($attribute) {
      return [
      0 => 'active',
      1 => 'Inactive'
      ] [$attribute]
      }
      [$attribute] what it's actually doing end or return array???

    • @CodersTape
      @CodersTape  5 лет назад +6

      @@shuvoahmed5217 Ahhh, I understand now, so that's just shorthand notation for the following...
      $status = [
      0 => 'active',
      1 => 'Inactive'
      ];
      return $status[$attribute];
      If you didn't have the [$attribute] it would return the entire array instead.
      So as an explanation, we are constructing an array matching the status code values to their string representations and then returning the specific status string using the $attributes as a key. The $attributes variable is coming from our database but as an integer, 0, 1, 2, 3 etc, so this effectively converts that to Active or Inactive so we can properly display that to our users.

    • @shuvoahmed5217
      @shuvoahmed5217 5 лет назад +2

      Thanks for explanation brother... Learning alot from your video..

    • @settunshein8254
      @settunshein8254 4 года назад

      @@CodersTape Thank You Sir

  • @anakyouzogoloov335
    @anakyouzogoloov335 5 лет назад

    thanks sir you are perfect keep it up

  • @adam192021
    @adam192021 5 лет назад

    The BEST as always!

  • @Neguskk
    @Neguskk 5 лет назад

    when i click on add customer form just reloads any help please?

    • @CodersTape
      @CodersTape  5 лет назад

      Maybe you mistyped the form action?

    • @Neguskk
      @Neguskk 5 лет назад

      @@CodersTape the form action = "/customers"
      can it be anything else?

    • @Neguskk
      @Neguskk 5 лет назад

      @@CodersTape found the issue deleted the company field from create so it would not work awesome tutorial either way!!