This is a weird error. I can add a new record to the database fine with a new UserEdit method, but if I try to update a record. Nothing. It doesn't even file a blank value, or issue an error.
What am I missing? To try to eliminate issues, I tried running the core update method in the documentation like so:
protected function create()
{
$save = UserEdit::find(715);
$save->First_Name = 'Jeffrey';
$save->Last_Name = 'Way';
$save->save();
$id = UserEdit::find(715)->toArray();
return view('NewUser', compact('id'));
//return $array;
}
This problem is the same as my earlier question here, but I didn't think it appropriate to double up the questions and this is technically a different problem. My blade just prints an array of the user data at the moment so I know if it's working or not:
#extends('layout')
#section('content')
<h1> Add Your Information {{ $id['name'] }}</h1>
<div class="col-md-6">
#foreach ($id as $key=>$value)
{{ $value }}<br>
#endforeach
</div>
#stop
The Routes:
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index');
Route::get('NewUser', 'UserEntryController#create');
Route::post('NewUser', 'UserEntryController#UserForm');
Route::post('NewUser', 'UserEntryController#UserForm')->name('submit');
I'm assuming I'm missing some crucial minor detail, but for the life of me, can't find what. Anybody got an idea where to look?
public function store(Request $request)
{
$user = new UserEdit;
//$user->name = $request->name;
$user->First_Name = "foo";
$user->Last_Name = "bar";
$user->save();
return $user;
}
Try this function, maybe you had a mistake in your code.
Related
I have a question regarding showing data from my database in Laravel.
I get the following error:
Trying to get property 'first_name' of non-object
It refers to this line of code:
#foreach ($contact as $c)
<h1 class="display-4">Bekijk details voor contact: {{ $c->first_name }} {{ $c->last_name }}</h1>
#endforeach
I get this data from my database by using Laravel's 'show' function as described below:
public function show($id)
{
$contact = Contact::find($id);
return view('contacts.show', compact('contact'));
}
My routing looks like this:
Route::resource('contacts', 'ContactController');
The reason I can't get my head wrapped around this error is because it seems to work just fine for other functions like Laravel's 'edit' function as described below:
public function edit($id)
{
$contact = Contact::find($id);
return view('contacts.edit', compact('contact'));
}
Any help would be appreciated, I would like to know why it is not working for my 'show' function whilst it is working for my 'edit' function, are there any differences I am not aware of?
Thanks in advance!
Kind regards,
Geert-Jan Knapen
Very likely, $contact is the object rather than collection, so you do not need to loop through it. you can access it directly.
<h1 class="display-4">Bekijk details voor contact: {{ $contact ->first_name }} {{ $contact ->last_name }}</h1>
Update it's better to use route model binding, so it handles if the contact does not exist.
public function show(Contact $contact)
{
return view('contacts.show', compact('contact'));
}
How to pass id from veiw into controller in then into another view again??
First of all this a basic question you should be able to search it anywhere on the internet. any how below is the solution.
web/routes.php:
Route::get('user/{id}','USerController#find')->name('user.get');
Or by passing user object in the route:
Route::get('user/{user}','USerController#find')->name('user.get');
UserController:
the below function accepts user object as a parameter in the route we defined above.
public function find(user $user)
{
return view('user.detail',compact('user'))
}
or
public function find($id)
{
$user = USer::find($id);
return view('user.detail',compact('user'))
}
resources/view/user/detail.blade.php:
{{ $user -> name }}
{{ $user -> email }}
and in order visit the route user.find use the below line:
View Detail
No you can use this code as reference to your project.
first solution is .
change route
Route::get('/show_vedio/{$id}', 'VedioController#show')->name(show_videos);
and use this {{ route('show_vedios', $vedio->id )}} in href
it's not entirely clear what you are trying to do, maybe i will help you:
public function show(int $id): View
{
$video = Video::find($id);
return view('video.show', ['video' => $video]);
}
I have a function where:
After I click the link named "hapus", the related data will be deleted and I want to have a popup alert to show that the data has been deleted.
*sorry for my bad english
*hapus technically means destroy
this the code:
public function hapus(Request $request, $id)
{
DB::table('kelompok')
->where('id', $id)
->delete();
return redirect()->back();
}
Use with() in your controller
function hapus(Request $request, $id)
{
DB::table('kelompok')
->where('id', $id)
->delete();
return redirect()->back()->with('alert', 'Deleted!');
}
In your blade template, retrieve the session after being redirected from controller:
#if (session('alert'))
<div class="alert alert-success">
{{ session('alert') }}
</div>
#endif
Extending #pbwned's answer,
you can also use javascript alert box in your blade view to display the flash session/message.
For example:
<script>
var msg = '{{Session::get('alert')}}';
var exist = '{{Session::has('alert')}}';
if(exist){
alert(msg);
}
</script>
Hope it helps =)
I am new in laravel and php. I am developing a magazine site with laravel 4. I want to store home page in laravel cache . But In my homepage , there are many queries . Please see details below.:
My HomepageController index method:
public function index()
{
return View::make('homepage');
}
My Query Helper Function which is used for post by category in my Homepage:
public static function cat_post($category, $limit)
{
$posts = Post::whereHas('categories', function($q) use ($category)
{
$q->where('category_slug', 'like', $category);
})->with('categories')->take($limit)->orderBy('created_at', 'DESC')->get();
return $posts;
}
In My homepage.blade.php , I used this helper function many times. like below:
<?php $national = Helper::cat_post('national', 3); ?>
#foreach ($national as $post)
{{ Helper::img_src($post) }}
<h4>{{ $post->title }}</h4>
</div>
#endforeach
Now i want to put homepage in cache and when new post created, then delete old homepage from cache and store new one.
Please help me. Is it possible ?????
To store a value in cache you can use Laravel's Cache:
public function index()
{
return Cache::rememberForever('homepageCache', function()
{
return View::make('homepage');
});
}
This snipped tries to retrieve a cached version of View::make('homepage') and otherwise creates it.
Every time a post is created, updated or deleted you need to invalide your homepageCache:
Cache::forget('homepageCache');
I'm sort of a newb to Laravel 4 and Sentry 2, but i've managed to survive so far. I'm running into a problem right now, cause when im logged in as userid(1) and i want to view the profile of userid(2) i'm just seeing the information of userid(1) on the profile of userid(2).
I'm aware that using filters might come in handy at this, but if i have to be honest. I have no idea what i should look at etc.
I know this site is not meant for giving answers. But if someone could give me a bit of an answer, where to look, what i should keep in mind etc. that would be very much appreciated.
---EDIT---
Route:
Route::group(array('before'=>'auth'), function(){
Route::get('logout', 'HomeController#logout');
Route::get('profile/{username}', 'ProfileController#getIndex');
});
Route::filter('auth', function($route)
{
$id = $route->getParameter('id');
if(Sentry::check() && Sentry::getUser()->id === $id) {
return Redirect::to('/');
}
});
ProfileController
public function getIndex($profile_uname)
{
if(Sentry::getUser()->username === $profile_uname) {
// This is your profile
return View::make('user.profile.index');
} else {
// This isn't your profile but you may see it!
return ??
}
}
View
#extends('layouts.userprofile')
#section('title')
{{$user->username}}'s Profile
#stop
#section('notification')
#stop
#section('menu')
#include('layouts.menus.homemenu')
#stop
#section('sidebar')
#include('layouts.menus.profilemenu')
#stop
#section('content')
<div class="col-sm-10 col-md-10 col-xs-10 col-lg-10">
<div class="panel panel-info">
<div class="panel-heading"><h3>{{ $user->username }}</h3></div>
</div>
</div>
#stop
#section('footer')
#stop
This might work for you:
<?php
public function getIndex($profile_uname)
{
if(Sentry::getUser()->username === $profile_uname) {
// This is your profile
return View::make('user.profile.index');
} else {
// This isn't your profile but you may see it!
return View::make('user.profile.index')->with('user', Sentry::findUserByLogin($profile_uname));
}
}
If username is not your login column, then you can do it in two steps:
$userId = \Cartalyst\Sentry\Users\Eloquent\User::where('username', $profile_uname)->first()->id;
return View::make('user.profile.index')->with('user', Sentry::findUserById($userId));
If you have a User model tied to your users table, you can just do:
$userId = User::where('username', $profile_uname)->first()->id;
return View::make('user.profile.index')->with('user', Sentry::findUserById($userId));
And in this last case you probably will be able to use that same model, since they would be the same in Sentry and pure Eloquent:
$user = User::where('username', $profile_uname)->first();
return View::make('user.profile.index')->with('user', $user);
Also, to avoid a conflict between your views related to the current logged user, you should rename the $user variable you are instantiating via View::share() or View::composer() from $user to $loggedUser or something like that.