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!
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.
I always find useful tips in this channel ;)
Keep going!
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.
You Made My Day ...
What an excellent tutorials ....
I am learning a lot ....
thank you soo much
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!
Thanks!
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.
How can i get active customers via url by their active attribute name? something like example.com/customers?active=active
or ?active=inactive
You really awesome guy, i am professional now in laravel just because of you and traversy media also, thaks from the heart
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)
Where do you have customer list link in your code? :)
Wow. Thats actually a pretty good job! Nice video!
Thanks
i didn't understand why we changed customers to /customers
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?
did you test it? Does it work for you? Personal preferences are always optional.
I open webpage in localhost(localhost/Laravel/Laravel/public/). How can I write form action path in .?
Hi Victor. Laravel propose the few approaches for JSON model conversion - resource & toJson serialization. Which one you prefer and when? Thanks.
wow....thanks for the clear explanation. I love the way accesors and mutators work!!!
Blew my mind when I first started with laravel.
very cool tips on Active column. Thanks
I am enjoying your video. Please make an e-commerce website with laravel 5.8.
nice and helpful tutorial,
thanks sir
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!
compact('customers') is just a shortcut for ['customers' => $customers]
saves you from typing customers twice.
Very Very well taught and helpful tutorial
Keep it up! Great series
i follow the code Attributes Active and InActive But they Didnt work from me
public function getActiveAttribute($attribute)
{
return [
0 => 'inactive',
1 => 'active'
][$attribute];
}
Hi, I am confused with the difference of with and without "/" in form action property and nav href property. Thanks.
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.
@@CodersTape Got it. Thanks.
@@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.
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];
}
You must be passing something other than 0 or 1.
Check what the field definition is for your status column. Is it integer? Does it only have 0 and 1 values?
I came back here again for the mutator trick you had, thanks again :)
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.
That’s a great question, check out the other comments on this video for an explanation. We had already spoken about what this means.
Hi, is that possible to use Accessors for foreign key? For example, I have foreign key named branch_id, so it getBranch_idAttribute?
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.
@@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);
Maybe I should put the foreign key for Report in "Branch" table?
may god always bless you good man
Thank you, truly appreciate that.
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');
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.
@@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
Compare your code to the code repo and spot the difference. Or put up your code so I can take a look.
@@CodersTape
Plz find the errors Victor
CustomerController.php
VIctor Plz Help
@Coder's Tape
i didn't understand why we changed customers to /customers
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.
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. :)
check again your routes. It should look like in the video. :) I guess you still have get('customers','CustomerController@store'), should be post().
@@dgloria replied very late. 😁 but thanks
@@bo88o I just started this tutorial.
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.
Certainly, if you’d like it to match.
@@CodersTape I am curious why "customer" works in Route and a.href, but doesn't work in form.action. Thanks.
@@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".
Great content
Great
awesome
You're the best!!!
I keep getting 404 page not found when ever i press on customers at the nav bar
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
@@CodersTape i have too 404 and redirect('customers') to /customers/customers url insted of /customers for some reason
@@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 .....
@@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
Very strange. Please write back on this comment when you sort it out so others may benefit as well.
Anyone please explain me what it is actually doing
In the Accessors [$attrbt] end of the array.... ???
Not sure what you mean by that question.
public function getActiveAttribute ($attribute) {
return [
0 => 'active',
1 => 'Inactive'
] [$attribute]
}
[$attribute] what it's actually doing end or return array???
@@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.
Thanks for explanation brother... Learning alot from your video..
@@CodersTape Thank You Sir
thanks sir you are perfect keep it up
The BEST as always!
when i click on add customer form just reloads any help please?
Maybe you mistyped the form action?
@@CodersTape the form action = "/customers"
can it be anything else?
@@CodersTape found the issue deleted the company field from create so it would not work awesome tutorial either way!!