Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vainway 's avatar

Property [name] does not exist on this collection instance.

i just can't find why my data in the graphics table doesn't show up

public function index () { // if(!empty($graphics)) { // $graphics = Graphic::get('amount'); // return view('post.graphics.thankyou', ['graphics' => $graphics]); // }

   if (Auth::check()) {

        $graphics = Graphic::all();
        return view('post.graphics.payment', ['graphics' => $graphics]);
    }
    else {
        return view('auth.login');
    }
}
0 likes
34 replies
Sergiu17's avatar

What do you have in your view?

{{ $graphics->name }} ?
vainway 's avatar

same that's what am using but it doesn't get it

vainway 's avatar

that works too i used it before but i just want to excute it a single data from the user logged in

vainway 's avatar

it look like this

class User extends Authenticatable implements MustVerifyEmail { use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'avatar', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

public function graphics(){
    return $this->hasMany('App\Graphic');
}

}

vainway 's avatar

this one is on graphic model

class Graphic extends Model { protected $fillable = [ 'name', 'time', 'description', 'file', 'file_title', 'amount', 'amount_file', 'user_id' ];

public function user(){
    return $this->belongsTo('App\User');
}

}

vainway 's avatar

what do u see about my models i target create data and return another page with its userid or id of data created

dezineHQ's avatar

please be more specific.

Do you want to display just the users graphics or all graphics

dezineHQ's avatar

when you use

$graphics = Graphic::all();

it will return a collection of all graphics. You need to iterate over a collection with a forloop.

to get just the users graphics try,

@foreach(auth()->user()->graphics as $graphic)
	
	{{ $graphic->name }}

@endforeach

dezineHQ's avatar

You will need to pass through the $id to the method, Find the graphic by $id and pass it to view

public function show($id) 
{

	return view('guavahire.graphics.payment')->with([

		'graphic' = Graphic::findOrFail($id)

	]);
}

Then in view you can just call

	
{{ $graphic->name }}

vainway 's avatar

which other function can i use to execute something without looping them all u have in database

@foreach(auth()->user()->graphics as $graphic) {{ $graphic->amount }} @endforeach

vainway 's avatar

@tashari thanks it worked but i have another problem on that : first of all how it is on my project

a person create a post and then it redirects him to on payments page but when it get on payment page it show an error {{ $graphic->name }} that Undefined variable: graphics (View: C:\xampp\htdocs\laravelpro\resources\views\post\graphics\payment.blade.php)

but when i go on the browser url and write /graphics.payment/5 it works and show what i want

so, you get that a user can't know that he will use that way i use in the url browser

dezineHQ's avatar

are you passing the graphic Id to the payment page

show me the method where it is redirected to the payment page

vainway 's avatar

the creating is made but after while is redirected it show that error see my redirects here


public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|min:3',
            'time' => 'required|min:3',
            'description' => 'required|min:10',
            'file' => 'required|file|max:2048'
        ]);
        if (Auth::check()) {
        $upload = $request->file('file');
        $path = $upload->store('public/storage/graphics');
        $file = Graphic::create([
            'name' => $request->name,
            'time' => $request->time,
            'description' => $request->description,
            'file_title' => $upload->getClientOriginalName(),
            'file' => $path,
            'user_id' => Auth::user()->id
        ]);
            return view('post.graphics.payment');
        }
        
    }

Snapey's avatar

Please format your code by putting 3 backticks ``` on a line before and after each code block

vainway 's avatar

@tashari in the view i use only this

<button type="submit" class="btn btn-primary ">{{ $graphics->name}}</button>

Snapey's avatar
 $graphics = Graphic::all();

$graphics is a collection. It cannot have a name.

One of the models inside this collection might have a name.

If you are showing your button inside a loop, then perhaps you meant

<button type="submit" class="btn btn-primary ">{{ $graphic->name}}</button>
vainway 's avatar

@snapey the problem is when i create the post and while redirecting to payments it show me

Undefined variable: graphics (View: C:\xampp\htdocs\laravelpro\resources\views\graphics\payment.blade.php)

but when i type in the url and put on the id i want it works

it doesn't reopen well i go on it directly

Snapey's avatar

Do you show your payments page?

vainway 's avatar

this is the payment page

<form action="{{ route('send') }}" method="POST" id="payment-form">
                @csrf
                <div class="row">
                    <div class="col-md-12">
                    <div class="form-group">
                        <label class="bmd-label-floating">Name</label>
                        <input id="name" type="text" class="form-control" placeholder="" value="{{ Auth::user()->name }}" required="" autocomplete="name">
                    </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6">
                    <div class="form-group">
                        <label class="bmd-label-floating">Email</label>
                        <input id="email" type="email" class="form-control" placeholder="" value="{{ Auth::user()->email }}" required="" autocomplete="email">
                    </div>
                    </div>
                    <div class="col-md-6">
                    <div class="form-group">
                        <label class="bmd-label-floating">Phone</label>
                        <input id="phone" type="number" class="form-control" required="" autocomplete="tel">
                    </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                    <div class="form-group">
                        <label class="bmd-label-floating">Card</label>
                        <div id="card-element" class="form-control">
                          <!-- A Stripe Element will be inserted here. -->
                        </div>
                    </div>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Payment {{ $graphics->name }}</button>
                </form>
Snapey's avatar

so you see there at the bottom, you ask for $graphics->name but you did not pass $graphics to the view

return view('guavahire.graphics.payment');

But what you should do is NEVER return a view from a POST route . You should always redirect to a GET route which then provides the view (after getting all the data it needs)

dezineHQ's avatar
Use this in the bottom of the storw method. 


return redirect()->action('GraphicController@show',['id' => $file-id]);


Then in show method

public function show($id) 
{

    Graphic::findOrFail($id);

      return view('guavahire.graphics.payment',compact('graphic');
}



dezineHQ's avatar

Apologies for formatting as this was posted from my phone

Snapey's avatar

You still need to create a GET route for that action.

vainway 's avatar

i changed up in my store but next it show me another error

which is this

Property [name] does not exist on the Eloquent builder instance

public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|min:3',
            'time' => 'required|min:3',
            'description' => 'required|min:10',
            'file' => 'required|file|max:2048'
        ]);
        if (Auth::check()) {
        $upload = $request->file('file');
        $path = $upload->store('public/storage/graphics');
        $file = Graphic::create([
            'name' => $request->name,
            'time' => $request->time,
            'description' => $request->description,
            'file_title' => $upload->getClientOriginalName(),
            'file' => $path,
            'user_id' => Auth::user()->id
        ]);
            $graphics = Graphic::where('id', $request->id);
            return view('post.graphics.payment', ['graphics' => $graphics]);
        }
        
    }

and also in my route i use get

Route::resource('graphics', 'GraphicsController');

Route::get('/graphics.payment/{id}', 'Payment/GraphicsController@index')->name('payment');

this index for payment

public function index ($id)
    {
        $graphics = Graphic::findOrFail($id);
        return view('post.graphics.payment')->with([

            'graphics' => $graphics
    
        ]);
 }

this is the models

public function user(){
        return $this->belongsTo('App\User');
 }
in the user model is this one

public function graphics(){
        return $this->hasMany('App\Graphic');
 }

that's all i have

dezineHQ's avatar
public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|min:3',
            'time' => 'required|min:3',
            'description' => 'required|min:10',
            'file' => 'required|file|max:2048'
        ]);

        if (Auth::check()) {
             $upload = $request->file('file');
             $path = $upload->store('public/storage/graphics');

            $file = Graphic::create([
                 'name' => $request->name,
                 'time' => $request->time,
                 'description' => $request->description,
                 'file_title' => $upload->getClientOriginalName(),
                 'file' => $path,
                 'user_id' => Auth::user()->id
             ]);

     
            return redirect('Payment/GraphicController@index',['id' => $file->id]);

		// you can grab the id from the newly created resource - $file
        }

	//if auth::check() fails then what
        
    }



Route::get('/graphics.payment/{id}', 'Payment/GraphicsController@index')->name('payment');


public function index ($id)
    {
             $graphics = Graphic::findOrFail($id);
        
        return view('guavahire.graphics.payment')->with([

            'graphics' => $graphics
    
        ]);
    }
  

vainway 's avatar

i change that but still give me errors

Argument 2 passed to Symfony\Component\HttpFoundation\RedirectResponse::__construct() must be of the type int, array given, called in

Snapey's avatar

change

return redirect('Payment/GraphicController@index',['id' => $file->id]);

to

return redirect(route('payment',['id' => $file->id]));
Next

Please or to participate in this conversation.