Laravel component is not receiving variables, no matter what i do - php

First, i have the component file, located at resources/views/component.
game-card.blade.php
#props(['game'])
<div class = 'games'>
<a href = 'game/{{$game->id}}'> view page </a>
<p> game: {{$game->name}} </p> <br>
<p> genre: {{$game->genre}} </p> <br>
</div>
Then this component is called at my view, located in resources/views
listing.blade.php
#extends('layout')
#section('list')
<div class = 'listContainer'>
#unless(count($games) === 0)
#foreach($games as $game)
//doesn't work
<x-game-card :game = "$game"/>
#endforeach
#else
<p> 0 Games </p>
#endunless
</div>
#endsection
The variable $game is not passed in the component <x-game-card/>, i even tried to use short atribute syntax (<x-game-card :$game/>) but it still doesn't work.
If it matters, the file listing.blade.php is yielded at the file layout.blade.php, located in the same folder.
layout.blade.php
<body>
<!-- Header -->
#yield('list')
#yield('singlegame')
#include('partials._footer')

For any prop that you want to pass to your component, you need to register it on the component class. In this case, the class is probably app/View/Components/GameCard.php
On the class, you need to do something like:
class GameCard extends Component
{
public $game;
public function __construct($game)
{
$this->game = $game;
}
Source: https://laravel.com/docs/9.x/blade#passing-data-to-components

i found the root of the problem. I just can't believe that the solution is so simple. I am going to post it here so less people make the same mistake i did
First, create the class (i personally use the command php artisan make:component to do that) and update it just like Nico did.
Second, when you put the <x-component in the HTML, MAKE SURE TO NOT LEAVE ANY SPACES IN THE VARIABLE!!!
my mistake was using <x-game-card :game = "$game"/>
instead of <x-game-card :game="$game"/>

Related

On what basics we give url and name in laravel route file

You may find me stupid but i am unable to understand on what basics we give url and name in our route file
Example:
Route::get('/order/getOrders', 'OrderController#getOrders')-
>name('order.getOrders')->middleware('auth');
can anyone please tell me.
and if we take url on the basics of where our file in view folder like( order->getorder blade file)
Then what if my path is layouts.site.topbar
In view instead of pages, my file is in layouts.
EDIT:
blade file
<a href="{{ route('sync.index') }}">
#if(isset($syncs))
#foreach ($syncs as $sync)
#endforeach
{{ $sync->session_date }}
#endif
</a>
controller file
class TopbarController extends Controller
{
public function index()
{ die('o');
$syncNames = Sync::select('session_date','session_time')->where('user_id',$user_id)->get();
return view('layouts.site.topbar', array(
'syncs' =>$syncNames
));
}
public function sync_finish_session() {
die('s');
$user_id = Auth::id();
$sync_date = date('M d ',strtotime("now"));
$sync_time = date('M d, Y H:i:s',strtotime("now"));
$sync = Sync::where('user_id',$user_id)->get();
if(count( $sync) > 0) {
Sync::where('user_id',$user_id)->update(['session_date'=>$sync_date,'session_time'=>$sync_time,'user_id'=>$user_id]);
}
else {
$dates = new Sync();
$dates->session_date = $sync_date;
$dates->session_time = $sync_time;
$dates->user_id = $user_id;
$dates->save();
}
return $sync;
}
}
web file
Route::post('/sync_finish_session', 'TopbarController#sync_finish_session')->name('sync_finish_session')->middleware('auth');
Route::get('/sync/index', 'TopbarController#index')->name('sync.index')->middleware('auth');
Now whats the problem its giving nothing even i put die but its not going in controller file.
I think this is more a personal preference thing than that there are rules.
The convention I use is name(<model>.<action>)
This way i can create routes like
Route::get('/users/{id}/view', 'UserController#view')->name('users.specific.view')->middleware('auth');
You just name route as you do want. There is no strict rules how to name route. You can change name('order.getOrders') to name("anyName") and use new name in templates.
As the Laravel documentaton about rounting says:
Named routes allow the convenient generation of URLs or redirects for specific routes.
So, you can use this name to generate URLs or redirects. For example:
You could put this in your web.php file:
Route::get('/image/index', 'API\SettingsController#index')->name('image.index');
And call that route like this in your view:
Le met see that index!
Where the {{ route('image.index') }} references the name you gave to it.
You can name your route(s) anything you want. If you wanted, you could call your above route "mySuperCoolRouteName":
Route::get('/order/getOrders', 'OrderController#getOrders')-
>name('mySuperCoolRouteName')->middleware('auth');
and later in a view file you can use this name as a "shorthand" to get/print the URL of that route:
To My Cool Route
will be rendered to
To My Cool Route

Load data in view in Laravel

I have a simple controller function that fetch all records from db. but when i am trying to show all these records it show nothing. In fact it shows me hard coded foreach loop like this.
#foreach ($compactData as $value) {{ $value->Name }} #endforeach
this is my contoller function.
public function showallProducts()
{
$productstock = Product::all()->stocks;
$productoldprice = Product::all()->OldPrices;
$productcurrentprice = Product::all()->CurrentPrice;
$compactData=array('productstock', 'productoldprice', 'productcurrentprice');
return view('welcome', compact($compactData));
}
this is my view
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Laravel
</div>
<div class="title m-b-md">
All products
</div>
<table>
<tbody>
#foreach ($compactData as $value)
{{ $value->Name }}
#endforeach
</tbody>
</table>
</div>
</div>
</body>
why it is behaving like this. any solution?? I am using phpstorm version 17. Is their any setting issue to run project because what ever project I ran it gives me the only page which i ran with only html?
My route is.
Route::get('/', function () {
$action = 'showallProducts';
return App::make('ProductController')->$action();
});
Have you checked your $compactData variable? Please dd($compactData) to see what it contains.
Problem 1
You are accessing a relational property as a property of Eloquent collection, like this:
Product::all()->stocks
which is not correct. Because the Collection object doesn't have the property stocks but yes the Product object might have a stocks property. Please read the Laravel documentation about Collection.
Problem 2
$compactData = array('productstock', 'productoldprice', 'productcurrentprice');
This line creating an array of 4 string, plain string not variable. So, your $compactData is containing an array of 4 string. If you want to have a variable with associative array then you need to do the following:
$compactData = compact('productstock', 'productoldprice', 'productcurrentprice');
Problem 3
return view('welcome', compact($compactData));
Here you are trying to pass the $compactDate to the welcome view but unfortunately compact() function doesn't accept variable but the string name of that variable as I have written in Problem 2. So, it should be:
return view('welcome', compact('compactData'));
Problem 4
Finally, in the blade you are accessing each element of the $compactData data variable and print them as string which might be an object.
You most likely have a problem with your web server.
Try to use Laravel Valet as development environnement.
Edit : I found this : Valet for Windows
I think you didn't mention the blade in the name of the view file by which it is saved. So change the name of the file by which it is save to something like:
filename.blade.php
and try again.
Explanation:
#foreach ($compactData as $value) this is the syntax of blade template engine, and to parse and excute it, you have to mention the blade extension in the name.

Laravel get div without ajax

Hi I am trying to get the content within a div element that also happens to be within a form into my controller. I dont want to use ajax. How may I get that done ?
<div id="editorcontents" name="editorcontents">
</div>
Then in controller
Use Input;
$content = Input::get('editorcontents');
In your controller, do something like this. Look up the correct way in the docs (https://laravel.com/docs/5.4/eloquent#retrieving-models). For example, if you want ALL input, you would do Input::all();, instead of Input::where('editorcontents')->get();
public function index() {
$content = Input::where('editorcontents')->get();
return view('your_view.blade.file', compact('content'));
}
Then in your view your would now have $content, that you passed from your controller.
start of by looking at it, add this at top of your view: {{ dd($content) }}. This will die dump $content.
Go ahead and remove that line and do something like (docs here https://laravel.com/docs/5.4/blade#loops):
<div id="editorcontents" name="editorcontents">
#forelse ($content as $value)
<li>{{ $value->body }}</li>
#empty
<p>No content</p>
#endforelse
</div>

Laravel 4: Using flexible layouts within routes

I have an application without controllers and read about controller layouts in laravel 4 documentation and this other article too, but I don't know where to start for implement it within routes (version 4), how can I do that?
Error received: InvalidArgumentException, View [master] not found.
app/routes.php
<?php
View::name('layouts.master', 'layout');
$layout = View::of('layout');
Route::get('users/create', array('as' => 'users.create', function() use($layout) {
//#TODO: load view using 'layouts.master',
// desirable: append 'users.create' and 'users.menu' views to sidebar and content sections.
//return View::make('users.create');
return $layout->nest('content', 'master');
}));
?>
app/views/layouts/master.blade.php
<html>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
app/views/users/create.blade.php
{{ Form::open() }}
{{ Form::text('name') }}
{{ Form::submit('submit') }}
{{ Form::close() }}
app/views/users/menu.blade.php
<!-- This is appended to the master sidebar -->
<p>Create user</p>
Update: I modified example code to clarify what I want to do. Check app/routes.php and its comments
The code in your routes file is trying to nest the master layout within itself, which isn't really what you want. You're getting the error because 'master' would look for app/views/master.blade.php. That's easily fixed by changing it to 'layouts.master', but I wouldn't like to think what might happen...
The root cause of the issue you're having is the difference between "yielding" views from a Blade template, and nesting them from a route. When you nest a route, you need to echo it rather than using the #yield tag.
// File: app/routes.php
View::name('layouts.master', 'layout');
$layout = View::of('layout');
Route::get('users/create', array('as' => 'users.create', function() use ($layout)
{
return $layout
->nest('content', 'users.create')
->nest('sidebar', 'users.menu');
}));
/*
|--------------------------------------------------------------------------
| View Composer
|--------------------------------------------------------------------------
|
| Code in this method will be applied to all views that use the master
| layout. We use that to our advantage by injecting an "empty" sidebar
| when none is set when returning the view. It will error otherwise.
|
*/
View::composer('layouts.master', function($view)
{
if (!array_key_exists('sidebar', $view->getData()))
{
$view->with('sidebar', '');
}
});
// File: app/views/layouts/master.blade.php
<html>
<body>
#section('sidebar')
This is the master sidebar
{{ $sidebar }}
#show
<div class="container">
{{ $content }}
</div>
</body>
</html>
Laravel's View composers are a powerful tool. If you have any data (eg logged-in user info) used by all views that share the same template(s), you can use the composers to save injecting the data every time you load the view.
You could also use the #parent tag to append content, assuming you;re using blade for templating. E.g. (in the view)
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#stop
You don't need to use nesting views if you're using blade.
app/views/users/create.blade.php
You need to extend the master.blade
#extends('layouts.master')
#section('content')
// form stuff here
#stop
Now, all you need to do is call create.blade
return View::make('users.create')
Just throwing this out there as a possible solution using controller routing (whereas you can set the template from within the controller).
app/routes.php
Route::controller('something', 'SomethingController');
app/controllers/SomethingController.php
class SomethingController extends BaseController {
protected $layout = "templates.main"; // denotes views/templates/main.blade.php
public function getIndex() { // the "landing" page for "/something" or "/something/index"
$this->layout->content = View::make('something.index')->with("myVar", "Hello, world!"); // load in views/something/index.blade.php INTO main.blade.php
}
public function getTest() { // for "/something/test"
$this->layout->content = View::make('something.index')->nest("widget", "something.widget", array("myVar" => "Hello, World!"));
}
}
app/views/templates/main.blade.php
#include('templates.partials.header')
#yield('something')
#yield('content')
#include('templates.partials.footer')
app/views/something/widget.blade.php
I'm a widget. {{ $myVar }}
app/views/something/index.blade.php
#section('something')
I will go in the 'something' yield in main.blade.php
#stop
#section('content')
I will go in the 'content' yield in main.blade.php.
{{ $myVar }}
{{ $widget }}
#stop
?>
Now you can test http://myserver/something and http://myserver/something/test to see the differences. Note: not tested but as a rough example.

Codeigniter - forms

I'm new to CodeIgniter and try to understand how to create forms. I searched both on the Net and stackopverflow but did not reach anything.
What i want is to create forms with helpers.
In order to do that in my controller create a function, named formElements() and the code is
public function formElements()
{
$this->load->helper(array('form'));
}
and in kayit.php
i try to create some html elements
<?=form_open('kayit/formElements')?>
<?=form_fieldset('Login Form')?>
<div class="textfield">
<?=form_label('username', 'user_name')?>
<?=form_input('user_name')?>
</div>
<div class="textfield">
<?=form_label('password', 'user_pass')?>
<?=form_password('user_pass')?>
</div>
<div class="buttons">
<?=form_submit('login', 'Login')?>
</div>
<?=form_fieldset_close()?>
<?=form_close();?>
However i take the error : Fatal error: Call to undefined function form_open() in C:\xampp\htdocs\pasaj\application\views\kayit.php on line 220
why?
You can also include the form helper in ./application/config/autoload.php file
$autoload['helper'] = array('form');
You can include the form helper in your Controller's constructor function, like:
$this->load->helper('form');
And it should work fine in view.
Ref: Form Helper
Hope it helps

Categories