codinglara

codinglara

Member Since 5 Months Ago

Experience Points
710
Total
Experience

4,290 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
0
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-in-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
710 XP
Sep
13
1 month ago
Activity icon

Replied to Laravel 7 Vs Laravel 8 Dilemma

@snapey I appreciate your opinion but there's no need to be rude.

Sep
10
1 month ago
Activity icon

Started a new Conversation Laravel 7 Vs Laravel 8 Dilemma

Is it smart to work on new projects but use v7? I started learning Laravel 7 a few months ago and I got used to it, still have a lot of things to learn, I am having trouble understanding all the changes in v8.

I've been using Bootstrap and SASS for years now, so I am already having some issues with v8 because Laravel UI is dropped. I am not familiar with Tailwind CSS, also Livewire and Inertia seem complicated for me at the moment. Is Livewire (or Inertia) necessary or it's just an option? How to use Bootstrap in v8? What's the best practice for using Bootstrap in v8? Is it better to just learn and use Tailwind instead? I tried running these commands

composer require laravel/ui
php artisan ui bootstrap

but I have problems when I run

php artisan ui bootstrap --auth

because it's not working in v8.

Sep
09
1 month ago
Activity icon

Replied to Laravel 8 "Target Class [Auth\LoginController] Does Not Exist."

It's probably a stupid questions but can you still use Bootstrap in v8? And how? I am not familiar with Tailwind, so I tried to run these commands

composer require laravel/ui

and

php artisan ui bootstrap

and

php artisan ui bootstrap --auth

but it didn't work because v8 is now using jetstream.

Activity icon

Replied to Laravel 8 DB Exception After Installing Jetstream

I had the same problem, I updated variables in my .env file and then ran migrations and it worked.

Jul
16
3 months ago
Activity icon

Replied to OrderBy() Does Not Work

Thanks a lot!!! I completely forgot that I used a variable with the same name in View Composers. I just changed the variable name in Controller and then in view too and it works.

Thanks!

Activity icon

Replied to OrderBy() Does Not Work

When I do that, it's in the wrong order.

Activity icon

Replied to OrderBy() Does Not Work

So, I've managed to solve this by these changes in my code. But I am not sure if this is a good practice because I want to keep all DB related functions in my controller.

in my view, I used sortByDesc()

 <tbody>
                    @foreach($categories->sortByDesc('id') as $category)
                    <tr>
                        <td>{{ $category->id }}</td>
                        <td>{{$category->name}}</td>
                        <td>{{$category->description}}</td>
                        <td>{{$category->img_path}}</td>
                        <td class="text-center"><a href="{{ route('admin.kategorije.izmeni', ['id' => $category->id]) }}"><i class="far fa-edit"></i></a></td>
                        <td class="text-center"><a href="{{ route('admin.kategorije.obrisi', ['id' => $category->id]) }}"><i class="far fa-trash-alt"></i></a> </td>
                    </tr>
                    @endforeach
                </tbody>

this is how the function looks like in CategoryController

  public function getAdminCategories(){
        $categories = Category::all();
        return view('admin.kategorije.kategorije', ['categories' => $categories ]);
    }

I got what I wanted but I am not sure if this is a good approach. And I want to understand why orderBy() did not work.

Activity icon

Replied to OrderBy() Does Not Work

Here's the view

@extends('layouts.admin')
@section('pg-title')
    {{ "Kategorije | Admin" }}
@endsection
@section('content')
<div class="d-flex align-items-center sm-wrap">
    <h2>Kategorije</h2>
    <div class="ml-auto">
        <a href="{{ route('admin.kategorije.nova') }}" class="btn admin-button">Nova kategorija</a>
    </div>
</div>
@include('partials.admin.admin-info')
    <div class="sm-wrap">
        <div class="table-responsive">
            <table class="table table-hover table-dark table-borderless table-style">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>Naziv</th>
                    <th>Opis</th>
                    <th>Slika</th>
                    <th>Uredi</th>
                    <th>Obriši</th>
                </tr>
                </thead>
                <tbody>
                    @foreach($categories as $category)
                    <tr>
                        <td>{{ $category->id }}</td>
                        <td>{{$category->name}}</td>
                        <td>{{$category->description}}</td>
                        <td>{{$category->img_path}}</td>
                        <td class="text-center"><a href="{{ route('admin.kategorije.izmeni', ['id' => $category->id]) }}"><i class="far fa-edit"></i></a></td>
                        <td class="text-center"><a href="{{ route('admin.kategorije.obrisi', ['id' => $category->id]) }}"><i class="far fa-trash-alt"></i></a> </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
    </div>
@endsection
Activity icon

Replied to OrderBy() Does Not Work

This is my model for now

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Category extends Model
{
    use SoftDeletes;
    protected $fillable = ['name', 'description', 'img_path'];
}
Activity icon

Replied to OrderBy() Does Not Work

Yes, but even when I want to run some other queries orderBy is not working.

orderBy('created_at', 'asc);

orderBy('name', 'desc');

and so on.

Activity icon

Replied to OrderBy() Does Not Work

This is what I got after printing

Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\Category Object ( [fillable:protected] => Array ( [0] => name [1] => description [2] => img_path ) [connection:protected] => mysql [table:protected] => categories [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 10 [name] => Nova kategorija [description] => Jos jedna kategorija u nizu [img_path] => graphic.png [created_at] => 2020-07-16 08:26:06 [updated_at] => 2020-07-16 08:26:06 [deleted_at] => )


etc

It is in the correct order in this case. I am not using any jquery plugin for datatables.

Jul
15
3 months ago
Activity icon

Replied to OrderBy() Does Not Work

All I have in my view is a table where I want to show the data I retrieved from the DB.

<tbody>
                    @foreach($categories as $category)
                    <tr>
                        <td>{{ $category->id }}</td>
                        <td>{{$category->name}}</td>
                        <td>{{$category->description}}</td>
                        <td>{{$category->img_path}}</td>
                        <td class="text-center"><a href="#"><i class="far fa-edit"></i></a></td>
                        <td class="text-center"><a href="#"><i class="far fa-trash-alt"></i></a> </td>
                    </tr>
                    @endforeach
                </tbody>
Activity icon

Started a new Conversation OrderBy() Does Not Work

I am retrieving the data from the database, I get the results I need but they are not ordered. For example, I want to order my categories by id, desc.

Here's the first way I tried to do it.

  public function getAdminCategories(){
        $categories = DB::table('categories')->orderBy('id', 'DESC')->get();
        return view('admin.kategorije.kategorije', ['categories' => $categories ]);
    }

And another one which is basically the same.

  public function getAdminCategories(){
        $categories = Category::orderBy('id', 'DESC')->get();
        return view('admin.kategorije.kategorije', ['categories' => $categories ]);
    }

None of it works. The route is fine, I get all the results but not in the right order.

Jul
14
3 months ago
Activity icon

Awarded Best Reply on View Composer Problem - Data Not Passed

Please ignore. I actually did not register ViewServiceProvider in config/app.php.

I added the last line and it works

 * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

        App\Providers\ViewServiceProvider::class,
Activity icon

Replied to View Composer Problem - Data Not Passed

Please ignore. I actually did not register ViewServiceProvider in config/app.php.

I added the last line and it works

 * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

        App\Providers\ViewServiceProvider::class,
Activity icon

Started a new Conversation View Composer Problem - Data Not Passed

I want to retrieve all categories from the database in my nav menu, I want to pass all categories from database to a navigation view for the whole application. Not sure where I've made a mistake.

I created ViewServiceProvider.php which is registered in config/app.php

ViewServiceProvider.php

<?php

namespace App\Providers;
use App\Category;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class ViewServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        View::composer('*', function ($view){
            $view->with('categories', Category::all());
        });
    }
}

config/app.php

'providers' => [

        /*
		.........

         * Laravel Framework Service Providers...
         */

  
        Illuminate\View\ViewServiceProvider::class,

In header.blade.php I want to list all categories.

 @foreach($categories as $category)
                                                        <li>
                                                            <a href=""> {{ $category->name }}</a>
                                                        </li>
                                                    @endforeach

Category.php Model, nothing there yet

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $fillable = ['name', 'description', 'img_path', 'slug'];
}

CategoryController.php

<?php

namespace App\Http\Controllers;
use App\Category;
use App\Http\ViewComposers\NavComposer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class CategoryController extends Controller
{
    public function getCategories(){
        $categories = Category::all();
        return view('index', ['categories' => $categories]);
    }
}

App\Http\ViewComposers\NavComposer.php

<?php

namespace App\Http\ViewComposers;

use App\Category;

class NavComposer {
    public function compose($view){
        $view->with('categories', Category::all());
    }
}

And the error I get

Facade\Ignition\Exceptions\ViewException Undefined variable: categories (View: /home/user/Documents/projects/project/resources/views/partials/header.blade.php)

May
09
5 months ago
Activity icon

Started a new Conversation I Need An Accessor But Not On All Results/views

In admin panel, I have a (bootstrap) table in messages.blade.php view for previewing messages that come from the contact form (stored in DB table). I have a column named 'message' where I want to show the truncated message and then in order to see the full message I have a show.blade.php view.

In order to truncate the message when retrieving it from DB I used an accessor and it gets the job done. But when I want to see the full message in another view I also get the truncated message (and I understand why) but in that particular view, I want to see the full message.

Is there any way I can use an accessor but in one view only but not in others? Or to get the desired result without using accessors?

messages.blade.php (get all messages and show a truncated message)

<table class="table table-dark table-hover table-borderless">
                    <thead>
                    <tr>
                        <th class="admin-heading">ID</th>
                        <th class="admin-heading">First Name</th>
                        <th class="admin-heading">Last Name</th>
                        <th class="admin-heading">E-mail</th>
                        <th class="admin-heading">Message</th>
                        <th class="text-center admin-heading">View</th>
                        <th class="text-center admin-heading">Delete</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($messages as $message)
                        <tr>
                            <td>{{ $message->id }}</td>
                            <td>{{ $message->first_name }}</td>
                            <td>{{ $message->last_name }}</td>
                            <td>{{ $message->email }}</td>
                            <td>{{ $message->message }}</td>
                            <td class="text-center"><a href="{{route('admin.show', ['id' => $message->id])}}"><span class="icons"><i class="far fa-eye"></i></span> </a> </td>
                            <td class="text-center"><a href=""><span class="icons"><i class="far fa-trash-alt"></i></span> </a> </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>

show.blade.php (get a single message)

{{ $message->id }}
{{ $message->first_name }}
{{ $message->last_name }}
{{ $message->email }}
{{ $message->message }}

Model

<?php

namespace App;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    protected $fillable = ['first_name', 'last_name', 'email', 'message'];
    public function getMessageAttribute($value) {
        return Str::limit($value, 10);
    }
}

Route

 Route::get('messages/{id}', [
        'uses' => '[email protected]',
        'as' => 'admin.show'
    ]);

Controller

public function showAdminMessage($id){
        $message = Contact::find($id);
        return view('admin.contact.show', ['message' => $message]);
    }
May
08
5 months ago
Activity icon

Replied to Save() Function For Updating Doesn't Work And Doesn't Output An Error

@tray2 Thanks! I can't believe I've spent two hours trying to fix this.

Activity icon

Started a new Conversation Save() Function For Updating Doesn't Work And Doesn't Output An Error

When I want to save an updated post laravel does not output any kind of error, I get the success message but the updated post is not saved in the database. I'm getting familiar with Eloquent and I've read several threads but no help. All column names in the related table (and database in general) are lowercase.

dd($item->save());

returns true.

Here's my function in the controller:

  public function updateMenu(Request $request){
        $this->validate($request, [
            'name' => 'bail|required|max:255',
            'description' => 'required|min:10',
            'image_path' => 'required|max:255',
            'price' => 'required|numeric',
            'category_id' => 'required'
        ]);
        $item = Menu::find($request->input('id'));
        $item->name = $request->input('name');
        $item->description = $request->input('description');
        $item->image_path = $request->input('image_path');
        $item->price = $request->input('price');
        $item->category_id = $request->input('category_id');
        $item->save;
       // dd($item->save());
        return redirect()->route('admin.index')->with('info',
        'Selected menu item has been edited!');
    }

Here's the model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Menu extends Model
{
    protected $fillable = ['name', 'description', 'image_path', 'price', 'category_id'];
    public function category(){
        return $this->belongsTo('App\Category', 'category_id');
    }
}

Here's my view (form).

<form action="{{ route('admin.menu.update')}}" method="post">
                    <div class="form-group">
                        <label for="name">Name:</label>
                        <input type="text" class="form-control" name="name" id="name" value="{{$item->name}}">
                    </div>
                    @foreach($categories as $category)
                        <div class="form-check form-group">
                            <input type="radio" name="category_id" class="form-check-input" value="{{$category->id}}" id="category">{{$category->name}}
                        </div>
                    @endforeach
                    <div class="form-group">
                        <label for="description">Description:</label>
                        <textarea class="form-control" rows="5" name="description" id="description">{{$item->description}}</textarea>
                    </div>
                    <div class="form-group">
                        <label for="image_path">Image Path:</label>
                        <input type="text" class="form-control" name="image_path" id="image_path" value="{{$item->image_path}}">
                    </div>
                    <div class="form-group">
                        <label for="price">Price:</label>
                        <input type="text" class="form-control" name="price" id="price" value="{{$item->price}}">
                    </div>
           
                    {{ csrf_field() }}
                    <input type="hidden" name="id" value="{{$item->id}}"/>
                    <button type="submit" class="btn custom-btn custom-fill-btn">Edit</button>
                </form>

Here's the route, the route works btw.

 Route::post('edit', [
            'uses' => '[email protected]',
            'as' => 'admin.menu.update'
        ]);

I've used the same approach for updating some other models and it works just fine, not sure what's the problem here because no errors are thrown.

  • Also, only when I leave
dd($item->save);

in the update function, it actually updates the database, but without it doesn't update and I get no errors.

May
07
5 months ago
Activity icon

Replied to 1:M Relationship - Insert Problem

@tray2 That was it. Thanks a lot. It works now.

Activity icon

Replied to 1:M Relationship - Insert Problem

I can create a new item when I set the default value as null but that's not what I want to achieve.

Activity icon

Started a new Conversation 1:M Relationship - Insert Problem

I have two tables 'categories' and 'menus'. When I want to create a new menu item I get the following error:

'Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value (SQL: insert into menus (name, description, image_path, price, updated_at, created_at) values (Americano, New menu item, americano.jpg, 5, 2020-05-08 07:27:15, 2020-05-08 07:27:15))

I implemented CRUD functionality for 'categories' table and it works just fine, I have issues with a one-to-many relationship. Here's my code.

create_categories_table.php migration

    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('description');
            $table->string('image_path'); 
           $table->timestamps();
        });
    }

create_menus_table.php migration

    public function up()
    {
        Schema::create('menus', function (Blueprint $table) {
            $table->id();
            $table->integer('category_id');
            $table->string('name');
            $table->text('description');
            $table->string('image_path');
            $table->float('price');
            $table->timestamps();
        });
    }

Category Model

class Category extends Model
{
    protected $fillable = ['name', 'description', 'image_path'];

    public function menus(){
        return $this->hasMany('App\Menu', 'category_id');
    }
}

Menu Model

class Menu extends Model
{
    protected $fillable = ['name', 'description', 'image_path', 'price', 'category_id'];
    public function category(){
        return $this->belongsTo('App\Category', 'category_id');
    }
}

storeMenu() function in MenuController.php

    public function storeMenu(Request $request){
        $this->validate($request, [
            'name' => 'bail|required|max:255',
            'description' => 'required|min:10',
            'image_path' => 'required|max:255',
            'price' => 'required|numeric',
            'category' => 'required'
        ]);
        $item = new Menu([
           'name' => $request->input('name'),
            'description' => $request->input('description'),
            'image_path' => $request->input('image_path'),
            'price' => $request->input('price'),
            'category' => $request->input('category')
        ]);
        $item->save();
        //$item->category()->sync($request->input('category') === null ? '' : $request->input('category'));

        return redirect()->route('admin.index')->with('info', 'New menu item ' . $request->input('name') . ' has been created!');
    }

create.blade.menu

I am not sure my loop is fine.

<form action="{{ route('admin.menu.create')}}" method="post">
                    <div class="form-group">
                        <label for="name">Name:</label>
                        <input type="text" class="form-control" name="name" id="name">
                    </div>

                    @foreach($categories as $category)
                        <div class="form-check form-group">
                            <input type="radio" name="category" class="form-check-input" value="{{$category->id}}" id="category">{{$category->name}}
                        </div>
                    @endforeach

                    <div class="form-group">
                        <label for="description">Description:</label>
                        <textarea class="form-control" rows="5" name="description" id="description"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="image_path">Image Path:</label>
                        <input type="text" class="form-control" name="image_path" id="image_path">
                    </div>
                    <div class="form-group">
                        <label for="price">Price:</label>
                        <input type="text" class="form-control" name="price" id="price">
                    </div>
                    {{ csrf_field() }}
                    <button type="submit" class="btn custom-btn custom-fill-btn">Create</button>
                </form>

May
05
5 months ago
Activity icon

Started a new Conversation View Composer Issue Or Routing Issue?

I started learning Laravel a couple of days ago so be patient with me.

I have a footer partial (partials.footer) which I included in my master layout. I want to pass all categories links (Hot Beverages, Cold Beverages, Food etc.) in my footer so they can be accessed from all views. I created ViewServiceProvider in App\Providers and NavigationComposer file in App\Views\Composers.

ViewServiceProvider.php

<?php

namespace App\Providers;

use App\Category;
use App\Http\View\Composers\NavigationComposer;
use Illuminate\Support\ServiceProvider;


class ViewServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        View::composer('*', function ($view){
         $view->with('categories', \App\Category::all());
        });

    }
}

NavigationComposer.php

<?php
namespace App\Http\View\Composers;
use Illuminate\View\View;

class NavigationComposer {

    protected $categories;

    public function __construct(CategoryRepository $categories)
    {
        $this->categories = $categories;
    }

    public function compose(View $view){
        $view->with('categories', $this->categories->all());
    }
}

footer.blade.php

<div class="footer">
                    <p class="mb-4 text-uppercase font-weight-bold">Our Menu</p>
                    <ul class="list-group mb-4">
                    @foreach($categories as $category)
                        <li class="list-inline-item">
                            <a href="{{route('menu.category', ['category_id' => $category->id])}}">{{ $category->name }}</a>
                        </li>
                    @endforeach
                    </ul>
                </div>

On every view, the list with links loads just fine. The problem is when I click on a link I get this error:

Facade\Ignition\Exceptions\ViewException Undefined variable: categories (View: /home/user/PhpstormProjects/project-name/resources/views/partials/footer.blade.php) http://127.0.0.1:8000/category/1

And here's a route for this view.

Route::get('category/{category_id}', [
   'uses' => '[email protected]',
    'as' => 'menu.category'
]);