laravel how to get single row and post to view - php

in MySql database i have systemSetting table and this have any data.
in this table i have this fields :
id - site_copyright - date
i want to fill form with this single row.
{{ Form::model($siteSettings) }}
{{ Form::label('site_copyright' , 'copy right') }}
{{ Form::text('site_copyright', null , array('id'=>'site_copyright' )) }}
{{ Form::label('site_copyright' , 'copy right') }}
{{ Form::text('site_copyright', null , array('id'=>'site_copyright' )) }}
{{ Form::close() }}
UPDATE:
in CONTROLLER of that i have this:
public function getIndex()
{
$siteSettings = SystemSetting::all();
return View::make('back_end.layouts.manageHeaders')->with('siteSettings', $siteSettings);
}
in Result, form could not parse data to input fields.
i get error for this :
$siteSettings->site_copyright

This is a duplicate of many questions, such as this: Laravel display table record using query
The short answer is: use first() method to get a single entry.

You're using form model binding, so change your controller to:
public function getIndex()
{
$siteSettings = new SystemSetting;
return View::make('back_end.layouts.manageHeaders', compact('siteSettings'));
}

Query work on 5.1 or more
return DB::table('users')->where('id', $id)->value('field_name');
//if lower version of laravel like 4.2
DB::table('users')->where('id', $id)->pluck('field_name');

Related

Laravel 8 Property [id] does not exist on this collection instance [duplicate]

I am following Laracasts' videos: Basic Model/Controller/View Workflow.
I have a table holds contact information.
CREATE TABLE `about` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`content` text COLLATE utf8_unicode_ci,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I am trying to pass data to view using the following code in the controller file:
public function index()
{
$about = Page::where('page', 'about-me')->get(); //id = 3
return view('about', compact('about'));
}
When I try to show the code as shown below,
#section('title')
{{$about->title}}
#stop
#section('content')
{!! $about->content !!}
#stop
I get error that says:
Property [title] does not exist on this collection instance. (View: E:\laragon\www\newsite\resources\views\about.blade.php)
But if I change the retrieving method in the controller file, it works.
public function index()
{
$about = Page::find(3);
return view('about', compact('about'));
}
When I use dd($about) in the first case (where()->get()) the data is encapsulated by an array. In the second case (find(3)) it displays data as expected.
What am i doing wrong?
When you're using get() you get a collection. In this case you need to iterate over it to get properties:
#foreach ($collection as $object)
{{ $object->title }}
#endforeach
Or you could just get one of objects by it's index:
{{ $collection[0]->title }}
Or get first object from collection:
{{ $collection->first() }}
When you're using find() or first() you get an object, so you can get properties with simple:
{{ $object->title }}
With get() method you get a collection (all data that match the query), try to use first() instead, it return only one element, like this:
$about = Page::where('page', 'about-me')->first();
A person might get this while working with factory functions, so I can confirm this is valid syntax:
$user = factory(User::class, 1)->create()->first();
You might see the collection instance error if you do something like:
$user = factory(User::class, 1)->create()->id;
so change it to:
$user = factory(User::class, 1)->create()->first()->id;
$about = DB::where('page', 'about-me')->first();
in stead of get().
It works on my project. Thanks.
You Should Used Collection keyword in Controller.
Like Here..
public function ApiView(){
return User::collection(Profile::all());
}
Here, User is Resource Name and Profile is Model Name.
Thank You.
$about->first()->id or
$stm->first()->title and your problem is sorted out.
As result of $about = Page::where('page', 'about-me')->get() is a laravel collection, you can get a specific attribute like title using pluck() method which is described in https://laravel.com/docs/master/collections#method-pluck.
$titles = $about->pluck( 'title' )
#foreach($titles as $title)
{{ $title }}
#endforeach
The stored information in about valiable, is in collection format, so you just need to loop through it.
#if(count($about))
#foreach($about as $page)
{{$page->title}}
#endforeach
#endif

How to correctly update more than 1 data in database [duplicate]

I am following Laracasts' videos: Basic Model/Controller/View Workflow.
I have a table holds contact information.
CREATE TABLE `about` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`content` text COLLATE utf8_unicode_ci,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I am trying to pass data to view using the following code in the controller file:
public function index()
{
$about = Page::where('page', 'about-me')->get(); //id = 3
return view('about', compact('about'));
}
When I try to show the code as shown below,
#section('title')
{{$about->title}}
#stop
#section('content')
{!! $about->content !!}
#stop
I get error that says:
Property [title] does not exist on this collection instance. (View: E:\laragon\www\newsite\resources\views\about.blade.php)
But if I change the retrieving method in the controller file, it works.
public function index()
{
$about = Page::find(3);
return view('about', compact('about'));
}
When I use dd($about) in the first case (where()->get()) the data is encapsulated by an array. In the second case (find(3)) it displays data as expected.
What am i doing wrong?
When you're using get() you get a collection. In this case you need to iterate over it to get properties:
#foreach ($collection as $object)
{{ $object->title }}
#endforeach
Or you could just get one of objects by it's index:
{{ $collection[0]->title }}
Or get first object from collection:
{{ $collection->first() }}
When you're using find() or first() you get an object, so you can get properties with simple:
{{ $object->title }}
With get() method you get a collection (all data that match the query), try to use first() instead, it return only one element, like this:
$about = Page::where('page', 'about-me')->first();
A person might get this while working with factory functions, so I can confirm this is valid syntax:
$user = factory(User::class, 1)->create()->first();
You might see the collection instance error if you do something like:
$user = factory(User::class, 1)->create()->id;
so change it to:
$user = factory(User::class, 1)->create()->first()->id;
$about = DB::where('page', 'about-me')->first();
in stead of get().
It works on my project. Thanks.
You Should Used Collection keyword in Controller.
Like Here..
public function ApiView(){
return User::collection(Profile::all());
}
Here, User is Resource Name and Profile is Model Name.
Thank You.
$about->first()->id or
$stm->first()->title and your problem is sorted out.
As result of $about = Page::where('page', 'about-me')->get() is a laravel collection, you can get a specific attribute like title using pluck() method which is described in https://laravel.com/docs/master/collections#method-pluck.
$titles = $about->pluck( 'title' )
#foreach($titles as $title)
{{ $title }}
#endforeach
The stored information in about valiable, is in collection format, so you just need to loop through it.
#if(count($about))
#foreach($about as $page)
{{$page->title}}
#endforeach
#endif

Laravel(how to bring two columns from two separate tables in one .blade file) without touching the DB?

What is the most effective way for returning data from two tables in on view?
Like an employee check the vehicle for each order.
Route::get('orderVehicle',"adminController#orderVehicle");
public function orderVehicle(Request $reques){
$orders = new Order;
$vehicles = new Vehicle; $orders->id; $vehicles->id; return view('adminVeiw.orderVehicle',compact('orders','vehicles')); }
#foreach($orders as $or) {{ $or->id }} #endforeach {{ $vehicles->id }}
And the error is
"Trying to get property 'id' of non-object (View:
/var/www/html/full-Restaurant-App-Using-Laravel/resources/views/adminVeiw/orderVehicle.blade.php)"
So any suggestions?
do this if you don't wanna change your code in controller
#foreach($orders as $or)
#if(!empty($or->id))
{{ $or->id }}
#endif
#endforeach
#if(!empty($vahicles->id))
{{ $vehicles->id }}
#endif
in this case you won't get error but i don't know how you wanna this works for you,
i hope it helps
public function orderVehicle()
{
$orders = Order::create();
$vehicles = Vehicle::create();
return view('adminVeiw.orderVehicle', compact('orders', 'vehicles'));
}

Laravel: print URL or default value in blade view

I have Post model which has user function returning User model - creator of the post.
class Post extends Model
{
/**
* Get the user that owns the post.
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
In blade view I achieved how to print author name
{{ $post->user->name or 'Anonymous' }}
Above code works, but it is very hmm sensitive?
For example, I tried change this code to:
{{ $post->user->name, 'Anonymous' }}
<?php $post->user->name or 'Anonymous' ?>
Results? Trying to get property of non-object error on line with this code.
Maybe I am skipping something simple but important. How to print URL or default value in blade view. Pseudo code what I mean:
{{ '' or 'Anonymous' }}
Try
{{ isset($post->user->name) ? '' : 'Anonymous' }}
If this doesn't work (I haven't check it) try something like this:
#if (isset($post->user->name))
#else
Anonymous
#endif
Actually the error is not because the code is sensitive, it is because you are actually trying to access non objects values.
To achieve what you are looking for:
{{ isset($post->user)?'' : 'Anonymous' }}

How to manipulate an object in laravel

I have a laravel query as the one below:
$campaign = Campaign::with(array('tracks.flights' => function($q) use ($dates)
{
$q->whereRaw("flights.start_date BETWEEN '". $dates['start']."' AND '".$dates['end']."'")->orderBy('start_date')
->with('asset')
->with('comments');
}
))
->with('tracks.group')
->with('tracks.media')
->orderBy('created_at', 'desc')
->find($id);
I am very new to laravel and right now the response from this query returns all the data required including the comments with the comments attributes from the DB.
What i want to achieve is manipulate the comments object so it includes the user name from the users table as the comments table has the user_id only as an attribute.
How can I achieve this? I am very new to laravel.
Your Comment model must have a relationship with the User model, such as:
public function user()
{
return $this->belongsTo('App\User');
}
Now when you are iterating comments you are able to do $comment->user->name or in your case it will be something like this:
#if(!$campaign->tracks->flights->isEmpty())
#foreach($campaign->tracks->flights as $flight)
#if(!$flight->comments->isEmpty())
#foreach($flight->comments as $comment)
{!! $comment->user->name !!}
#endforeach
#endif
#endforeach
#endif
Once you can do this, next step is to understand eager load with eloquent.

Categories