I am now working with my "Update function".
public function edit($id)
{
$product = Inventory::find($id);
return view('/update',compact('product')) ;
}
This is my view button in my blade
<a href="{{ route('edit',$prod->id) }}" class="btn btn-raised btn-primary btn-sm">
view
</a>
It works when I redirect it to my edit blade
but when I changed the $id to $prod_num
public function edit($prod_num)
{
$product = Inventory::find($prod_num);
return view('/update',compact('product')) ;
}
my $id is bigInt and primary key(AU) while $prod_num is a string.
how is gonna work if I want to look for the prod_id
Thank you
The find() method will looks in the primary key of your model. If you want to get a record by another attribute, you can use where() and first():
$product = Inventory::where('prod_num', $prod_num)->first();
Related
**after clicking "read more" i want to show a single post**
<a href="{{ URL::to('single/blog/'.$post->id) }}" class="btn btn-primary
float-right">Read More →</a>
** **the route is****
Route::get('single/blog/{id}','Web\Site\HomeController#show');
**the controller is**
public function show($id)
{
$posts = Post::findOrFail($id);
return view('site.home.singleblog',compact('posts'));
}
****the single section is****
#foreach($posts as $post)
<img class="img-responsive" src="{{asset("uploads/posts/$post->id/image/$post->image") }}" alt=""
{{ $post->name }}
{{$post->description}}
#endforeach
Your variable $posts is a single Post instance from Post::findOrFail($id) (where $id is coming from a route parameter, so a single value). You don't want to be iterating an instance of a Model. Use it in your view like a single model instance not a collection.
public function show($id)
{
view('site.home.singleblog', [
'post' => Post::findOrFail($id),
]);
}
Then in the view just remove the #foreach and #endforeach.
Try this:
{{ $post['name'] }} {{$post['description']}}
If you fetched $posts successfully, it should work. The error says $post is not an object but you are using object syntax to get value by key. So use array syntax.
I created a model relationship between User and Message. I want to implement a list of messages for the authenticated user but I get the following error.
Method Illuminate\Database\Eloquent\Collection::links does not exist
Controller
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('message.index')->with('messages', $user->message);
}
Message provider
class message extends Model
{
public function user() {
return $this->belongsTo('App\User');
}
}
User provider
public function message ()
{
return $this->hasMany('App\Message');
}
index.blade.php
#extends('layouts.app')
#section('content')
<h1>messages</h1>
#if(count($messages)>0)
#foreach ($messages as $message)
<div class="well">
<h3>{{$message->user}}</h3>
<small>Written on {{$message->created_at}} </small>
</div>
#endforeach
{{$messages->links()}}
#else
<p> no post found </p>
#endif
#endsection
Error
"Method Illuminate\Database\Eloquent\Collection::links does not exist.(View: C:\xampp\htdocs\basicwebsite\resources\views\message\index.blade.php)"
Check your view blade, that method (links()) only could be used when your data model is implementing paginate() method.
If you dont use paginate(), remove this part:
{{$messages->links() }}
If you are trying to paginate your data when it gets to the view then you need to add the paginate in your controller before passing the data to the view. Example
return $users = Users::select('id','name')->paginate(10);
with that paginate method in your controller, you can call the links method to paginate your object in view as shown below
{{$users->links()}}
hope it helps you
There are 2 ways to resolve this issue:
Either use paginate function while searching data from database:
$users = DB::table('users')->where('id',$user_id)->paginate(1);
Remove links() function from index.blade.php
{{ $messages->links() }}
Remove {{ $messages->links() }} to in your index.blade.php because {{ $messages->links() }} is supported only when you use paginate
You can do something like this in your controller file.
public function index()
{
$messages = Message::all()->paginate(5);
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('message.index')->with('messages', $messages, $user->message);
}
my laravel model funciton :
public function isAdminOrSuperAdmin()
{
return $this->role() == config('custom_config.constants.user_types.SUPER_ADMIN')
|| $this->role() == config('custom_config.constants.user_types.ADMIN');
}
i try to access in view :
#if($user->isAdminOrSuperAdmin())
<a class="btn btn-primary pull-right" style="margin-top:
-10px;margin-bottom: 5px" href="{!! route('admin.users.create') !!}">
Add New
</a>
#endif
but it show error:
Method Illuminate\Database\Eloquent\Collection::isAdminOrSuperAdmin does not exist. (View:/resources/views/admin/users/index.blade.php)
thanks in advance.
Check the error:
Method Illuminate\Database\Eloquent\Collection::isAdminOrSuperAdmin does not exist. (View:/resources/views/admin/users/index.blade.php)
This means, you are trying to call a method of your model on a Collection instance instead of an actual User model instance.
When querying several items from your database, Laravel returns an instance of the Collection class that contains all the resulting model objects.
Maybe you are doing something like this:
public function aCoolFunction()
{
$user = User::where('column', 'value')
->get(); // <-----
return view('my_view')->with('user', $user);
}
The get() method returns a Collection, not a single element.
Try the first() instead:
public function aCoolFunction()
{
$user = User::where('column', 'value')
->first(); // <-----
return view('my_view')->with('user', $user);
}
Now in your view the $user variable will actually hold and instance of your User model user in which the isAdminOrSuperAdmin() method is defined, and not a collection of it.
I don't think this is the best way but you can pass the function to view using your controller :
in your controller :
public function index(User $user)
{
$ModelFunction = $user->yourModelFunction();
return View('test',compact('user','ModelFunction'));
}
And in your View :
{{ $ModelFunction }}
you must call function like this:
#if(is_admin_or_super_admin())
I'm looking to fetch unique values from a table in my database and pass the company name into my view as options in a drop down list within a form.
The function in my controller looks like this:
public function index()
{
$id = Auth::user()->id;
$admin = Admin::find($id);
$companies = DB::table('vacancies')->distinct()->select('company')->get()->toArray();
return view('admin')->with('admin',$admin)->with('companies',$companies);
}
And my view looks like this:
<div class="form-group">
#foreach ($companies as $company)
{{ Form::label('Select Company')}}
{{ Form::select('companies', $company->company, ['class'=>'form-control', 'placeholder'=>'Please select ...']) }}
#endforeach
</div>
I'm getting the following error:
Invalid argument supplied for foreach()
If I try {{dd($companies}} I can see an array is being passed through to the view OK. This array looks like this:
array:353[
0 => {#274 ▼
+"company": "Example company name"
}......
]
Ditching the loop and reverting back to:
<div class="form-group">
{{ Form::label('Select Company')}}
{{ Form::select('companies', $companies, ['class'=>'form-control', 'placeholder'=>'Please select ...']) }}
</div>
causes another error to be thrown which states:
htmlspecialchars() expects parameter 1 to be string, object given
Where am I going wrong here?
In your controller
public function index()
{
$id = Auth::user()->id;
$admin = Admin::find($id);
$companies = DB::table('vacancies')->distinct()->pluck('company')->toArray();
return view('admin')->with('admin',$admin)->with('companies',$companies);
}
Fllow as per below
your index function must be like below
public function index()
{
$id = Auth::user()->id;
$admin = Admin::find($id);
$companies = DB::table('vacancies')->distinct()->pluck('company')->toArray();
return view('admin')->with('admin',$admin)->with('companies',$companies);
}
your view must me like below
<div class="form-group">
{{ Form::label('Select Company')}}
{{ Form::select('companies', $companies, ['class'=>'form-control', 'placeholder'=>'Please select ...']) }}
</div>
let me know if any
Let me show you what I have.
//Post Controller
public function showPost(Post $post)
{
$albums = $post->album()->with('photos')->get();
$comments = $post->comments()->where('approved', '=', 1)->get();
$this->layout->title = $post->title;
$this->layout->main = View::make('home')->nest('content','posts.single', compact('post', 'comments','albums'));
}
mysql sentence executed correctly
string 'select * from albums where albums.id = '{"id":19,"title":"Post no 19","read_more":"Lorem i'... (length=786)
string 'select * from albums where albums.post_id = '19'' (length=54)
string 'select * from images where images.album_id in ('4')' (length=57)
I want to access the Photo object in blade, everything parses to the template but when I try to get the Photos
#foreach($albums->Photos as $photo)
Undefined property: Illuminate\Database\Eloquent\Collection::$Photos (View:
// post model
public function album()
{
return $this->hasOne('Album');
}
//album model
public function Photos(){
return $this->hasMany('Image');
}
Try
#foreach($albums->Photos()->get() as $photo)
{{$photo->attribute}}
#endforeach
You need to call the function that holds the relationship then use
get()
method to return an Eloquent Collection and then iterate it with a foreach loop.
#foreach($albums as $album)
#foreach($album->Photos as $photo)
<p>{{ $photo->image }}</p>
#endforeach
#endforeach
THats the answer