Laravel 5 – Conditionally append variables

A few words before

I know that you can append variables to model arrays and json representations by using the protected $appends = ["your", "vars", "here"]; array. But imagine the following situation:

The situation

Our use case would be a fictional game or similiar:

Imagine that we have a User model that holds simple information about an (human) user, like the full name, address and so on.

Now, we also have a Faction model that represents the faction/origin/guild/… of this user.

The Faction model is eager-loaded when retrieving users, because the Faction name is wanted almost every time when displaying the user information.

A User also has DailyStatistics, which holds some information about their daily scores (simple points would be enough).

The Clue

Because I want to know the points of the a faction, which is the sum of the user points, I thought about appending a new variable totalPoints.

The getTotalPointsAttribute function would look like this:

function getTotalPointsAttribute(){
    return $this->users->sum->getTotalPoints();

The problem

Everytime when we retrieve a user now, the eager-loaded faction would also want to calculate the totalPoints attribute. That means, that we have a lot of overhead per user.

The question

Is there a way to avoid situations like this? Can I “conditionally” append variables? Are properties calculated when they are hidden?

I tried to wrap the totalPoints variable in a simple function, instead of an accessor instead. The problem is, that Frontend-Frameworks like VueJS would need access to the totalPoints variable (or to an endpoint to retrieve that value, but this solution is the least favorable).

Source: stackoverflow-php

change laravel 5.4 text mail content-type to text/plain

I am sending a small text mail with laravel:

public function build()
    return $this->text('emails.empty')->subject($this->msg)

The view empty.blade.php should only print the message:

{{ $msg }}

But neomutt receives text/html content:

[-- text/html wird nicht unterst├╝tzt ('v' benutzen, um diesen Teil anzuzeigen) --]

and thunderbird also shows text/html instead of plain text:

MIME-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

What can I do to get the mail so that neomutt does not complain?
I guess I need text/plain.

Source: stackoverflow-php

Laravel 5.4 make:auth logging out in refresh or view change

So I have a problem which is very confusing.

I make authentication with php artisan make:auth. And I only change the controller in the router and the redirect in the LoginController as the documentation write. Before it is working perfectly but now it has a big problem. After I refresh the page or go to an other view it’s loout all the time.

I red a lot of stackoverflow question and answer but those not helped me out as well.

Source: stackoverflow-php

found error in laravel 5.4, Trying to get property of non-object

In the admin area, I am trying to display the products in a listing page. Each product has category, color, size and type of the material
My tables structure

id   | category_name  
1    | Cat1  
2    | Cat2 

id   |  color_name  
1    |  Color1   
2    |  Color2

id   | material_type  
1    | material1  
2    | material2 

id   | size_name  
1    | size1  
2    | size2 

My product table

id  name  description category_id  color_id   material_id   size_id
1   pro1  product 1        1          1           1            1
2   pro2  product 2        2          2           2            2

Category Model

namespace App;

use IlluminateDatabaseEloquentModel;
use AppProduct;

class Category extends Model
    protected $table = 'categories';

    public function products(){
      return $this->hasOne(Product::class);

color, size and material Models have the same structure

namespace App;

use IlluminateDatabaseEloquentModel;
use AppProduct;

class Color extends Model
    protected $table = 'colors';
    public function products(){
      return $this->hasMany(Product::class);

In the product controller, I have the following index function to display the products

public function index()
      $products = Product::with('categories', 'colors' , 'sizes', 
      'materials', 'fantasias')->get();
      return view('backend.product.product-library', compact('products'));

I would like the output to be

name  description  category   color    material   size 
pro1  product 1      cat1    color1    mateial1   size1
pro2  product 2      cat2    color2    material2  size2

My View is

  @foreach( $products as $item )
          <td class="thumbnail" ><img src="{!! '/images/product-feature/'.$item->product_preview !!}" alt=""></td>
          <td><h3>{{ $item->product_name }}</h3></td>
          <td><p>{{ $item->product_description }}</p></td>
          <td><p>{{ $item->categories->category_name }}</p></td>
          <td><p>{{ $item->colors->color_name }}</p></td>
          <td><p>{{ $item->materials->color_name }}</p></td>
          <td><p>{{ $item->sizes->color_name }}</p></td>
          <td rowspan="2">
            <a class="btn transparent" href="{{ route('products.edit', ['product'=>$item->id]) }}">Edit</a>
            <form action="{{ route('products.destroy', ['product'=>$item->id]) }}" method="POST" enctype="multipart/form-data">
              <input name="_method" type="hidden" value="DELETE">
              <button type="submit" class="btn transparent">Delete</button>
              <input type="hidden" name="_token" value="{{Session::token()}}">

The following error appears

ErrorException in b5a959a09cc5b7e6abf71778d058119094681a2d.php line 28:
Trying to get property of non-object (View: /var/www/html/pet/resources/views/backend/product/product-library.blade.php)

Thank you so much for the help,

Source: stackoverflow-php

List all the users with the count of drivers they registered

i have tow models Userand Driver the user can register any number of drivers drivers

in drivers table i have agent_id, i want to list all the users with the count of the drivers they registered

in my Usercontroller i want to get the count of the drivers registered by the agents

public function index(Request $request) 
    $users = User::all();

    $registration_centers = RegistrationCenter::all();

    $drivers_registered = Driver::where('agent_id' , $agent_id)->count();  //something like this but i don't have agent_id to query with

    return view('agents', compact('users', 'registration_centers', drivers_registered));

thank you

Source: stackoverflow-php